Solved ProgramsICSEClass 10Computer Applications

Prime Number Program in Java: Check a Number and Print Primes in a Range

Check whether a number is prime in Java by counting its factors, a faster square-root method with isPrime(), and a program that prints all prime numbers in a range.

4 min read
Solved Programs

"Write a program to check whether a number is prime" appears in almost every ICSE Computer Applications test, and prime checks are part of many ISC practical questions such as twin primes and circular primes. This post explains the logic, gives a simple program and a faster one, and prints all the prime numbers in a range.

What is a prime number?

A prime number has exactly two factors: 1 and the number itself. 2, 3, 5, 7, 11 and 13 are prime. 1 is not prime because it has only one factor, and 2 is the only even prime number. A number with more than two factors, such as 15 (1, 3, 5, 15), is called composite.

Program 1: count the factors

The simplest method follows the definition: count how many numbers from 1 to n divide n exactly. If the count is 2, the number is prime.

import java.util.*;

class PrimeCheck
{
    public static void main(String[] args)
    {
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter a number: ");
        int n = sc.nextInt();

        int count = 0;                   // number of factors of n
        for (int i = 1; i <= n; i++)
        {
            if (n % i == 0)
                count++;
        }
        if (count == 2)
            System.out.println(n + " is a prime number");
        else
            System.out.println(n + " is not a prime number");
    }
}

Output:

Enter a number: 13
13 is a prime number

Dry run for n = 6

i    6 % i   count
1    0       1
2    0       2
3    0       3
4    2       3
5    1       3
6    0       4
count is 4, not 2, so 6 is not a prime number

Program 2: a faster check with a method

If n has a factor other than 1 and n, at least one such factor is no bigger than the square root of n. So the loop only needs to test divisors from 2 while i * i <= n, and it stops at the first factor it finds. For 97 the loop runs 8 times instead of 97. Putting the check in a method isPrime() also makes it easy to reuse.

import java.util.*;

class PrimeFast
{
    static boolean isPrime(int n)
    {
        if (n < 2)
            return false;                // 0 and 1 are not prime
        for (int i = 2; i * i <= n; i++)
        {
            if (n % i == 0)
                return false;            // found a factor other than 1 and n
        }
        return true;
    }

    public static void main(String[] args)
    {
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter a number: ");
        int n = sc.nextInt();
        System.out.println(n + (isPrime(n) ? " is a prime number" : " is not a prime number"));
    }
}

Program 3: prime numbers in a range

With isPrime() written once, printing all primes between two limits is a simple loop.

import java.util.*;

class PrimeRange
{
    static boolean isPrime(int n)
    {
        if (n < 2)
            return false;
        for (int i = 2; i * i <= n; i++)
        {
            if (n % i == 0)
                return false;
        }
        return true;
    }

    public static void main(String[] args)
    {
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter the lower limit: ");
        int a = sc.nextInt();
        System.out.print("Enter the upper limit: ");
        int b = sc.nextInt();

        int count = 0;
        for (int i = a; i <= b; i++)
        {
            if (isPrime(i))
            {
                System.out.print(i + " ");
                count++;
            }
        }
        System.out.println();
        System.out.println("Number of prime numbers = " + count);
    }
}

Output:

Enter the lower limit: 10
Enter the upper limit: 50
11 13 17 19 23 29 31 37 41 43 47
Number of prime numbers = 11

Variable description

  • n — int — the number to be checked
  • i — int — loop counter used as a possible factor
  • count — int — number of factors of n found so far
  • a, b — int — lower and upper limits of the range

Common mistakes

  • Treating 1 as prime. Both programs print "not prime" for 0 and 1.
  • Starting the faster loop at 1 instead of 2: every number is divisible by 1.
  • Printing "prime" inside the loop, which prints a message for every divisor tested.
  • Forgetting to reset count to 0 when checking many numbers in a loop.

Practice questions

  1. Print the first 20 prime numbers.
  2. Find the sum of all prime numbers between 1 and 100.
  3. A twin prime pair is two primes that differ by 2, such as (11, 13). Print all twin primes below 100.
  4. Check whether a number is a prime palindrome, such as 131.

Next, try the twin prime program, the ISC practical on circular primes and the special number programs that use the same digit and factor logic.