Java Code To Check Fibonnaci Series

April 6, 2023

Explanation:

         Fibonacci series means the next number is the sum of the previous two number.

         The First two number is always 0 and 1.
        
         Step 1: In this program,we use class name as Fibonacci.

         Step 2: In that main method,we declare the variable n1=0;n2=1,n3,i,count=10.

         Step 3: And then use for loop.
    
         Step 4: In that loop,the initalization starts from 2 because 0 and 1 are already 
                 printed.

         Step 5: To find n3 value,add n1 and n2.Now the n3 value will be printed.

         Step 6: Now we assign n2 value to n1 and n3 value to n2 in that statement 

                                      n1=n2;
                                      n2=n3;

         Step 7: And then put the value in the previous step n3=n1+n2.

         Step 8: This process will continue until the loop exixts.

Program:

public class Fibonnaci 
{
	public static void main(String args[]) 
	{
		int n1 = 0, n2 = 1, n3, i, num = 10;
		System.out.println("The fibonacci series are:");
		System.out.print(n1 + " " + n2);
		for (i = 2; i < num; ++i) 
		{
			n3 = n1 + n2;
			System.out.print(" " + n3);
			n1 = n2;
			n2 = n3;
		}
	}
}

Compile:

javac Fibonnaci.java
 java Fibonnaci

Output:

The fibonacci series are:
0 1 1 2 3 5 8 13 21 34

You Might Also Like