Fibbonacci Series - Java Program
Fibbonacci Series is a sequence of numbers in which each number is sum of the previous 2 numbers.
Fibonacci Series
0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181 6765 10946 17711 28657 46368 75025 121393 ...
Fibonacci Series - Algorithm
- Start with 2 numbers 0 stored in num1 and 1 stored in num2
- Print the 2 numbers num1 and num2
- Store the second number num2 in a temporary variable temp
- Add first number num1 and second number num2 and store as num2
- Store value of temp in num1
- Print the 2 numbers num1 and num2
- Repeat Step 3 to Step 6 for n number of times. n is the count of fibonacci numbers you want to print
Fibonacci Series - Java Program to print the first 15 fibbonacci numbers
public class FibonacciSeries {
public static void main (String args[]) {
int num1= 0;
int num2= 1;
int temp= 0;
System.out.print(num1 + " ");
System.out.print(num2 + " ");
for(int i=0; i<15; i++) {
System.out.print(" " + ( num1 + num2) + " ");
temp=num2;
num2=num1 + num2;
num1=temp;
}
}
}
Output of the Java program would be:
0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987