← All resources

Write a program to print the first 20 terms of the Fibonacci Series: 0, 1, 1, 2, 3, 5, 8, 13 …

Click here to know more about the Fibonacci Sequence

/*
 * Write a program to print the first 20 terms of the Fibonacci Series
 * 0 + 1 + 1 + 2 + 3 + 5 + 8 + 13 ...
 */
public class Fibonacci
{
    public static void main()
    {
        int i, a=0, b=1, c;
        System.out.println("Fibonacci Series:");
        System.out.println(a);
        System.out.println(b);
        for(i=3;i<=10;i++)
        {
            c = a+b;
            System.out.println(c);
            a=b;
            b=c;
        }
    }
}