-
Notifications
You must be signed in to change notification settings - Fork 86
/
Copy pathGoldbach.java
54 lines (54 loc) · 1.44 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
import java.util.*;
class Goldbach
{
int N; //declaration
public Goldbach() //default constructor to initialise N
{
N=0;
}
void input() //to input N
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter a number");
N=sc.nextInt();
}
int prime(int n) //to check for prime
{
int c=0;
for(int i=1;i<=n;i++)
{
if(n%i==0)
c++;
}
return c;
}
void sum()
{
if(N%2==0)
{
if(N>9 && N<50)
{
for(int i=3;i<=N/2;i++)
{
for(int j=3;j<=N;j++)
{
if(i+j==N && prime(i)==2 && prime(j)==2)
{
System.out.println("Prime pairs are="+i+" "+j); //printing the odd prime pairs
}
}
}
}
else
System.out.print("Invalid input,Number out of range");
}
else
System.out.print("Invalid input,number is Odd");
}
public static void main()
{
Goldbach ob=new Goldbach(); //creating object
ob.input(); //calling
ob.sum(); //calling
}
}