Java Program to Print the Fibonacci Series 1 1 2 3 5 8 13 …N

In the previous article we have discussed about Java Program to Print the Series 1 2 9 28 65 N

In this article we are going to see how to print the Fibonacci series by using Java programming language. Observe, 1 1 2 3 5 8 Pattern In Java, 1+1+2+3+5+8 Java Program, Fibonacci Series Logic In Java, 0 1 2 3 6 Series In Java, 1+(1*2)+(1*2*3) Java Program, Fibonacci Series In Java Using Scanner, 0 1 1 2 3 5 In Java, 1 2 2 3 3 3 Pattern In Java, Fibonacci Series In Java Using While Loop.

Java Program to Print the Fibonacci Series 1 1 2 3 5 8 13 …N

On observing the pattern carefully, we can see 1st and 2nd number is 1, 1

Then the next number follows a logic by adding with the previous 2 numbers.

1
1
1+1 = 2
2+1 = 3
3+2=  5 …. and so on.

Example:

1 1 2 3 5 8 …… N

Let’s see different ways to print Fibonacci series.

Method-1: Java Program to Print the Fibonacci Series 1 1 2 3 5 8 13 …N By Using For Loop

Approach:

  • Create Scanner class object.
  • Declare an integer variable say ‘n’ which holds the Nth term of the series.
  • Prompt the user to enter a number as value of n.
  • Let declare an integer variable say ‘a’, ‘b’ and initialize it to 1, 1 respectively.
  • Use a for loop from i=1 to i<=n-2 where the loop is incremented by 1
  • Inside the for loop we will find the value of c where c=a+b, now after getting the third term now a becomes b and b becomes c and then the loop continues till it is terminated.
  • Print the result in the series.

Program:

import java.util.*;
public class Main
{
    public static void main(String [] args)
    {
        //creating object of Scanner class 
        Scanner s = new Scanner(System.in);
        //Taking input of number of elements in the series
        System.out.println("Enter the Nth term “N” ");
        int n = s.nextInt();
        int a = 1, b=1;
        System.out.print(a+" "+b);
        //for loop to print the series
        for (int i = 1; i <= n-2; i++) 
        {
            int c = a+b;
            a = b;
            b = c;
  	        System.out.print(" "+c);
        } 
    }
}
Output:

Enter the Nth term “N” 
5
1 1 2 3 5

Method-2: Java Program to Print the Fibonacci Series 1 1 2 3 5 8 13 …N By Using While Loop

Approach:

  • Create Scanner class object.
  • Declare an integer variable say ‘n’ which holds the Nth term of the series.
  • Prompt the user to enter a number as value of n.
  • Let declare an integer variable say ‘a’, ‘b’ and initialize it to 1, 1 respectively.
  • Declare and initialize an integer variable i=1
  • Continue a while loop till i<=n-2, where i is incremented by 1.
  • Inside the while loop we will find the value of c where c=a+b, now after getting the third term now a becomes b and b becomes c and then the loop continues till it is terminated.
  • Print the result in the series.

Program:

import java.util.*;
public class Main
{
    public static void main(String [] args)
    {
        //creating object of Scanner class 
        Scanner s = new Scanner(System.in);
        //Taking input of number of elements in the series
        System.out.println("Enter the Nth term “N” ");
        int n = s.nextInt();
        int a=1, b=1;
        System.out.print(a+" "+b);
        int i=1;
        while(i<=n-2)
        {
            int c = a+b;
            a = b;
            b = c;
  	        System.out.print(" "+c);
            i++;
        }      
    }
}
Output:

Enter the Nth term “N” 
7
1 1 2 3 5 8 13

Method-3: Java Program to Print the Fibonacci Series 1 1 2 3 5 8 13 …N By Using User Defined Method

Approach:

  • Create Scanner class object.
  • Declare an integer variable say ‘n’ which holds the Nth term of the series.
  • Prompt the user to enter a number as value of n.
  • Then call a user defined method say printSeries() by passing n as parameter.
  • Inside method declare an integer variable say ‘a’, ‘b’ and initialize it to 1, 1 respectively.
  • Use a for loop from i=1 to i<=n-2 where the loop is incremented by 1
  • Inside the for loop we will find the value of c where c=a+b, now after getting the third term now a becomes b and b becomes c and then the loop continues till it is terminated.
  • Print the result in the series.

Program:

import java.util.*;
public class Main
{
    public static void main(String[] args)
    {
        // creating object of scanner class 
        Scanner s = new Scanner(System.in);
        //Taking input of number of elements in the series
        System.out.println("Enter the value of Nth term 'N' ");
        int n = s.nextInt();
        // calling printSeries method to print the series
        printSeries(n);
    }
    
    //printSeries method to print the series
    public static void printSeries(int n)
    {
        int a=1, b=1;
        System.out.print(a+" "+b);
        //for loop to print the series
        for (int i = 1; i <=n-2; i++) 
        {
            int c = a+b;
            a = b;
            b = c;
  	        System.out.print(" "+c);
        } 
    }
}
Output:

Enter the value of Nth term 'N' 
9
1 1 2 3 5 8 13 21 34

Are you a job seeker and trying to find simple java programs for Interview? This would be the right choice for you, just tap on the link and start preparing the java programs covered to crack the interview.

Test yourself:

  1. Write A Java Program To Print Fibonacci Series
  2. Write A Program To Print Fibonacci Series In Java
  3. Wap To Print Fibonacci Series In Java
  4. Print Fibonacci Series In Java

Related Java Programs: