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
- Start. In
main, create an objectobof classFibonacci. - Call
accept(): read the number of terms inton. - Call
display():- If
nis $0$ or negative, print an error message and stop. - Otherwise set
a = 0andb = 1: the current term and the next term. - Repeat for
ifrom $1$ ton:- Print
afollowed by a space. - Find
c = a + b, the term afterb. - Move forward:
a = bandb = c.
- Print
- If
- 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
| Variable | Data Type | Purpose |
|---|---|---|
n | int | Data member that stores the number of terms to be printed |
sc | Scanner | Object used to read the number of terms from the keyboard |
a | int | Current term of the series, the one to be printed next |
b | int | The term that comes after a |
c | int | Sum of a and b: the term after b |
i | int | Loop counter that counts the terms printed |
args | String[] | Command line arguments of main (not used) |
ob | Fibonacci | Object 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.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.
| i | Printed (a) | c = a + b | a | b |
|---|---|---|---|---|
| 1 | 0 | 1 | 1 | 1 |
| 2 | 1 | 2 | 1 | 2 |
| 3 | 1 | 3 | 2 | 3 |
| 4 | 2 | 5 | 3 | 5 |
| 5 | 3 | 8 | 5 | 8 |
| 6 | 5 | 13 | 8 | 13 |
| 7 | 8 | 21 | 13 | 21 |
| 8 | 13 | 34 | 21 | 34 |
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
- Sum of terms - "Write a program to find the sum of the first n terms of the Fibonacci series."
- Range - "Write a program to display the Fibonacci terms that lie between m and n."
- Check a term - "Write a program to accept a number and check whether it is a term of the Fibonacci series."