ICSE Class 10 Computer Applications Question 70 of 76

Revising Basic Java Concepts — Question 74

Back to all questions
74
Question

Question 51

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

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;
        }
    }
}
Output
BlueJ output of KboatFibonacci.java
Answer