Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Main {
- public static int fibonacciSequence(int n0, int n1, int elements){
- if(n0 == n1) System.out.print(String.format(" %d %d", n0,n1));
- if(elements == 0){
- return n0 + n1;
- }
- int currentValue = n0 + n1;
- System.out.print(String.format(" %d", currentValue));
- return(fibonacciSequence(n1,currentValue,elements-1));
- }
- public static void main(String[] args) {
- int n0 = 1;
- int n1 = 1;
- int elements;
- System.out.print("\nPlease provide how many of Fibonacci series should I list: ");
- Scanner scanner = new Scanner(System.in);
- if(scanner.hasNextInt()){
- elements = scanner.nextInt();
- System.out.println(String.format("Will resolve Fibonacci series for %d elements",elements));
- System.out.println("Iteration:");
- System.out.print(String.format("%d %d", n0,n1));
- for (int i = 0; i < elements; i++) {
- int nextN = n0+n1;
- System.out.print(String.format(" %d",nextN));
- n0 = n1;
- n1 = nextN;
- }
- System.out.println("\nRecursion");
- fibonacciSequence(1,1,elements);
- } else {
- System.out.println("Provided input wasn't an int!");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement