Check Number as Sum of Two Prime Numbers in Java



The given task is to write a Java program to check if an integer can be expressed as the sum of two prime numbers. A number is said to be a prime if it has only two factors, which are 1 and itself, and cannot be divided by any other number.

Some examples of prime numbers are 2, 3, 5, 7, 11, 13, and so on. 2 is the only even prime number; all other prime numbers are odd numbers.

Example Scenario

Let's understand the problem with an example. The possible solution is 43 = 2 + 41. Here, 2 and 41 are prime numbers.

Input: num = 43;
Output: res = TRUE

Check Number as Sum of Two Prime Numbers

To check whether a given number can be expressed as the sum of two prime numbers in Java, we need to iterate over the numbers that are smaller than the given integer and then find possible pairs of prime numbers using a nested if statement.

If both numbers in each pair are prime and their sum is equal to the given number, print TRUE; otherwise, print FALSE.

Example 1

A Java program that demonstrates how to check whether a number can be expressed as the sum of two prime numbers is given below:

public class SumOfPrimes {
   public static void main(String[] args) {
      int my_input, i;
      boolean my_temp = false;
      my_input = 34;
      System.out.println("The number is defined as " +my_input);
      for (i = 2; i <= my_input / 2; ++i) {
         if (IsPrime(i)) {
            if (IsPrime(my_input - i)) {
               my_temp = true;
			   break;
            }
         }
      }
      if (!my_temp) {
         System.out.println("FALSE"); 
      } else {
         System.out.println("TRUE");
      }
   }
   static boolean IsPrime(int num) {
      boolean my_prime = true;
      for (int i = 2; i <= num / 2; ++i) {
         if (num % i == 0) {
            my_prime = false;
            break;
         }
      }
      return my_prime;
   }
}

Following is the output of the above program -

The number is defined as 43
TRUE

Example 2

In this Java program, we use the Sieve of Eratosthenes algorithm, which is a more optimized way to find pairs of prime numbers from 1 to a given integer. Then, we check if the number can be expressed as the sum of two prime numbers.

import java.util.Arrays;

public class SumOfPrimes {
   public static void main(String[] args) {
      int my_input = 19;
      System.out.println("The number is defined as " +my_input);
      boolean[] primes = IsPrime(my_input);
      boolean my_temp = false;
      for (int i = 2; i <= my_input / 2; ++i) {
         if (primes[i]) {
            if (primes[my_input - i]) {
               my_temp = true;
               break;
            }
         }
      }
      if (!my_temp) {
         System.out.println("FALSE");
      } else {
         System.out.println("TRUE");
      }
   }

   // Sieve of Eratosthenes algorithm to find all primes less than n
   public static boolean[] IsPrime(int n) {
      boolean[] my_prime = new boolean[n + 1];
      Arrays.fill(my_prime, true);
      my_prime[0] = my_prime[1] = false;
      for (int i = 2; i * i <= n; i++) {
         if (my_prime[i]) {
            for (int j = i * i; j <= n; j += i) {
                my_prime[j] = false;
            }
         }
      }
      return my_prime;
   }
}

Following is the output of the above program -

The number is defined as 19
TRUE
Updated on: 2025-06-19T10:12:31+05:30

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements