FastFoodCoding

Login


Connect with one of the following accounts





Login

Please wait while we redirect...

Views: 304   



Fibonacci series program in Java, it takes the number of items to be printed and print that many numbers in Fibonacci series.


import java.util.Scanner;

public class Fibonacci {

    public static void main(String[] args) {
        int first = 0;
        int second = 1;
        int next;

        System.out.println("Enter the number of terms for fibonacci series: ");
        Scanner in = new Scanner(System.in);
        int numOfItemsInResult = in.nextInt();
        in.close();

        System.out.print(first + ", " + second);

        for (int count = 3; count <= numOfItemsInResult; count++) {
            next = first + second;
            System.out.print(", " + next);
            first = second;
            second = next;
        }

    }
}

Output:

Enter the number of terms for fibonacci series: 
40
0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584, 4181, 6765, 10946, 17711, 28657, 46368, 75025, 121393, 196418, 317811, 514229, 832040, 1346269, 2178309, 3524578, 5702887, 9227465, 14930352, 24157817, 39088169, 63245986



On By


Top Tutorials

Top Questions

Top Articles

Top Blogs

Top News