in this lesion we learn how what is mean by Fibonacci Series and how to print Fibonacci Series / numbers using java programming language.
What is Fibonacci Series in Java?
A Fibonacci Series in Java is a series of numbers in which the next number is the sum of the previous two numbers. The first two numbers of the Fibonacci series are 0 and 1. The Fibonacci numbers are significantly used in the computational run-time study of an algorithm to determine the greatest common divisor of two integers.
In fibonacci series, next number is the sum of previous two numbers for example 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55 etc. The first two numbers of fibonacci series are 0 and 1.
There are two ways to write the fibonacci series program in java:
Fibonacci Series without using recursion
Fibonacci Series using recursion
Examples:
Input: N = 9
Output: 0 1 1 2 3 5 8 13 21
Fibonacci Series Program in Java using For Loop
public class FibonacciSeries {
public static void main(String[] args)
{
int n = 10;
int previousNumber = 0;
int nextNumber = 1;
System.out.print("Fibonacci Series of "+ n +" numbers:");
for (int i = 1; i <= n; ++i)
{
System.out.print(previousNumber+" ");
int sum = previousNumber + nextNumber;
previousNumber = nextNumber;
nextNumber = sum;
}
}
}
Fibonacci Series of 10 numbers:0 1 1 2 3 5 8 13 21 34
in above program, 1. previousNumber is initialized to 0 and nextNumber is initialized to 1. The Fibonacci For Loop iterates through n number
2. Display the previousNumber
3. Calculates sum of previousNumber and nextNumber
4. Updates new values of previousNumber and nextNumber
Fibonacci Series Based On The User Input:
package javaapplication1;
import java.util.Scanner;
public class FibonacciSeies {
public static void main(String[] args)
{
int n = 0;
int previousNumber = 0;
int nextNumber = 1;
System.out.println("How many numbers you want in Fibonacci:");
Scanner sc = new Scanner(System.in);
n = sc.nextInt();
System.out.print("Fibonacci Series of "+n+" numbers:");
for (int i = 1; i <= n; ++i)
{
System.out.print(previousNumber+" ");
int sum = previousNumber + nextNumber;
previousNumber = nextNumber;
nextNumber = sum;
}
}
}
How many numbers you want in Fibonacci:
20
Fibonacci Series of 20 numbers:0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181
in the above program logic is same as earlier, Instead of hardcoding the number of elements to show in Java Fibonacci Series, the user is asked to write number.
Fibonacci Series Program in Java using While Loop
in this program only change is that we use while loop for Fibonacci Series using java programming.
public class FibonacciWhileExample {
public static void main(String[] args)
{
int n = 15, previousNumber = 0, nextNumber = 1;
System.out.print("Fibonacci Series of "+n+" numbers:");
int i=1;
while(i <= n )
{
System.out.print(previousNumber+" ");
int sum = previousNumber + nextNumber;
previousNumber = nextNumber;
nextNumber = sum;
i++;
}
}
}
Fibonacci Series of 15 numbers:0 1 1 2 3 5 8 13 21 34 55 89 144 233 377