Prashnikaप्रश्निका
Code
About
‹ All Computer Science notes

ISC Practicals and Theory - Fibonacci Series (Iterative Technique)

Updated 20 Oct 2026

The Fibonacci series is a sequence of numbers in which every term, after the first two, is the sum of the two terms before it. This note prints the first $n$ terms using a loop that keeps only the last two terms.

Definition

The Fibonacci series starts with $0$ and $1$. Each next term is the sum of the previous two terms:

$F_1 = 0,\ F_2 = 1,\ F_t = F_{t-1} + F_{t-2}$ for $t > 2$

For example, the first 8 terms are $0, 1, 1, 2, 3, 5, 8, 13$, because $1 + 1 = 2$, $1 + 2 = 3$, $2 + 3 = 5$ and so on.

Algorithm

  1. Start. In main, create an object ob of class Fibonacci.
  2. Call accept(): read the number of terms into n.
  3. Call display():
    • If n is $0$ or negative, print an error message and stop.
    • Otherwise set a = 0 and b = 1: the current term and the next term.
    • Repeat for i from $1$ to n:
      • Print a followed by a space.
      • Find c = a + b, the term after b.
      • Move forward: a = b and b = c.
  4. Stop.

Program

The constructor sets n to $0$, accept() reads the input and display() checks it and prints the series with a for loop.

import java.util.Scanner;

public class Fibonacci
{
    int n;

    Fibonacci()
    {
        n = 0;
    }

    void accept()
    {
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter the number of terms: ");
        n = sc.nextInt();
    }

    void display()
    {
        if (n <= 0)
        {
            System.out.println("Invalid input. Number of terms must be positive.");
            return;
        }
        int a = 0, b = 1, c;
        System.out.print("Fibonacci series: ");
        for (int i = 1; i <= n; i++)
        {
            System.out.print(a + " ");
            c = a + b;
            a = b;
            b = c;
        }
        System.out.println();
    }

    public static void main(String args[])
    {
        Fibonacci ob = new Fibonacci();
        ob.accept();
        ob.display();
    }
}

Variable description table

VariableData TypePurpose
nintData member that stores the number of terms to be printed
scScannerObject used to read the number of terms from the keyboard
aintCurrent term of the series, the one to be printed next
bintThe term that comes after a
cintSum of a and b: the term after b
iintLoop counter that counts the terms printed
argsString[]Command line arguments of main (not used)
obFibonacciObject of the class created in main to call the methods

Sample input and output

Sample 1

Enter the number of terms: 8
Fibonacci series: 0 1 1 2 3 5 8 13 

Sample 2

Enter the number of terms: 1
Fibonacci series: 0 

Sample 3

Enter the number of terms: 0
Invalid input. Number of terms must be positive.

One value per line

Dry run

Take $n = 8$. At the start a = 0 and b = 1. The values of c, a and b are those after the pass.

iPrinted (a)c = a + bab
10111
21212
31323
42535
53858
6513813
78211321
813342134

The loop ends when i becomes 9. The output is 0 1 1 2 3 5 8 13, the same as the real run above.

Exam tip: Update in the order c = a + b, then a = b, then b = c. Changing a first loses its old value.

Variations

  1. Sum of terms - "Write a program to find the sum of the first n terms of the Fibonacci series."
  2. Range - "Write a program to display the Fibonacci terms that lie between m and n."
  3. Check a term - "Write a program to accept a number and check whether it is a term of the Fibonacci series."