## Question on Goldbach Number

A Goldbach number is a positive even integer that can be expressed as the sum of two odd primes.

Note: All even integer numbers greater than 4 are Goldbach numbers.

**Example:**

6 = 3 + 3

10 = 3 + 7

10 = 5 + 5

Hence, 6 has one odd prime pair 3 and 3. Similarly, 10 has two odd prime pairs, i.e. 3, 7 and 5, 5.

Write a program to accept an even integer ‘N’ where N > 9 and N < 50. Find all the odd prime pairs whose sum is equal to the number ‘N’.

Test your program with the following data and some random data:

**Example 1:**

INPUT:

N = 14

OUTPUT:

Prime pairs are:

3, 11

7, 7

**Example 2:**

INPUT:

N = 30

OUTPUT:

Prime numbers are:

7, 23

11, 19

13, 17

**Example 3:**

INPUT:

N = 17

OUTPUT:

Invalid input. Number is odd.

**Example 4:**

INPUT:

N = 126

OUTPUT:

Invalid input. Number is out of range.

## Java Program on Goldbach Number:

```
import java.io.*;
class Goldbach{
public static void main(String args[])
throws IOException{
int n = 0;
int p = 3;
int q = 0;
InputStreamReader in = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(in);
System.out.print("N = ");
n = Integer.parseInt(br.readLine());
if(n % 2 != 0){
System.out.println("Invalid input. Number is odd.");
return;
}
else if(n < 10 || n > 49){
System.out.println("Invalid input. Number is out of range.");
return;
}
System.out.println("Prime pairs are:");
while(p < n){
q = n - p;
if(isPrime(p) && isPrime(q) && p <= q)
System.out.println(p + ", " + q);
p += 2;
}
}
public static boolean isPrime(int n){
int f = 0;
for(int i = 1; i <= n; i++){
if(n % i == 0)
f++;
}
return f == 2;
}
}
```

## 12 replies on “Goldbach Number Java Program ISC 2018 Practical”

if(f == 2)

return true;

return false;

In this block u don’t need an if statement, u could just use return f == 2 and it would be true or false

Yes, correct. The necessary changes are done.

Sir can you give variable description for this program because I have to give this in my Computer science project

Sir can u pls send the variable description table?

Variable description table is not required in Class 12. However, to make a variable list table, you need to create 3 columns: Variable Name, Data type, Variable Description.

i want allgorithm to this program

Sure Unnati. Here is the algorithm:

Step 1: Input the number to be checked as N.

Step 2: LET P = 3

Step 3: If N MOD 2 != 0 then N is not a Goldbach Number. Thus Exit.

Step 4: If N < 10 OR N > 49 then N is out of range. Thus Exit.

Step 5: While P < N, Repeat Step 6 through 8: Step 6: Let Q = N - P Step 7: If P and Q are Prime and P <= Q then Print P, Q Step 8: Let P = P + 2

Thank you

Sir,in the above program the function isPrime(int) is ‘else’ statement optional if the number is not prime?

No. It is compulsory.

public static boolean isPrime(int n){

int f = 0;

for(int i = 1; i <= n; i++){

if(n % i == 0)

f++;

}

if(f == 2)

return true;

return false;

}

}

Sir,in above method why 'else' statement is not there if the number is not prime as you had mentioned that 'else' statement is compulsory?

Oh, here ‘else’ is not required because the return statement is there to exit from the method.