Rajat Malakar Pages

Monday, 22 July 2013

Using While loop Find the Fibonacci series in Java.

// Rajat Malakar


import java.util.*;

public class Fibonacci {

    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);
        int prev, next, sum, n = 1, count;
        System.out.println("Enter number upto which Fibonacci series to print: ");
        count = sc.nextInt();
        prev = next = 1;
        System.out.println(">>>>>> : Fibonacci Series : <<<<<<");
        while (n <= count) {
            System.out.println(prev);
            sum = prev + next;
            prev = next;
            next = sum;
            n++;
        }
    }
}

satta king 786