Fibonacci Series in Java

You are currently viewing Fibonacci Series in Java

Hello Geeks! In this tutorial, I’m going to share the details about Fibonacci Series in Java program. Initially, We will talk about Fibonacci Series and how to display Fibonacci Series using java program? Okay, Let’s start the tutorial.

What is Fibonacci Series?

The Fibonacci series is nothing but the next term is the sum of the previous two terms. The first two terms of the Fibonacci sequence are 0 followed by 1.

Here, We can use the for loop and the if…condition to display Fibonacci Series using java program. Let’s see one by one clearly.

Fibonacci Series in Java

Now, The for loop will use to display Fibonacci Series in java program here.

class FibonacciExample1{
public static void main(String args[])
{  
 int n1=0,n2=1,n3,i,count=10;  
 System.out.print(n1+" "+n2);//printing 0 and 1  
  
 for(i=2;i<count;++i)//loop starts from 2 because 0 and 1 are already printed  
 {  
  n3=n1+n2;  
  System.out.print(" "+n3);  
  n1=n2;  
  n2=n3;  
 }  

}}

Source Code Execution

Initially, We have to declare the respective variables to store the input and output values. Secondly, The “0” and “1” input values will be stored in the variable. After running this program, The for loop will iterate from i=2 to i<count. Finally, The Fibonacci Series output will be displayed using the system.out.printIn() function.

Output

The output will display below for your reference.

0 1 1 2 3 5 8 13 21 34
Read Also : Java Program to Add Two Numbers

Note

We can also display Fibonacci Series using some other languages like Fibonacci Series in JavaScript and Fibonacci Series in PHP. We have already posted those articles on our web portal. If you want to learn then please read and learn it.

Fibonacci Series in Java

Below, The if…condition will help to show Fibonacci Series using java program here.

class FibonacciExample2{
 static int n1=0,n2=1,n3=0;  
 static void printFibonacci(int count){  
    if(count>0){  
         n3 = n1 + n2;  
         n1 = n2;  
         n2 = n3;  
         System.out.print(" "+n3); 
         printFibonacci(count-1);  
     }  
 }  
 public static void main(String args[]){  
  int count=10;  
  System.out.print(n1+" "+n2);//printing 0 and 1  
  printFibonacci(count-2);//n-2 because 2 numbers are already printed 
 }
}

Program Explanation

As usual, We have to assign the variables for input and output values. After that, The “0” and “1” input values will be stored in the variables. Later, The if…condition will check whether the count value is greater than zero.

If it is greater than zero then it will start to calculate the Fibonacci Series. If the count is equal to 10 then it will print the Fibonacci Series. Finally, The output will show on the screen using the system.out.printIn() function.

Output

The example output will show below for your reference.

0 1 1 2 3 5 8 13 21 34

Final Words

I hope this article helps you to Show Fibonacci Sequence in Java Program. If you face any issues please let me know via the comment section. Share this article with other Java developers via social networks. Thank You!

Manoj S

Hi, I'm Manoj working as a part-time blogger in Coding Diksha. Here, I post about programming to help developers.

Leave a Reply