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

Output:

*** Fibonacci Series Using For Loop ***

Fibonacci Series upto 7 numbers: 0 1 1 2 3 5 8

Click Here For Java Online Compiler

Solution:

public class FibonacciSeriesUsingForLoop {

    public static void main(String[] args) {

        int count = 7;
        int num1 = 0;
        int num2 = 1;
        System.out.println("*** Fibonacci Series Using For Loop ***" + "\n");
        System.out.print("Fibonacci Series upto " + count + " numbers: ");

        for (int i = 1; i <= count; ++i) {
            System.out.print(num1 + " ");

            int sumOfPrevTwo = num1 + num2;
            num1 = num2;
            num2 = sumOfPrevTwo;
        }
    }
}
Share This :

Related Post



sentiment_satisfied Emoticon