Java Program to generate Fibonacci Series

Java program to generate Fibonacci series upto N number
Fibonacci series:0 1 1 2 3 5 8 13 21 34 55

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
package logic;
import java.util.Scanner;
 
public class Fibonacci 
{
	public static void main(String[] args)
	{
	 Scanner c=new Scanner(System.in);
 
	 System.out.print("enter a number");
 
         int n=c.nextInt();
 
         int n1=0;
         int n2=1;
         int n3=0;
         System.out.print(0+" "+1);
         while(n3<n)
         {
 
    	    n3=n1+n2;
    	    n1=n2;
    	    n2=n3;
 
    	    System.out.print(n3+" ");
 
         }
 
	}
 
}

output:

1
2
3
enter a number
50
0 1 1  2  3  5  8  13  21  34  55

Leave a Reply

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