Java program to print a Fibonacci series


Fibonacci Series generates subsequent number by adding two previous numbers. Fibonacci series starts from two numbers − F0 & F1. The initial values of F0 & F1 can be taken 0, 1 or 1, 1 respectively.

Fn = Fn-1 + Fn-2

Algorithm

1. Take integer variable A, B, C
2. Set A = 1, B = 1
3. DISPLAY A, B
4. C = A + B
5. DISPLAY C
6. Set A = B, B = C
7. REPEAT from 4 - 6, for n times

Example

Live Demo

public class FibonacciSeries2{
   public static void main(String args[]) {
      int a, b, c, i, n;
      n = 10;
      a = b = 1;
      System.out.print(a+" "+b);
      for(i = 1; i <= n-2; i++) {
         c = a + b;
         System.out.print(" ");
         System.out.print(c);
         a = b;
         b = c;
      }
   }
}

Output

1 1 2 3 5 8 13 21 34 55

Samual Sam
Samual Sam

Learning faster. Every day.

Updated on: 13-Mar-2020

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements