‹ All Computer Science notes
ISC Practicals and Theory - Prime Number (Using a Class)
Updated 20 Oct 2026
A prime number is a number that has exactly two factors. This version is written as a class Prime with a data member and separate methods to read the number, test it and display the result. The main method only creates an object and calls the methods.
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$.
- $1$ is not prime: it has only one factor.
Algorithm
- In
main, create the objectobof classPrime. The constructorPrime()setsnum = 0. - Call
ob.input(), which reads the number into the data membernum. - Call
ob.display():- If
num < 1, display "Invalid input". - Otherwise call
isPrime()and display whethernumis a prime number.
- If
- The method
isPrime()works as follows:- Set
count = 0. - Repeat for
ifrom $1$ tonum: ifnum % i == 0, increasecountby $1$. - Return
trueifcount == 2, otherwise returnfalse.
- Set
Program
The constructor Prime() initialises num, input() reads the number, isPrime() returns whether it has exactly two factors, and display() prints the message.
import java.util.Scanner;
public class Prime
{
int num;
Prime()
{
num = 0;
}
void input()
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter a number: ");
num = sc.nextInt();
}
boolean isPrime()
{
int count = 0;
for (int i = 1; i <= num; i++)
{
if (num % i == 0)
count++;
}
return count == 2;
}
void display()
{
if (num < 1)
System.out.println("Invalid input");
else if (isPrime())
System.out.println(num + " is a prime number");
else
System.out.println(num + " is not a prime number");
}
public static void main(String args[])
{
Prime ob = new Prime();
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 |
| count | int | Counts the factors of num found so far in isPrime() |
| i | int | Loop counter; each value from 1 to num is tested as a factor |
| args | String[] | Command line arguments of main; not used |
| ob | Prime | Object of the class Prime 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 = 12
| i | num % i | Factor? | count |
|---|---|---|---|
| 1 | 0 | Yes | 1 |
| 2 | 0 | Yes | 2 |
| 3 | 0 | Yes | 3 |
| 4 | 0 | Yes | 4 |
| 5 | 2 | No | 4 |
| 6 | 0 | Yes | 5 |
| 7 | 5 | No | 5 |
| 8 | 4 | No | 5 |
| 9 | 3 | No | 5 |
| 10 | 2 | No | 5 |
| 11 | 1 | No | 5 |
| 12 | 0 | Yes | 6 |
isPrime() returns false because count = 6, so display() shows that $12$ is not a prime number, as in the second sample run.
Variations
- Range: "Display all prime numbers between m and n."
- 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."