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

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

What is a Prime Number?

A prime number is a positive integer greater than 1 that is only divisible by 1 and itself. It cannot be divided evenly by any other number except 1 and itself. For example, 2, 3, 5, 7, 11, and 13 are prime numbers, while 4, 6, 8, and 9 are not.

In this Article, we will write a program to Check Whether a Number can be Expressed as Sum of Two Prime Numbers.

Prime Numbers in Java :

We can check whether a given number is prime or not in java using different methods like loops, Recursion etc. We can also check prime numbers in the given intervals using these methods.

Algorithm to Check Whether a Number can be Expressed as Sum of Two Prime Numbers :

  1. Take the input of the number to be checked.
  2. Initialize a variable and set it to false.
  3. With the help of for loop, iterate over the range from 2 to half of the number.
  4. For each number in the range, check if it is prime or not.
  5. If the current number is prime, check if the difference between the original number and the current number is also prime.
  6. If both numbers are prime, set the variable to true and break out of the for a loop.
  7. After the for loop, check the value of the variable. If it is true, the number can be expressed as the sum of two prime numbers, otherwise it cannot.

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

Run
// Importing all the required packages
import java.util.Scanner;
import java.util.*;

public class Main{
  public static void main(String[] args){
    // Scanner class for taking input  
    Scanner scn = new Scanner(System.in);
    System.out.println("Enter a number: ");
    int n = scn.nextInt();
    
    boolean isFound = false;
    
    // loop to iterate over the given input
    for (int i = 2; i <= n / 2; i++) {
      // if the boolean variable is true
      if (isPrime(i)) {
        int diff = n - i;

        if (isPrime(diff)) {
          System.out.println(n + " can be expressed as the sum of " + i + " and " + diff);
          isFound = true;
        }
      }
    }
    
    // if the boolean variable is false
    if (!isFound) {
      System.out.println(n + " cannot be expressed as the sum of two prime numbers.");
    }
  }

  // function to check whether a number is prime or not
  public static boolean isPrime(int n) {
    if (n <= 1) {
      return false;
    }
    for (int i = 2; i < n; i++) {
      if (n % i == 0) {
        return false;
      }
    }
    return true;
  }
}
Output:

Enter a number: 34
34 can be expressed as the sum of 3 and 31
34 can be expressed as the sum of 5 and 29
34 can be expressed as the sum of 11 and 23
34 can be expressed as the sum of 17 and 17

Explanation :

In the above program, the isPrime function checks whether a number is prime or not. The main program takes a number as input and uses a for loop to iterate over the range from 2 to half of the number, rounded down. For each number in the range, it checks if the number and the difference between the original number and the current number are both prime. If both numbers are prime, it sets the isFound variable to true and breaks out of the for loop. After the for loop has finished, the program checks the value of isFound and outputs the appropriate message.

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription