-
Notifications
You must be signed in to change notification settings - Fork 42
/
Copy pathgoldbach.java
70 lines (69 loc) · 1.47 KB
/
goldbach.java
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import java.util.*;
class goldbach
{
Scanner ob=new Scanner(System.in);
int n=0;
void input()//input a number from user
{
System.out.println("Enter a number");
n=ob.nextInt();
}
boolean isPrime(int n)//check whether it is prime or not
{
int c=0;
for(int i=2;i<n;i++)
{
if(n%i==0)
c++;
}
if(c==0)
return true;
else
return false;
}
void check()//check whether it is goldbach number or not
{
int a=3;
int b=0;
if(n%2==0)
{
if(n>9&&n<50)
{
while(a<n)
{
b=n-a;
if(isPrime(a)&& isPrime(b) && a<=b)
{
if(a+b==n)
System.out.println(a+" + "+b+ " =Goldbach number");
else
System.out.println("Not a Goldbach number");
}
a=a+2;
}
}
else
{
System.out.println("Invalid input");
}
}
else
{
System.out.println("Invalid input");
}
}
void main()
{
goldbach ab=new goldbach();
ab.input();
ab.isPrime(30);
ab.check();
}
}
/* OUTPUT
Enter a number
30
7 + 23 =Goldbach number
11 + 19 =Goldbach number
13 + 17 =Goldbach number
*/