1. What is the Fibonacci series?
The Fibonacci series (or Fibonacci sequence) is an infinite sequence of positive integers that begins with two elements, 0 and 1. The following elements obey the rule: each element is always the sum of the two preceding elements. For example 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, etc.
The recursive formula for the Fibonacci sequence is: S(n) = S(n-1) + S(n-2) with S(0) = 0 and S(1) = 1.
In programming, there are 2 common types of requirements when working with the Fibonacci sequence:
- Enter n, output the first n digits in the Fibonacci sequence
- Find the nth element in the Fibonacci sequence
In this article, examples will mainly solve the requirement: enter n and output the first n digits in the Fibonacci sequence.
2. Print the Fibonacci series using loops in Java
The code below uses a for loop to find the Fibonacci sequence in Java.
package fibonacci;
import java.util.Scanner;
public class Fibonacci {
public static void main(String[] args) {
int n;
System.out.print("Enter the number of first digits in the fibonacci sequence "
+ "you want to output: ");
try (Scanner scanner = new Scanner(System.in)) {
n = scanner.nextInt();
}
int n1 = 0, n2 = 1;
if (n == 1) {
System.out.print(n1);
} else if (n == 2) {
System.out.print(n1 + " " + n2);
} else {
System.out.print(n1 + " " + n2);
int tempNthTerm;
for (int i = 3; i <= n; i++) {
tempNthTerm = n1 + n2;
System.out.print(" " + tempNthTerm);
n1 = n2;
n2 = tempNthTerm;
}
}
}
}
In this code, we have initialized the variables ‘n1’ and ‘n2’. ‘n1’ and ‘n2’ are initialized to 0 and 1 respectively because the Fibonacci series starts with these two numbers. ‘n’ is entered by a user, represents the number of terms to be printed in the series.
Firstly, we check the cases where n=1 and n=2. If nā„3 then we have used a ‘for’ loop to iterate over from 3 to ‘n’. Inside the loop, we have printed the value of ‘n1’, and ‘n2’, and then calculated the sum of ‘n1’ and ‘n2’. We have then updated the values of ‘n1’ and ‘n2’ for the next iteration.
3. Print the Fibonacci series using recursion in Java
The code below uses recursion to find the Fibonacci sequence in Java.
package fibonacci;
import java.util.Scanner;
public class Fibonacci {
public static int findFibonacci(int n) {
if (n == 0) {
return 0;
}
if (n == 1) {
return 1;
}
return findFibonacci(n - 2) + findFibonacci(n - 1);
}
public static void main(String[] args) {
int n;
System.out.print("Enter the number of first digits in the fibonacci sequence "
+ "you want to output: ");
try (Scanner scanner = new Scanner(System.in)) {
n = scanner.nextInt();
}
for (int i = 0; i < n; i++) {
System.out.print(Fibonacci.findFibonacci(i) + " ");
}
}
}
In this code, we have a main()
method that ‘n’ is entered by a user. We have a for
loop that calls the fibonacci()
method with the value of i
as an argument. The fibonacci()
method is defined to take an integer n
as an argument and it returns the nth
number in the Fibonacci series.
The fibonacci()
method uses recursion to calculate the nth
number in the series. If the value n
is either 0 or 1, we return n
. Otherwise, we recursively call the fibonacci()
method with n-1 and n-2 as arguments and return the sum of the two values.
This article has presented how to use for loops and recursion to output the first n digits in the Fibonacci sequence. Please write a Java program to find the nth element in the Fibonacci sequence.