Print Fibonacci Series Using "While Loop" Only | Java Programs

Output:

*** Fibonacci Series Using While Loop Only ***

Fibonacci Series of 10 numbers: 0 1 1 2 3 5 8 13 21 34

Click Here For Java Online Compiler

Solution:

class FibonacciSeriesUsingWhileLoop {

    public static void main(String[] args) {

        int count = 10;
        int num1 = 0;
        int num2 = 1;
        System.out.println("*** Fibonacci Series Using While Loop Only ***" + "\n");
        System.out.print("Fibonacci Series of " + count + " numbers: ");

        int i = 1;
        while (i <= count) {
            System.out.print(num1 + " ");
            int sumOfPrevTwo = num1 + num2;
            num1 = num2;
            num2 = sumOfPrevTwo;
            i++;
        }
    }
}
Share This :

Related Post



sentiment_satisfied Emoticon