DEV Community

sravanre
sravanre

Posted on

Fibonacci Series Java program

Hi all,
this is a sample easy program example for Fibonacci Series
We are creating a static method from a Class called Series.
Without creating a object of it. we are calling the method in the Main() of the program. N is the number for which you want the fibonacci Series to be created.

import java.util.Scanner;

class Series
{   
    static void SeriesFibonacci(int N)
    {
        int num1 = 0;
        int num2 = 1;

        for (int i=0;i<=N;i++) {
            System.out.print(num1 + ",");
            int num3 = num1 + num2;
            num1 = num2;
            num2 = num3;

        }
    }
}

public class fibinnacoSeries {
    public static void main(String[] args) {

        Scanner Scanner = new Scanner(System.in);
        System.out.println("Enter the number for the fibinnanco series : ");
        Series.SeriesFibonacci(Scanner.nextInt());

    }

}
Enter fullscreen mode Exit fullscreen mode

Output for N = 20

Enter the number for the fibinnanco series : 
20
0,1,1,2,3,5,8,13,21,34,55,89,144,233,377,610,987,1597,2584,4181,6765,
Enter fullscreen mode Exit fullscreen mode

Top comments (0)