Home » Programming » Java Interview Programs » Different types of Java programs to print Fibonacci series

Different types of Java programs to print Fibonacci series

In this article, we are going to see how to write the Java Program to print the Fibonacci series. Here let us see the different ways of the program to print a Fibonacci series in java. Those are

  • Print Fibonacci series using For loop in Java.
  • Display Fibonacci series using while loop in Java.
  • Print Fibonacci series using recursion in Java.
  • Display Fibonacci series within a given number.

Before getting into the program part let me explain what the Fibonacci series is?. Generally, in mathematics, the series is a list of numbers that are generated based on some rule or pattern. In the same way, the Fibonacci series is also a list of numbers (or) series in which each number is the sum of two preceding numbers.

The below is an example of the Fibonacci series.

0 ,1, 1, 2, 3, 5, 8, 13, 21, 34 ...............

Just look into the above series. Any number in the series is the sum of the two preceding numbers. Now let's start to write the java program.

Java program to print Fibonacci series using For loop

The below Java program uses For loop to print the Fibonacci series. Generally, if we want to print the series of numbers then we should use any one of the looping statements in java. Without using looping and iteration then how should we print the series of numbers right.

The below Java program prints the Fibonacci series with the first 10 numbers.

public class FibonacciSeries {	
static void createFibonacciSeries(int count){		
	//The variables are fN=first Number, sN=second number, aN=additional number, count = The total number in the fibonacci series.
	int fN=0, sN=1, aN=0;	
	for(int i=1;i<=count;i++){			
		aN=fN+sN;
		System.out.print(fN+" ");
		fN=sN;
		sN=aN;			
	}
}	
public static void main(String args[]){
	//Print the first 10 fibanocci number
	FibonacciSeries.createFibonacciSeries(10);
}
}

Output

0 ,1, 1, 2, 3, 5, 8, 13, 21, 34

Java program to print Fibonacci series using While loop

As I said earlier if we want to print a series of numbers in java then we should use looping statements. We have already seen the For loop. Now let us use the While loop to print the Fibonacci series in Java.

The below Java program prints the Fibonacci series with the first 10 numbers.

public class FibonacciSeries {	
static void createFibonacciSeries(int count){		
	//The variables are fN=first Number, sN=second number, aN=additional number, count = The total number in the fibonacci series.
	int fN=0, sN=1, aN=0, i=0;
	while(count>i){		
		i++;
		aN=fN+sN;
		System.out.print(fN+" ");
		fN=sN;
		sN=aN;		
	}		
}	
public static void main(String args[]){
	//Print the first 10 fibanocci number
	FibonacciSeries.createFibonacciSeries(10);
}
}

Output

0 ,1, 1, 2, 3, 5, 8, 13, 21, 34

Java program to print Fibonacci series using recursion

As I said already if we want to print a series of numbers in java then we should use any one of the looping statements. But in some cases, we need to print the Fibonacci series without using the looping statements in java.

Cool!. We can do that by using the recursion technique in java. First, let us know what is recursion?. Simple, the function calls itself is called recursion in java.

Instead of doing iteration using looping statements, we can call the method within the same method up to the numbers we want to print in the series. Here the recursion comes into the picture.

The below Java program prints the Fibonacci series with the first 10 numbers.

public class FibonacciSeries {	
//The variables are fN=first Number, sN=second number, aN=additional number, count = The total number in the fibonacci series.
static int fN=0,sN=1,aN=0;	
static void createFibonacciSeries(int count){
	if(count>0){    
        aN = fN + sN;    
        System.out.print(" "+fN);
        fN = sN;    
        sN = aN;           
        createFibonacciSeries(count-1);    
    }  
}	
public static void main(String args[]){
	//Print the first 10 fibanocci number
	FibonacciSeries.createFibonacciSeries(10);
}
}

Output

0 ,1, 1, 2, 3, 5, 8, 13, 21, 34

Note: In this recursion technique, we have declared the variables as static but we didn't declare the variables as static when we use For and While Loop.

The reason is that the values of the variables inside the looping statements like For and While are in the memory whenever the changes happen up to the iteration completes.

But in the recursion technique, I mean within the method the values of variables are not in the memory when calling the method again from inside of it. So this is why we have declared the variables as static at the class level. It keeps the values of the variables in the memory whenever the value changes.

Java program to print Fibonacci series within a given number

Sometimes we want to print the Fibonacci series within the given number. For example, we have to print the Fibonacci series within 100 or 200 and so on.

The below Java program achieves that. It prints the Fibonacci series within 100.

public class FibonacciSeries {	
static void createFibonacciSeries(int count){		
	//The variables are fN=first Number, sN=second number, aN=additional number, count = The total number in the fibonacci series.
	int fN=0, sN=1, aN=0;
	while(count>fN){		
		aN=fN+sN;
		System.out.print(fN+" ");
		fN=sN;
		sN=aN;		
	}		
}	
public static void main(String args[]){
	//Print the fibonacci series within 100
	FibonacciSeries.createFibonacciSeries(100);
}
}

Output

0 ,1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89

Note: All the above programs used variables as integers so that we can display the Fibonacci series within the max range of Integer in Java. The range of Integer in Java is -2147483648 to 2147483647.

But if you need the program to work beyond the Integer range then we should use BigInteger in Java. The below java program uses BigInteger to display the Fibonacci series in Java.

import java.math.BigInteger;
public class FibonacciSeries {	
static void createFibonacciSeries(BigInteger count){		
	//The variables are fN=first Number, sN=second number, aN=additional number, count = The total number in the fibonacci series.
	BigInteger fN=BigInteger.valueOf(0);
	BigInteger sN=BigInteger.valueOf(1);
	BigInteger aN=BigInteger.valueOf(0);
	BigInteger i=BigInteger.valueOf(0);	
	while(count.compareTo(i)==1 ? true : false){
		i = i.add(BigInteger.ONE);
		aN=fN.add(sN);
		System.out.print(fN+" ");
		fN=sN;
		sN=aN;		
	}	
}	
public static void main(String args[]){
	//Print the first 10 fibanocci number
	BigInteger n = new BigInteger("10");
	FibonacciSeries.createFibonacciSeries(n);
}
}

Output

0 1 1 2 3 5 8 13 21 34

The above program displays the first 10 numbers in the Fibonacci series. But you can display the Fibonacci series up to any maximum number.

I hope you understood about the Fibonacci series and the different ways to print the Fibonacci series in java. In the next article let us see the Java Programs for all the features of the Fibonacci Series like how to find a given number is a Fibonacci number? and how to print the Fibonacci series within the numbers range?. Thanks!. Keep Reading!.

Leave a Reply

Your email address will not be published. Required fields are marked *