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.

Theory
There are many theories which express numbers as a sum of two primes like Goldbach’s Conjecture which states that any even number greater than 2 can be expressed as a sum of two primes.
Prime number is a number which only have two divisors i.e. a number which can not be divided by any other number other than 1 or itself is a prime number.
Here we will check for all the numbers if they can be expressed as sum of two primes or not.
Algorithm
- Take number as input in n
- Initialize a variable flag as 0
- Iterate using for loop from value of i between (2, n/2)
- For each iteration Call a function sum_of_two_primes for value of i is it returns 1
- Call same function for value n-i and if it is also 1 then print i and n-i as answer increment the flag to 1
- If flag is 0 print not possible
- Create function sum_of_two_prime where check if passed number is prime return true else false

Java code
//Java program to check whether a number can be expressed as a sum of two prime numbers import java.util.Scanner; public class Main { 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 : 14 14 = 3 + 11 14 = 7 + 7
Prime Course Trailer
Related Banners
Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription
Login/Signup to comment
package Java.WOKINGNUMBER;
import java.util.Scanner;
public class SumTwoPrimeNo {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println(“Enter a number: “);
int n = sc.nextInt();
int flag = 0;
for (int i = 2; i < n / 2; i++) {
if (sum_of_two_primes(i) && sum_of_two_primes(n – i)) {
System.out.println(n + " = " + i + " + " + (n – i));
flag = 1;
}
}
if (flag == 0) {
System.out.println(n + " is Not possible");
}
}
static boolean sum_of_two_primes(int n) {
if (n < 2) {
return false;
}
for (int i = 2; i < n / 2; i++) {
if (n % i == 0) {
return false;
}
}
return true;
}
}
import java.util.*;
class Main{
String SumOfTwoPrime(int n)
{
for(int i=1;i*i<=n;i++)
{
if(prime(i)==1){
if(prime(n-i)==1)
{
return n+" = "+i+" + "+(n-i);
}
}
}
return "Given num can't be expressed in sum of two primes";
}
int prime(int n)
{
int c=0;
for(int i=1;i*i<=n;i++)
{
if(n%i==0)
{
c++;
if(n/i!=i)
c++;
}
}
if(c==2)
return 1;
else
return 0;
}
public static void main(String args[])
{
Main obj=new Main();
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
System.out.print(obj.SumOfTwoPrime(n));
}
}
public static void sum_of_two_prime_nos(int n) { //brute force
for(int i=2;i<=n;i++) {
for(int j=2;j<Math.sqrt(i);j++) {
int c=0;
if(i%j==0) {
c++;
}
if(c==0) {
for(int k=2;k<=n;k++) {
for(int l=2;l<k;l++) {
int c2=0;
if(k%l==0) {
c2++;
}
if(c2==0) {
if(i+k==n){
System.out.println(i+"+"+k);
return;
}
}
}
}
}
}
}
public class Practice{
public static void main(String[] args) {
Practice pr = new Practice();
pr.issumOfTwoPrime(14);
}
boolean isPr(int n){
for(int i = 2; i<n;i++){
if(n%i==0){
return false;
}
}
return true;
}
void issumOfTwoPrime(int n){
int arr [] = new int [n];
int index = 0;
for (int i=2; i<=n ;i++){
if(isPr(i)){
arr[index++] = i;
}
}
int flag =1;
if(n<4){
System.out.println("it is not possible");
}
else {
for(int i = 0; i<index; i++){
for(int j= 0; j<index;j++){
if(arr[i]+arr[j]==n){
System.out.println(arr[i]+" + "+arr[j]+" = "+n);
}
}
}
}
}
}
package Top100PrepInsta;
import java.util.Scanner;
public class NumberAsSumOfTwoPrimeNum {
public static boolean primeOrNot(int number) {
boolean flag=true; //Prime number
if(number==2) {
flag = true;
}
if(number2) {
for(int i=2; i<number; i++) {
if(number%i==0) {
flag = false;
}
}
}
return flag;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc = new Scanner(System.in);
System.out.print("Enter the number: ");
int number = sc.nextInt();
boolean primeNumPair = false;
for(int i=2; i<=number/2; i++) {
if(primeOrNot(i)) {
int otherNum = number-i;
if(primeOrNot(otherNum)) {
System.out.println("The prime number pair are: "+i+" and "+otherNum);
primeNumPair = true;
}
}
}
if(primeNumPair==false) {
System.out.println("No such pair exists!");
}
}
}
import java.util.*;
public class Main{
public static int isPrime(int a){
int flag=1;
for(int i=2;i<a;i++){
if(a%i==0){
flag=0;
break;
}
}
return flag;
}
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
int flag=0;
for(int i=2;i<=n/2;i++){
if(isPrime(i)==1){
if(isPrime(n-i)==1){
flag=1;
System.out.println("("+i+"+"+(n-i)+")=("+n+")");
}
}
}
if(flag==0){
System.out.println("Number cannot be expressed as a sum of two prime numbers");
}
}
}
package codes100;
import java.util.Scanner;
public class ExpBySumOfPrimes {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
int n= sc.nextInt();int c=0;
for(int i=2; i<n; i++) {
for(int j=2; j<n; j++) {
if(isPrime(i) && isPrime(j)) {
if(i+j==n) {
System.out.println(i+"+"+j+"="+n);
c++;
}
}
}
}
if(c==0) {
System.out.println("no");
}
}
public static boolean isPrime(int n) {
int count=0;
for(int i=2; i0) {
return false;
}else {
return true;
}
}
}