-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNumberAsSumOfTwoPrimes.java
More file actions
55 lines (45 loc) · 1.23 KB
/
NumberAsSumOfTwoPrimes.java
File metadata and controls
55 lines (45 loc) · 1.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package numerics;
import java.util.Scanner;
public class NumberAsSumOfTwoPrimes {
private static int checkPrime(int n) {
int i, isPrime = 1;
for (i = 2; i <= n / 2; ++i) {
if (n % i == 0) {
isPrime = 0;
break;
}
}
return isPrime;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int num,flag = 0;
System.out.println("Enter an Integer : ");
num = sc.nextInt();
System.out.println("\n"+num+" is Expressed in terms of Two Prime Numers : ");
for (int i = 2; i <= num / 2; ++i)
{
// condition for i to be a prime number
if (checkPrime(i) == 1)
{
// condition for n-i to be a prime number
if (checkPrime(num - i) == 1)
{
System.out.println(num+" = "+i+" + "+ (num - i));
flag = 1;
}
}
}
if (flag == 0)
System.out.println(num+" cannot be expressed as the sum of two prime numbers.");
}
}
/*
Output :
Enter an Integer :
40
40 is Expressed in terms of Two Prime Numers :
40 = 3 + 37
40 = 11 + 29
40 = 17 + 23
*/