Fibonacci series program in Java
In this post, you will learn different ways to write Fibonacci series programs using Java.
The Fibonacci series are the sequence of numbers in which the next number is the sum of the previous two numbers. The Fibonacci series was well-known hundreds of years earlier. The "Fibonacci" name came from the nickname "Bonacci".
We can easily remember Fibonacci Sequence using the Fibonacci Day, which is November 23rd. As 23rd November has the digits "1, 1, 2, 3" which is part of the sequence.
In the above image, the first two numbers are 0 and 1. So, according to the Fibonacci rule, the third number is 1 (sum of 0 and 1). The fourth number is 2 and so on.
0 + 1 = 1 // 0, 1, 1
1 + 1 = 2 // 0, 1, 1, 2
1 + 2 = 3 // 0, 1, 1, 2, 3
2 + 3 = 5 // 0, 1, 1, 2, 3, 5
0 ,1 , 1, 2, 3, 5, 8, 13, 21, 34....
Fibonacci series program in Java using for loop
Here, you will learn how to print Fibonacci series using a for loop in Java. In this below code, f1 contains first number, i.e., 0 and f2 contains second number, i.e., 1 and n contains total Fibonacci series number count.
public class FibonacciSeries{
public static void main(String args[])
{
int f1=0,f2=1,n,i,count = 15;
//printing 0 and 1
System.out.print(f1+" "+f2);
// start the loop from 2
for(i=2;i<count;++i)
{
n = f1+f2;
System.out.print(" "+n);
f1=f2;
f2=n;
}
}
}
Output of the above code:
0 1 1 2 3 5 8 13 21 34 55 89 144 233 377
Fibonacci series program in Java using while loop
Here, you will learn how to print Fibonacci series using while loop in Java. In this below code, n1 contains first number, i.e., 0 and n2 contains second number, i.e., 1 and n contains total Fibonacci series number count. In each iteration of the while loop, we get the next number by adding the previous two numbers and simultaneously we swap the first number with the second and the second with the third.
public class FibonacciSeries{
static void fibonacciSeries(int n)
{
int n1 = 0, n2 = 1;
int counter = 0;
while (counter < n) {
// Print the number
System.out.print(n1 + " ");
int n3 = n2 + n1;
n1 = n2;
n2 = n3;
counter = counter + 1;
}
}
// Driver Code
public static void main(String args[])
{
// initialize number n
int n = 13;
// Call function
fibonacciSeries(n);
}
}
Output of the above code:
0 1 1 2 3 5 8 13 21 34 55 89 144
Fibonacci series in Java using recursion
Recursion function is a function which is called by itself. In the given example, we call the recursion function to get the Fibonacci series. A recursion function continues until some condition is met to prevent it. That's why we use the if statement to break the infinite recursion.
In the given program, we use recursion to generate the fibonacci series. The function fibonacciSeries() is called recursively until we get the result. In the function, we first check if the number count is greater than 0. If yes, we recursively call fibonacciSeries() with the values count-1 and count-2.
public class FibonacciSeries{
static int n1=0,n2=1,n3=0;
static void fibonacciSeries(int count){
if(count>0){
n3 = n1 + n2;
n1 = n2;
n2 = n3;
System.out.print(" "+n3);
fibonacciSeries(count-1);
}
}
public static void main(String args[]){
int count=10;
//printing 0 and 1
System.out.print(n1+" "+n2);
fibonacciSeries(count-2);
}
}
Output of the above code:
0 1 1 2 3 5 8 13 21 34
Related Articles
Capitalize first letter of each word JavaConvert binary to decimal in Java
Convert decimal to binary in Java
Convert decimal to octal in Java
Convert decimal to hexadecimal in Java
Simple interest program in Java
Check whether the given number is even or odd in java
Print prime numbers from 1 to 100 in Java
Java prime number program
Java program to convert celsius to fahrenheit
Java program to check leap year
Java program to find factorial of a number