Goldbach Number Java Program ISC Computer Science 2025 Practical

A number is said to be a Goldbach number, if the number can be expressed as the addition of two odd prime number pairs. If we follow the above condition, then we can find that every even number larger than 4 is a Goldbach number because it must have any pair of odd prime number pairs.

Example:
6 = 3, 3 (One pair of odd prime)
10 = 3, 7 and 5, 5 (Two pairs of odd prime)

Write a program to enter any positive even natural number ‘N’ where 1 ≤ N ≤ 50 and generate odd prime twin of ‘N’.

Test your program for the following data and some random data.

Example 1
INPUT: N = 14
OUTPUT:
ODD PRIME PAIRS ARE:
3, 11
7, 7

Example 2
INPUT: N = 20
OUTPUT:
ODD PRIME PAIRS ARE:
17, 3
13, 7

Example 3
INPUT: N = 44
OUTPUT:
ODD PRIME PAIRS ARE:
41, 3
37, 7
31, 13

Example 4
INPUT: N = 25
OUTPUT:
INVALID INPUT

import java.util.Scanner;
class Goldbach{
    public static void main(String[] args){
        Scanner in = new Scanner(System.in);
        System.out.print("N = ");
        int n = Integer.parseInt(in.nextLine());
        if(n > 50 || n % 2 != 0){
            System.out.println("INVALID INPUT");
            return;
        }
        int i = 3;
        while(i <= (n - i)){
            if(isPrime(i) && isPrime(n - i))
                System.out.println(i + ", " + (n - i));
            i++;
        }
    }
    public static boolean isPrime(int n){
        int f = 0;
        for(int i = 1; i <= n; i++){
            if(n % i == 0)
                f++;
        }
        return f == 2;
    }
}