Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- /**
- *
- * @author Morcitoni
- */
- public class FiboFacto {
- public static void main(String[] args) {
- Scanner sc = new Scanner(System.in);
- int n;
- String respuesta = ";";
- do {
- do {
- System.out.println("Introduzca un numero entero");
- n = Integer.parseInt(sc.nextLine());
- if (n < 1) {
- System.out.println("El número debe ser mayor que cero");
- }
- } while (n < 1);
- if (n % 2 == 0) {
- System.out.println(fibonacci(n));
- } else {
- System.out.println(factorial(n));
- }
- do {
- System.out.println("¿Quieres continuar? (si/no)");
- respuesta = sc.nextLine().toLowerCase().trim();
- if (!respuesta.equals("si") && !respuesta.equals("no")) {
- System.out.println("ERROR: tienes que escribir si o no");
- }
- } while (!respuesta.equals("si") && !respuesta.equals("no"));
- } while (respuesta.equals("si"));
- }
- public static long factorial(int n) {
- long a = n;
- long b = a - 1;
- if (n == 1) {
- a = 1;
- } else {
- while (b != 1) {
- a = a * b;
- b = b - 1;
- }
- }
- return a;
- }
- public static String fibonacci(int n) {
- String fibo = "";
- int a = 0;
- int b = 1;
- int suma = 0;
- if (n == 2) {
- fibo = "La sucesión es 0,1";
- } else {
- for (int i = 0; i < n; i++) {
- suma = a;
- a = a + b;
- b = suma;
- fibo = fibo + suma + (" ");
- }
- }
- return fibo;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement