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

Java Programming Java8Object Oriented Programming

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
raja
Published on 26-Apr-2018 06:49:13
Advertisements