‹ All Computer Science notes
ISC Practicals and Theory - Prime Number (Recursive Technique)
Updated 20 Oct 2026
A prime number is a number that has exactly two factors. In this version the factors are counted by a method that calls itself, instead of a loop.
Definition
A natural number $N$ is a prime number if it has exactly two factors: $1$ and $N$ itself.
- $7$ is prime: its only factors are $1$ and $7$.
- $12$ is not prime: its factors are $1, 2, 3, 4, 6, 12$.
Algorithm
- In
main, create the objectobof classPrimeRecand callinput()to readnum. - Call
display(). Ifnum < 1, display "Invalid input". - Otherwise call
countFactors(1)and check whether it returns $2$. - The recursive method
countFactors(int i)tests the divisori:- Base case: if
i > num, return $0$, since there are no more divisors to test. - Recursive case: if
num % i == 0, return $1$ +countFactors(i + 1). - Otherwise return
countFactors(i + 1).
- Base case: if
- If the count is $2$, display that
numis a prime number, else display that it is not.
Program
The method countFactors(int i) calls itself with the next divisor until i crosses num.
import java.util.Scanner;
public class PrimeRec
{
int num;
PrimeRec()
{
num = 0;
}
void input()
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter a number: ");
num = sc.nextInt();
}
int countFactors(int i)
{
if (i > num)
return 0;
if (num % i == 0)
return 1 + countFactors(i + 1);
return countFactors(i + 1);
}
void display()
{
if (num < 1)
System.out.println("Invalid input");
else if (countFactors(1) == 2)
System.out.println(num + " is a prime number");
else
System.out.println(num + " is not a prime number");
}
public static void main(String args[])
{
PrimeRec ob = new PrimeRec();
ob.input();
ob.display();
}
}Variable description table
| Variable | Data Type | Purpose |
|---|---|---|
| num | int | Data member; stores the number entered by the user |
| sc | Scanner | Object used in input() to read the number |
| i | int | Parameter of countFactors; the divisor being tested |
| args | String[] | Command line arguments of main; not used |
| ob | PrimeRec | Object of the class PrimeRec created in main |
Sample input and output
Sample 1
Enter a number: 7
7 is a prime numberSample 2
Enter a number: 12
12 is not a prime numberSample 3
Enter a number: 1
1 is not a prime numberSample 4
Enter a number: -4
Invalid inputDry run
Input: num = 7. The calls are made in this order, and each returns its value on the way back.
| Call | num % i | Returns |
|---|---|---|
| countFactors(1) | 0 | 1 + countFactors(2) |
| countFactors(2) | 1 | countFactors(3) |
| countFactors(3) | 1 | countFactors(4) |
| countFactors(4) | 3 | countFactors(5) |
| countFactors(5) | 2 | countFactors(6) |
| countFactors(6) | 1 | countFactors(7) |
| countFactors(7) | 0 | 1 + countFactors(8) |
| countFactors(8) | not tested, since $8 > 7$ | 0 |
Returning back: countFactors(7) gives $1 + 0 = 1$, and countFactors(1) gives $1 + 1 = 2$. The count is $2$, so $7$ is a prime number, as in the first sample run.
Variations
- Range: "Display all prime numbers between m and n using a recursive method."
- Twin primes: "Display all twin prime pairs in a given range."
- Emirp number: "Check whether a number is an emirp number: a prime whose reverse is also prime."