# Java Program to Check Goldbach Number

In the previous article, we have discussed Java Program to Check Triangular Number

In this article we are going to understand what Triangular number is and how we can check whether a number is Triangular or not in Java with examples.

## Program to Check Goldbach Number

Goldbach numbers are even integer numbers that can be divided into two odd primes. (All integers above 4 are GoldBach Number.)

Example :

6    =  3+3
16  =  13 + 3

In the above examples the numbers are Goldbach numbers as they are sum of two odd primes.

Practice Java programming from home without using any fancy software just by tapping on this Simple Java Programs for Beginners tutorial.

Approach :

1. Enter/declare a number and store it .
2. We check whether the number is greater than 4 or not, if not it is not a Goldbach number.
3. Then we start subtracting the prime numbers from this, until we find another prime.
4. If two primes are found then that number is said to be a Goldbach Number.

Program:

import java.util.Scanner;
public class GoldbachNumber{
public static void main(String args[])
{
//Taking the number as input from the user using scanner class
Scanner scan = new Scanner(System.in);
System.out.print("Enter a number : ");
int num = scan.nextInt();

int temp;
boolean flag = false;
// Checks if the number is above 4 then goes into the loop
if(num>4)
{
// Runs a loop from 3 to the num until the break condition is met
for(int i = 3; i<num; i+=2)
{
// Checks whether the current number is prime else goes out
if(isPrime(i))
{
// Finds the other number and checks if it is prime number
temp = num - i;
if(isPrime(temp))
{
flag = true;
break;
}
}
}
}
if(flag)
{
System.out.println(num+" is a Goldbach number");
}
else
{
System.out.println(num+" is Not a Goldbach number");
}

}

// Function to check for prime
static boolean isPrime(int num)
{
int iter = 2;
boolean flag = true;
while (num > iter)
{
if (num % iter == 0)
{
flag = false;
break;
}
iter++;
}
return flag;
}
}

Output:

Enter a number : 16
16 is a Goldbach number

Provided list ofÂ Simple Java Programs is specially designed for freshers and beginners to get familiarize with the concepts of Java programming language and become pro in coding.