KnowledgeBoat Logo

Computer Applications

Write a program to print Fibonacci series : 0, 1, 1, 2, 3, 5, 8….

Java

Java Iterative Stmts

17 Likes

Answer

public class KboatFibonacci
{
    public static void main(String args[]) {
        int a = 0;
        int b = 1;
        System.out.print(a + " " + b);
        /*
         * i is starting from 3 below
         * instead of 1 because we have 
         * already printed 2 terms of
         * the series. The for loop will 
         * print the series from third
         * term onwards.
         */
        for (int i = 3; i <= 20; i++) {
            int term = a + b;
            System.out.print(" " + term);
            a = b;
            b = term;
        }
    }
}

Variable Description Table

Program Explanation

Output

BlueJ output of Write a program to print Fibonacci series : 0, 1, 1, 2, 3, 5, 8….

Answered By

8 Likes


Related Questions