Home » Programming » Java Interview Programs » Java programs for all the features of Fibonacci Series

Java programs for all the features of Fibonacci Series

In this article let us see the Java Programs for How to check a number is a Fibonacci number? How to find a Fibonacci number in the series based on the position? How to find the position of a Fibonacci number in the Fibonacci series? and How to print the Fibonacci series within the numbers range?.

How to check a number is a Fibonacci number in Java?

We all know that the Fibonacci series is 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 ……………

I have already explained what is Fibonacci series? and how to write the java program to print the Fibonacci series? in the last article. So if you want to know that then please take a look at Different ways to print the Fibonacci series in Java.

The below is a formula to check a number is a Fibonacci number or not.

5*n2+4 (or) 5*n2-4 should be a perfect square.

For example, let's take the number 13 and find whether this is a Fibonacci number or not. Let's apply that with the first formula. Here n=13.

5*n2+4 = 5*13*13+4 = 849. Now we have to check this 849 is a perfect square or not. The square root of 849 is 29.137. So this is not a perfect square. Let's check with another formula.

5*n2-4 = 5*13*13-4 = 841. The square root of 841 is 29 and this is a perfect square. So this number 13 is a Fibonacci number. Now let's write the java program.

public class checkFibonacci {	
static boolean isFibanocciNumber(int n){			
	int formulaTotal = (5*n*n+4);		
	int formulaTotal1 = (5*n*n-4);
		
	int s  = (int) Math.sqrt(formulaTotal);
	int s1 = (int) Math.sqrt(formulaTotal1);
		
	if(s*s == formulaTotal || s1*s1 == formulaTotal1)
		return true;
	else
		return false;		
}
public static void main(String[] args) {		
	int n = 34;//works upto 22nd Fibonacci number (17711) but not works from 23rd fibonacci number (28657)
	System.out.println(isFibanocciNumber(n) ? "The number "+n+" is a Fibonacci number" : 
		"The number "+n+" is not a Fibonacci number ");
}
}

Output

The number 34 is a Fibonacci number

Note: The above java program works only up to the 22nd Fibonacci number but does not work from the 23rd Fibonacci number in the series. Because we used Integer in the program and the Integer range is -2147483648 to 2147483647 in Java.

Now let us test why this program doesn't work for the Fibonacci numbers from the 23rd position. Let's take the 22nd and 23rd Fibonacci numbers in the series and apply that to the formula.

22nd Fibonacci number is 17711. Let's apply for this number in any one of the formulas.

5*n2+4 = 5*17711*17711+4 = 1568397609 and the square root of 1568397609 is 39603 which is a perfect square and the result of the formula 1568397609 is within the Integer range in Java. So the program doesn't affect by it.

Now let's take a 23rd Fibonacci number 28657 and apply that to the formula.

5*n2+4 = 5*28657*28657+4 = 4106118249. Here the result of the formula is 4106118249 and this is out of the Integer range. So the program doesn't work from the 23rd number of the Fibonacci series due to the limit of the Integer range.

Now you may have a question like then how to find the bigger number is a Fibonacci number or not?. Cool!. We have an option for that too. Yes, we can use BigInteger instead of Integer in the above program.

import java.math.BigDecimal;
import java.math.BigInteger;
public class checkFibonacci {	
static boolean isFibanocciNumber(BigInteger n){		
	BigInteger number = BigInteger.valueOf(5);
	BigInteger number1 = BigInteger.valueOf(4);
		
	BigInteger formulaTotal = ((n.multiply(n)).multiply(number)).add(number1);		
	BigInteger formulaTotal1 = ((n.multiply(n)).multiply(number)).subtract(number1);
		
	BigInteger s  = BigDecimal.valueOf(Math.sqrt(formulaTotal.doubleValue())).toBigInteger();
	BigInteger s1 = BigDecimal.valueOf(Math.sqrt(formulaTotal1.doubleValue())).toBigInteger();
		
	if((s.multiply(s)).equals(formulaTotal) || (s1.multiply(s1)).equals(formulaTotal1))
		return true;
	else
		return false;		
}
public static void main(String[] args) {		
	BigInteger n= new BigInteger("3416454622906707");//works upto 76th fibonacci number 3416454622906707.		
	System.out.println(isFibanocciNumber(n) ? "The number "+n+" is a Fibonacci number" : 
		"The number "+n+" is not a Fibonacci number ");
}
}

Output

The number 3416454622906707 is a Fibonacci number

Note: The above program only works up to the 76th Fibonacci number 3416454622906707 due to the limit of Double range in Java. I have used the Double value of BigInteger to find the square root because the Math.sqrt() function is not available for the BigInteger in Java 8.

But in Java 9 and above the Math.sqrt() function is available for BigInteger too. So we can find any bigger number is a Fibonacci number or not in Java 9 and above using the same program but we have to use the Math.sqrt() method to find the square root of BigInteger.

How to find a Fibonacci number in the series based on the position in Java

We have to know the position of the Fibonacci numbers in the series. The below table describes the Fibonacci numbers with their positions in the series.

Position012345678910...
Fibonacci Numbers011235813213455...

The below Java program finds a Fibonacci number in the series based on the position we want.

import java.math.BigInteger;
public class FindNumberByPosition {	
static BigInteger findNumberBasedOnPositon(int n){		
	int positionCount=0;
	BigInteger number= BigInteger.valueOf(0);
	BigInteger fN=BigInteger.valueOf(0);
	BigInteger sN=BigInteger.valueOf(1);
	BigInteger aN=BigInteger.valueOf(0);		
	while(positionCount<=n){		
		aN=fN.add(sN);
		if(positionCount==n)
		number=fN;
		fN=sN;
		sN=aN;			
		positionCount++;			
	}		
	return number;		
}
public static void main(String[] args) {		
	int n=10;
	System.out.println("The "+n+"th position of Fibonacci number is " + findNumberBasedOnPositon(n));
}
}

Output

The 10th position of Fibonacci number is 55.

We can find the Fibonacci number in the series based on any position. Now let us check the 200th position of number in the Fibonacci series by putting the value n=200.

Output is below for the 200th position.

The 200th position of Fibonacci number is 280571172992510140037611932413038677189525

How to find the position of a Fibonacci number in the Fibonacci series in Java

The below Java program finds the position of a given Fibonacci number in the Fibonacci series.

public class FindPositionOfFibonacci {
static int findPositionOfSeries(int n){
	int fN=0, sN=1, aN=0, position=0;		
	while(n>fN){		
		aN=fN+sN;			
		fN=sN;
		sN=aN;	
		position++;			
	}			
	return position;
}	
public static void main(String[] args) {		
	int n = 6765; //Only we can find up to 46th position but can't find from 47th position		
	System.out.println("The position of a given number is "+findPositionOfSeries(n)+" in the Fibonacci series");
}
}

Output

The position of a given number is 20 in the Fibonacci series.

In the above program, we have to check the position of only the Fibonacci number in the series. So please pass only the Fibonacci number in the method findPositionOfSeries().

But If you want to pass any numbers irrespective of Fibonacci then you should check a number is a Fibonacci number or not before passing to the findPositionOfSeries() method. Please use the first program in this article to check a number is Fibonacci or not.

Note: We can find the maximum position as 46 in the above program. But we can't find the position from 47th, 48th, 49th, etc... due to the limitations of the Integer range in Java (-2147483648 to 2147483647).

Cool!. We have an option in Java to find any bigger position of any bigger Fibonacci number in the series by using BigInteger in Java. The below Java program does that.

import java.math.BigInteger;
public class FindPositionOfFibonacci {
static int findPositionOfSeries(BigInteger n){		
	int position= 0;
	BigInteger fN=BigInteger.valueOf(0);
	BigInteger sN=BigInteger.valueOf(1);
	BigInteger aN=BigInteger.valueOf(0);		
	while(n.compareTo(fN)==1 ? true : false){		
		aN=fN.add(sN);			
		fN=sN;
		sN=aN;	
		position++;		
	}			
	return position;
}	
public static void main(String[] args) {	
	BigInteger n = new BigInteger("139423224561697880139724382870407283950070256587697307264108962948325571622863290691557658876222521294125");
	System.out.println("The position of a given number is "+findPositionOfSeries(n)+" in the Fibonacci series");
}
}

Output

The position of a given number is 500 in the Fibonacci series.

How to print the Fibonacci series within the numbers range in Java

The below java program prints the Fibonacci series within the numbers range 6 to 1000.

public class FibonacciSeriesWithRange {	
static void createFibonacciSeriesByRange(int start, int end){		
	//The variables are fN=first Number, sN=second number, aN=additional number.
	int fN=0, sN=1, aN=0;		
	while(end>fN){
	        aN=fN+sN;
		if(fN>=start)
		System.out.print(fN+" ");
		fN=sN;
		sN=aN;			
	}			
}	
public static void main(String[] args) {		
	int start = 6, end=1000;	
	createFibonacciSeriesByRange(start, end);
}
}

Output

8 13 21 34 55 89 144 233 377 610 987

Again the above program only works up to the maximum limit of Integer in Java. If you want the program to work for the bigger numbers above the Integer range in Java then please use the below program.

import java.math.BigInteger;
public class FibonacciSeriesWithRange {	
static void createFibonacciSeriesByRange(BigInteger start, BigInteger end){		
	//The variables are fN=first Number, sN=second number, aN=additional number.	
	BigInteger fN=BigInteger.valueOf(0);
	BigInteger sN=BigInteger.valueOf(1);
	BigInteger aN=BigInteger.valueOf(0);		
	while(end.compareTo(fN)==1 ? true : false){		
		aN=fN.add(sN);
		if(fN.compareTo(start)==1 ? true : false || fN.equals(start))
		System.out.print(fN+" ");
		fN=sN;
		sN=aN;	
			
	}			
}	
public static void main(String[] args) {		
	BigInteger start = new BigInteger("6");
	BigInteger end = new BigInteger("1000");
	createFibonacciSeriesByRange(start, end);
}
}

Output

8 13 21 34 55 89 144 233 377 610 987

I hope you understood all the java programs in this article related to the Fibonacci series. Thanks!. Keep Reading!.

Leave a Reply

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