Java Program to Print Fibonacci Series
In this tutorial, we will learn how to print the Fibonacci Series. Fibonacci series is a series where the next number is the sum of the previous two numbers. But before moving further, if you are not familiar with the concept of the loops in java, then do check the article on Loops in Java.
Input: Enter the number: 10
Output: The Fibonacci Series is:
0 1 1 2 3 5 8 13 21 34
The above problem can be solved in the following ways:
Approach 1: Using a for loop
Approach 2: Using a while loop
Approach 3: To print the series up to a given number
Approach 4: Using Recursive Function.
Let us look at each of these approaches separately.
Program 1: To Print Fibonacci Series
In this program, we will see how to print the Fibonacci Series in Java using for loop. Here, firstly, we will ask the user to enter the number of terms and then we will find the Fibonacci Series.
Algorithm:
- Start
- Declare a variable for the total number of terms.
- Ask the user to initialize the number of terms.
- Print the first and second numbers of the series.
- Use a for loop to print the Fibonacci series up to that number of terms.
- Update the series terms in each iteration.
- Print the Fibonacci series.
- Stop
Let us look at the below example for a better understanding of the above algorithm.
//Java Program to print Fibonacci series
import java.util.*;
public class Main
{
public static void main(String[] args)
{
//Take input from the user
//Create instance of the Scanner class
Scanner sc=new Scanner(System.in);
int t1 = 0, t2 = 1;
System.out.print("Enter the number of terms: ");
int n=sc.nextInt(); //Declare and Initialize the number of terms
System.out.println("First " + n + " terms of fibonnaci series: ");
//Print the fibonacci series
for (int i = 1; i <= n; ++i)
{
System.out.print(t1 + " ");
int sum = t1 + t2;
t1 = t2;
t2 = sum;
}
}
}
Enter the number of terms: 10
First 10 terms of Fibonacci series:
0 1 1 2 3 5 8 13 21 34
Program 2: To Print Fibonacci Series
In this program, we will see how to print the Fibonacci Series in Java using a while loop. Here, firstly, we will ask the user to enter the number of terms and then we will find the Fibonacci Series.
Algorithm:
- Start
- Declare a variable for the total number of terms.
- Ask the user to initialize the number of terms.
- Print the first and second numbers of the series.
- Use a while loop to print the Fibonacci series up to that number of terms.
- Update the series terms in each iteration.
- Print the Fibonacci series.
- Stop
Let us look at the below example for a better understanding of the above algorithm.
//Java Program to print the Fibonacci series
import java.util.*;
public class Main
{
public static void main(String[] args)
{
//Take input from the user
//Create instance of the Scanner class
Scanner sc=new Scanner(System.in);
int t1 = 0, t2 = 1;
System.out.print("Enter the number of terms: ");
int n=sc.nextInt(); //Declare and Initialize the number of terms
System.out.println("First " + n + " terms of fibonnaci series: ");
//Print the fibonacci series
int i = 1;
while (i <= n)
{
System.out.print(t1 + " ");
int sum = t1 + t2;
t1 = t2;
t2 = sum;
i++;
}
}
}
Enter the number of terms: 10
First 10 terms of Fibonacci series:
0 1 1 2 3 5 8 13 21 34
Program 3: To Print Fibonacci Series
In this program, we will see how to print the Fibonacci Series in Java up to a given number. Here, firstly, we will ask the user to enter the number of terms and then we will find the Fibonacci Series up to that particular number.
Algorithm:
- Start
- Declare a variable.
- Ask the user to initialize the number.
- Print the first and second numbers of the series.
- Use a while loop to print the Fibonacci series up to that number.
- Update the series terms in each iteration.
- Print the Fibonacci series.
- Stop
Let us look at the below example for a better understanding of the above algorithm.
//Java Program to print the Fibonacci series
import java.util.*;
public class Main
{
public static void main(String[] args)
{
//Take input from the user
//Create instance of the Scanner class
Scanner sc=new Scanner(System.in);
int t1 = 0, t2 = 1;
System.out.print("Enter the number: ");
int n=sc.nextInt(); //Declare and Initialize the number
System.out.println("Fibonnaci series upto "+n+": ");
//Print the fibonacci series
while (t1 <= n)
{
System.out.print(t1 + " ");
int sum = t1 + t2;
t1 = t2;
t2 = sum;
}
}
}
Enter the number: 30
Fibonnaci series upto 30:
0 1 1 2 3 5 8 13 21
Program 4: To Print Fibonacci Series
In this program, we will see how to print the Fibonacci Series in Java using recursion. Here, firstly, we will ask the user to enter the number of terms and then we will find the Fibonacci Series.
Algorithm:
- Start
- Declare a variable for the total number of terms.
- Ask the user to initialize the number of terms.
- Print the first and second numbers of the series.
- Call a recursive function to print the Fibonacci series up to that number of terms.
- Update the series terms recursively.
- Print the Fibonacci series.
- Stop
Let us look at the below example for a better understanding of the above algorithm.
//Java Program to print Fibonacci series
import java.util.*;
public class Main
{
static int n1=0,n2=1,n3=0;
//Prints Fibonacci Series using Recursion
static void printFibonacci(int n)
{
if(n>0)
{
n3 = n1 + n2;
System.out.print(" "+n3);
n1 = n2;
n2 = n3;
printFibonacci(n-1);
}
}
public static void main(String args[])
{
//Take input from the user
//Create instance of the Scanner class
Scanner sc=new Scanner(System.in);
System.out.print("Enter the number of terms: ");
int n=sc.nextInt(); //Declare and Initialize the number of terms
System.out.print("Fibonacci Series up to "+n+" terms: ");
System.out.print(n1+" "+n2);//printing 0 and 1
printFibonacci(n-2);
}
}
Enter the number of terms: 12
Fibonacci Series up to 12 terms:
0 1 1 2 3 5 8 13 21 34 55 89