Table of Contents
The sequence refers to the number of consecutive numbers beginning with 0 and 1 such that each subsequent number is the sum of the two preceding ones. For example, 0, 1, 1, 2, 3, 5, 8, 13, and so on (each number is one greater than the preceding one). The Fibonacci series has an infinite number of terms. The fibonacci series using recursion is named after an Italian mathematician who lived in the 12th century, Leonardo of Pisa, also known as Fibonacci. Fibonacci was fascinated by the rabbits he saw in his homeland; he noticed that there were times when a rabbit would give birth to two little ones at once, and then within several months, it would have more babies than just one or two at once. He began to wonder about this mathematical phenomenon in nature and came up with the following rules regarding how rabbits will reproduce.
Display Fibonacci Series
The following term in the Fibonacci series is equal to the sum of the two terms before it. The Fibonacci sequence’s first two terms are 0 and 1, respectively.
Fibonacci Series: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34
Suppose, our first two terms are:
firstTerm = 0
secondTerm = 1
The next terms in the Fibonacci series would be calculated as:
nextTerm = firstTerm + secondTerm; (0 + 1)
firstTerm = secondTerm; (1)
secondTerm = nextTerm; (1)
nextTerm = firstTerm + secondTerm; (1 + 1)
....
Let’s now apply this logic to our program.
Click here to know more about Java programming in Entri App!
Display Fibonacci Series Using for Loop
You can use the program below to learn how to create a Java program that uses a for loop to generate the first ‘n’ numbers in the Fibonacci series. This reasoning is really straightforward. I have initialized the series’ first two numbers first. The next step is the for loop, which prints the value and adds its two immediate predecessors. Up until the program prints the first “n” numbers in the series, this process continues.
class Main {
public static void main(String[] args) {
int n = 10, firstTerm = 0, secondTerm = 1;
System.out.println("Fibonacci Series till " + n + " terms:");
for (int i = 1; i <= n; ++i) {
System.out.print(firstTerm + ", ");
// compute the next term
int nextTerm = firstTerm + secondTerm;
firstTerm = secondTerm;
secondTerm = nextTerm;
}
}
}
Output
Fibonacci Series till 10 terms: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34,
Enroll in our latest java programming course in the Entri App!
FirstTerm and SecondTerm are initialised with 0 and 1 in the programme above, respectively (first two digits of Fibonacci series).
Here, the for loop has been utilised to
print the first term in the series and then add the first and second terms to get the next term.
Add the value of the second term to the first term and the second term to the next term.
Display Fibonacci series using while loop
The reasoning is comparable to the earlier approach. You only need to be watchful of the while loop condition. To learn how to generate Fibonacci Series using a while loop, check at the Java code below.
class Main {
public static void main(String[] args) {
int i = 1, n = 10, firstTerm = 0, secondTerm = 1;
System.out.println("Fibonacci Series till " + n + " terms:");
while (i <= n) {
System.out.print(firstTerm + ", ");
int nextTerm = firstTerm + secondTerm;
firstTerm = secondTerm;
secondTerm = nextTerm;
i++;
}
}
}
Output
Fibonacci Series till 10 terms: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34,
This program operates in a similar manner to the one that came before it.
Additionally, even though both scripts are theoretically accurate, using a for loop in this situation is preferable. It’s because we know how many iterations there will be (between 1 and n).
Enroll in our latest java programming course in Entri App!
Display the Fibonacci series up to a given number
class Fibonacci {
public static void main(String[] args) {
int n = 100, firstTerm = 0, secondTerm = 1;
System.out.println("Fibonacci Series Upto " + n + ": ");
while (firstTerm <= n) {
System.out.print(firstTerm + ", ");
int nextTerm = firstTerm + secondTerm;
firstTerm = secondTerm;
secondTerm = nextTerm;
}
}
}
Output
Fibonacci Series Upto 100: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89,
In this illustration, we are displaying the Fibonacci sequence up to the specified number rather than the series up to a specific integer (100).
To do this, all we have to do is compare first term with n. Additionally, it is printed in the series if first term is less than n. Otherwise, the series is finished.
Fibonacci Series using recursion
The fundamental Java programming concept of recursion involves a function calling itself either directly or indirectly. A recursive function is a name given to the related function. A recursive algorithm can be used to tackle some issues with relative ease. Let’s look at how to print the first ‘n’ Fibonacci numbers in Java using recursion. You can learn how to create a recursive Java program to generate the first ‘n’ numbers in the Fibonacci series using recursion by using the program below. The reasoning behind this is pretty clear-cut. The for loop is used to loop until the limit after which each iteration calls the Fibonacci number(int n) function, which returns the Fibonacci number at position n. The previous two Fibonacci numbers are added by the Fibonacci function iteratively.
package Edureka; import java.util.Scanner; public class FibRec { public static void main(String[] args) { int n; System.out.println( "Enter how may fibonnaci numbers to print" ); Scanner scanner = new Scanner(System.in); n = scanner.nextInt(); for ( int i = 0 ; i<=n- 1 ; ++i) { System.out.print(fibonaccinumber(i) + " " ); } } public static int fibonaccinumber( int n) { if (n== 0 ) return 0 ; else if (n== 1 ) return 1 ; else return fibonaccinumber(n- 1 ) + fibonaccinumber(n- 2 ); } } |
Output:
1
2
3
|
Enter how may fibonnaci numbers to print 7 The first 7 Fibonacci numbers are: 0 1 1 2 3 5 8 |
Get the latest updates on java programming in the Entri app
If you are interested to learn new coding skills, the Entri app will help you to acquire them very easily. Entri app is following a structural study plan so that the students can learn very easily. If you don’t have a coding background, it won’t be a problem. You can download the Entri app from the google play store and enroll in your favorite course.