Can a number be expressed as a sum of two prime numbers using Java?

Can a number be expressed as a sum of two prime numbers using java?

Here, we will discuss program to check whether a number be expressed as a sum of two prime numbers using java.  we will ask the user to enter a positive integer and check whether that number can be expressed as the sum of two prime numbers. If that number can be expressed as sum of two prime numbers then print the numbers in the output and else print the statement that the number cannot be expressed as a sum of two prime numbers.

Number be expressed as a sum of two prime numbers using java

Working :

Step 1 : Ask the user to enter a positive integer.

Step 2 : Declare a variable with value to 0 for indication purpose so that if the number can be expressed as the sum of two prime numbers then the value 0 will change to 1, else remain same.

Step 3 : Use a loop and start it with 2 till number/2 (where number is a positive integer entered by the user) and increment the value of the loop by 1 in every iteration. 

Step 4 : Use a condition by calling function and passing the value of the loop and checking whether the returned value from the function is equal to 1 or not.If this condition get true then move to next step else move to step 3.

Note : We have considered that we have already created a returnable static function to check whether the number is prime or not.

Step 5 : Use another condition for second number and call function by passing value (number – value of the loop) and check whether the returned value from the function is equal to 1 or not. If this condition gets true then move to next step else move to the step 3.

Step 6 : Print the values which are checked in the conditions and increment the value of x by 1 so that we can get the indication that we have found the two prime numbers whose sum is equal to the entered number.

Step 7 : Repeat the steps 3 to 6 till the condition of the loop does not gets false.

Step 8 : Check if the value of variable which was declared for indication purpose is 0 then the number which was entered by user cannot be expressed as a sum of two prime numbers.

Code to check whether a Number be expressed as a sum of two prime numbers using java :

//Java program to check whether a number can be expressed as a sum of two prime numbers
import java.util.Scanner;
public class number_as_sum_of_two_prime_numbers
{
public static void main(String[] args)
{
//scanner class declaration
Scanner sc = new Scanner(System.in);
//input from user
System.out.print("Enter a number : ");
int number = sc.nextInt();
int x = 0;
for(int i = 2 ; i <= number/2 ; i++)
{
if(prime_or_not(i) == 1)
{
if(prime_or_not(number-i) == 1)
{
System.out.println(number+ " = "+i+" + "+(number-i));
x = 1;
}
}
}
if(x == 0)
System.out.println(+number+" cannot be expressed as a sum of two prime numbers");
}
//function for checking number is prime or not
public static int prime_or_not(int n)
{
int c = 1;
for(int i = 2 ; i < n ; i++)
{
if(n % i == 0)
{
c = 0;
break;
}
}
return c;
}
}

Output :

Enter a number : 34
34 = 3 + 31
34 = 5 + 29
34 = 11 + 23
34 = 17 + 17

Enter a number : 23
23 cannot be expressed as a sum of two prime numbers