Java program to print the fibonacci series of a given number using while loop


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.

Example

Live Demo

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

      while(i<n) {
         c = a + b; System.out.print(" ");
         System.out.print(c);
         a = b;
         b = c;
         i++;
      }
   }
}

Output

1 1 2 3 5 8 13 21 34 55 89

Updated on: 13-Mar-2020

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements