Java Program to Check Whether a Number can be Expressed as Sum of Two Prime Numbers


In this article, we will understand how to check whether a number can be expressed as sum of two prime numbers. Prime numbers are special numbers who have only two factors 1 and itself and cannot be divided by any other number.

A number is a prime number if its only factors are 1 and itself. 11 is a prime number. Its factors are 1 and 11 itself. 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.

Below is a demonstration of the same −

Input

Suppose our input is −

Input number : 43

Output

The desired output would be −

The number can be expressed as sum of two prime numbers.
The possible solutions are :
43 = 2 + 41

Algorithm

Step 1 - START
Step 2 - Declare two integer values namely my_input and i
Step 3 - Read the required values from the user/ define the values
Step 4 - Define a function IsPrime which takes an integer value and checks if the value is a prime number or not.
Step 5 - Using a for loop, iterate from 2 to half of ‘my_input’ value, check if the ‘i’ value and ‘my_input’ – ‘i’ values are both prime numbers. If yes, store both the values.
Step 6 - Display the result
Step 7 - Stop

Example 1

Here, the input is being entered by the user based on a prompt. You can try this example live in ourcoding ground tool run button.

import java.util.Scanner;
public class SumOfPrimes {
   public static void main(String[] args) {
      int my_input, i;
      boolean my_temp = false;
      my_input = 43;
      System.out.println("Required packages have been imported");
      Scanner my_scanner = new Scanner(System.in);
      System.out.println("A reader object has been defined ");
      System.out.print("Enter the number : ");
      my_input = my_scanner.nextInt();
      for (i = 2; i <= my_input / 2; ++i) {
         if (IsPrime(i)) {
            if (IsPrime(my_input - i)) {
               System.out.println("The number can be expressed as sum of two prime numbers.");
               System.out.println("The possible solutions are :");
               System.out.printf("%d = %d + %d\n", my_input, i, my_input - i);
               my_temp = true;
             }
         }
      }
      if (!my_temp)
         System.out.println(my_input + " cannot be expressed as the sum of two prime numbers.");
   }
   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;
   }
}

Output

Required packages have been imported
A reader object has been defined
Enter the number : 43
The number can be expressed as sum of two prime numbers.
All the possible solutions are :
43 = 2 + 41

Example 2

Here, the integer has been previously defined, and its value is accessed and displayed on the console.

public class SumOfPrimes {
   public static void main(String[] args) {
      int my_input, i;
      boolean my_temp = false;
      my_input = 43;
      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)) {
               System.out.println("The number can be expressed as sum of two prime numbers.");
               System.out.println("The possible solutions are :");
               System.out.printf("%d = %d + %d\n", my_input, i, my_input - i);
               my_temp = true;
            }
         }
      }
      if (!my_temp)
         System.out.println(my_input + " cannot be expressed as the sum of two prime numbers.");
   }
   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;
   }
}

Output

The number is defined as 43
The number can be expressed as sum of two prime numbers.
All the possible solutions are :
43 2 + 41

Updated on: 22-Feb-2022

738 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements