Prashnikaप्रश्निका
Code
About
‹ 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

  1. In main, create the object ob of class Prime. The constructor Prime() sets num = 0.
  2. Call ob.input(), which reads the number into the data member num.
  3. Call ob.display():
    • If num < 1, display "Invalid input".
    • Otherwise call isPrime() and display whether num is a prime number.
  4. The method isPrime() works as follows:
    • Set count = 0.
    • Repeat for i from $1$ to num: if num % i == 0, increase count by $1$.
    • Return true if count == 2, otherwise return false.

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

VariableData TypePurpose
numintData member; stores the number entered by the user
scScannerObject used in input() to read the number
countintCounts the factors of num found so far in isPrime()
iintLoop counter; each value from 1 to num is tested as a factor
argsString[]Command line arguments of main; not used
obPrimeObject of the class Prime created in main

Sample input and output

Sample 1

Enter a number: 7
7 is a prime number

Sample 2

Enter a number: 12
12 is not a prime number

Sample 3

Enter a number: 1
1 is not a prime number

Sample 4

Enter a number: -4
Invalid input

One value per line

Dry run

Input: num = 12

inum % iFactor?count
10Yes1
20Yes2
30Yes3
40Yes4
52No4
60Yes5
75No5
84No5
93No5
102No5
111No5
120Yes6

isPrime() returns false because count = 6, so display() shows that $12$ is not a prime number, as in the second sample run.

Variations

  1. Range: "Display all prime numbers between m and n."
  2. Twin primes: "Display all twin prime pairs in a given range."
  3. Emirp number: "Check whether a number is an emirp number: a prime whose reverse is also prime."