This program was asked in ISC 2018 Practical computer science paper. It a called Gold Bach number.


Click here to watch java videos

A Gold Bach 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 Gold Bach 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.

Gold Bach number
import java.io.*; 
class GoldBach 
{ 
    static int isPrime(int x) 
    { 
        int rem; 
        int count=0; 
        for(int i=1;i<=x;i++) 
        { 
            rem=x%i; 
            if(rem==0) 
            { 
                count++; 
            } 
        } 
        return count; 
    } 

    public static void main()throws IOException 
    { 
        BufferedReader br=new BufferedReader(new  
                InputStreamReader(System.in)); 
        System.out.println("Enter the number");
        int N=Integer.parseInt(br.readLine());  
        int res1=0; 
        int res2=0; 
        if(N>9 && N<50) 
        { 
            if(N%2==0) 
            { 
                System.out.println("PRIME PAIRS ARE:"); 
                for(int i=3;i<=N;i++) 
                { 
                    if(i%2!=0) 
                    { 
                        res1=isPrime(i); 
                        for(int j=i;j<=N;j++) 
                        { 
                            if(j%2!=0) 
                            { 
                                res2=isPrime(j); 
                                if(res1==2&&res2==2)  
                               { 
                                    int temp=i+j; 
                                    if(temp==N) 
                                    { 
                                        System.out.println(i+", "+j);  } 
                                    }
                                res2=0; 
                              } 
                            } 
                        res1=0; 
                       } 
                  } 
              } 
            else 
            { 
                System.out.println("INVALID INPUT. NUMBER IS ODD");  } 
            } 
        else 
        { 
            System.out.println("INVALID INPUT. NUMBER OUT OF RANGE.");  } 
        } 
     } 

Output:
Enter the number
14
PRIME PAIRS ARE:
3, 11
7, 7


Click here for more java programs

LEAVE A REPLY

Please enter your comment!
Please enter your name here