Skip to content

Instantly share code, notes, and snippets.

@juanecl juanecl/Fibonacci.java
Last active Dec 21, 2018

Embed
What would you like to do?
Function to get Fibonacci series numbers from one to an entered number by user
import java.util.ArrayList;
import java.util.List;
import java.util.*;
public class Fibonacci {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int end, init, aux, i, ciclo;
List < Integer > series = new ArrayList < > ();
System.out.print("Please enter a number higher than 1: ");
try {
number = scanner.nextInt();
System.out.println("The first " + number + " of the Fibonacci series are: ");
first = 1;
second = first;
series.add(first);
for (i = 2; i <= number; i++) {
series.add(aux);
System.out.println(aux);
aux = init + aux;
init = aux - init;
}
System.out.println("The variable series contains the numbers");
} catch (Exception e) {
System.out.println("Error exception: " + e);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.