codescracker


java

Java Program to Print Fibonacci Series



« Previous Program Next Program »


Print Fibonacci Series

To print Fibonacci series in Java Programming, you have to first print the starting two of the Fibonacci series and make a while loop to start printing the next number of the Fibonacci series. Use the three variables say a, b and c. Place b in c and c in a, and now place a+b in c to print the value of c to make the Fibonacci series as shown in the following program.

Java Programming Code to Print Fibonacci Series

Following Java Program ask to the user to enter the limit upto which he/she want to print the Fibonacci series:

/* Java Program Example - Print Fibonacci Series */
		
import java.util.Scanner;

public class JavaProgram
{
    public static void main(String args[])
    {
	
        int a=0, b=1, c=0, limit;
        Scanner scan = new Scanner(System.in);
		
        System.out.print("Upto How Many Term ? ");
        limit = scan.nextInt();
        
        /* print the first two terms of the Fibonacci series */
        
        System.out.print("Fibonacci Series : " + a + "  " + b + "  ");
        c = a + b;
        
        /* decrease the limit by 2, since the first two number 
         * of the Fibonacci series already printed manually
         */
        
        limit = limit - 2;
		
        while(limit>0)
        {
            System.out.print(c + "  ");
            a = b;
            b = c;
            c = a + b;
            limit--;
        }
		
    }
}

When the above Java Program is compile and executed, it will produce the following output:

Java Program print Fibonacci series

Same Program in Other Languages

You may also like to learn and practice the same program in other popular programming languages:


« Previous Program Next Program »



Tools
Calculator

Quick Links
Signup - Login - Give Online Test