Advertisement
Marin126

Fibofacto

Nov 11th, 2019
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.06 KB | None | 0 0
  1.  
  2. import java.util.Scanner;
  3.  
  4. /*
  5. * To change this license header, choose License Headers in Project Properties.
  6. * To change this template file, choose Tools | Templates
  7. * and open the template in the editor.
  8. */
  9. /**
  10. *
  11. * @author Morcitoni
  12. */
  13. public class FiboFacto {
  14.  
  15. public static void main(String[] args) {
  16. Scanner sc = new Scanner(System.in);
  17. int n;
  18. String respuesta = ";";
  19. do {
  20. do {
  21. System.out.println("Introduzca un numero entero");
  22. n = Integer.parseInt(sc.nextLine());
  23. if (n < 1) {
  24. System.out.println("El número debe ser mayor que cero");
  25. }
  26.  
  27. } while (n < 1);
  28.  
  29. if (n % 2 == 0) {
  30. System.out.println(fibonacci(n));
  31. } else {
  32. System.out.println(factorial(n));
  33. }
  34.  
  35. do {
  36.  
  37. System.out.println("¿Quieres continuar? (si/no)");
  38. respuesta = sc.nextLine().toLowerCase().trim();
  39. if (!respuesta.equals("si") && !respuesta.equals("no")) {
  40. System.out.println("ERROR: tienes que escribir si o no");
  41. }
  42. } while (!respuesta.equals("si") && !respuesta.equals("no"));
  43.  
  44. } while (respuesta.equals("si"));
  45.  
  46. }
  47.  
  48. public static long factorial(int n) {
  49. long a = n;
  50. long b = a - 1;
  51. if (n == 1) {
  52. a = 1;
  53. } else {
  54. while (b != 1) {
  55. a = a * b;
  56. b = b - 1;
  57.  
  58. }
  59. }
  60. return a;
  61. }
  62.  
  63. public static String fibonacci(int n) {
  64. String fibo = "";
  65. int a = 0;
  66. int b = 1;
  67. int suma = 0;
  68. if (n == 2) {
  69. fibo = "La sucesión es 0,1";
  70. } else {
  71. for (int i = 0; i < n; i++) {
  72. suma = a;
  73. a = a + b;
  74. b = suma;
  75. fibo = fibo + suma + (" ");
  76.  
  77. }
  78. }
  79. return fibo;
  80. }
  81. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement