Fibonacci Series in Java – The series of numbers up to a given number.

Home / Uncategorized / Fibonacci Series in Java – The series of numbers up to a given number.

Fibonacci Series in Java – The series of numbers up to a given number.

import java.util.Scanner;
public class FibonacciNumber 
{
     static int i=1,n,a=0,b=1,c;
     public static void main(String arges[])
     {
        System.out.print("Enter any number:");
        Scanner sc=new Scanner(System.in);
        n=sc.nextInt();
        System.out.print(a);
        System.out.print(" "+b);
        while(i<=n)
        {
            c=a+b;
            System.out.print(" "+c);
            a=b;
            b=c;
            i++;
        }
        System.out.println();
    }
}

Output:

Enter any number:10
0 1 1 2 3 5 8 13 21 34 55 89

Watch video of this program

Leave a Reply

Your email address will not be published. Required fields are marked *