/* Write a program to determine whether a given number can be expressed as sum of two prime numbers or not.
For example 34 can be expressed as sum of two prime numbers but 23 cannot be.
*/
import java.util.*;
import java.io.*;
public class SumOfTwoPrime {
public static void Primer(int num) {
ArrayList<Integer> ar = new ArrayList<Integer>();
boolean flag=false;
int prevPrime = 1;
for(int i=1;i<=num;i++) {
for(int j=2;j<i;j++) {
if(i%j==0) {
flag=false;
break;
}
else flag=true;
}
if(flag==true) {
ar.add(i);
}
}
for(int i=0;i<ar.size();i++) {
for(int j=0;j<ar.size();j++) {
if( (ar.get(i))+(ar.get(j)) == num )
System.out.println(num+" is sum of two primes "+ar.get(i)+" and "+ar.get(j));
}
}
}
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
Primer(n);
}
}
For example 34 can be expressed as sum of two prime numbers but 23 cannot be.
*/
import java.util.*;
import java.io.*;
public class SumOfTwoPrime {
public static void Primer(int num) {
ArrayList<Integer> ar = new ArrayList<Integer>();
boolean flag=false;
int prevPrime = 1;
for(int i=1;i<=num;i++) {
for(int j=2;j<i;j++) {
if(i%j==0) {
flag=false;
break;
}
else flag=true;
}
if(flag==true) {
ar.add(i);
}
}
for(int i=0;i<ar.size();i++) {
for(int j=0;j<ar.size();j++) {
if( (ar.get(i))+(ar.get(j)) == num )
System.out.println(num+" is sum of two primes "+ar.get(i)+" and "+ar.get(j));
}
}
}
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
Primer(n);
}
}
Comments
Post a Comment