top of page
  • Writer's pictureSatyam Gupta

GoldBach Number in JAVA

Goldbach Number

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


All even integers greater than 4 are Goldbach numbers. 


The expression of a given even number as a sum of two primes is called a Goldbach partition of that number.


Examples of Goldbach Numbers are :-


6 = 3 + 3

8 = 3 + 5

10 = 3 + 7 = 5 + 5

12 = 7 + 5


Example 1: INPUT: N = 14 Prime pairs are: 3, 11 7, 7 

Output : It is a GoldBach Number


INPUT: N = 28 Prime pairs are: 3, 11 7, 7 

Output : It is a GoldBach Number


 

Algorithm and Program  given Below :-

Algorithm :-

  • Input number in a variable

  • Store the input number in another variable

  • define two arrays used for storing and calculating odd prime numbers

  • Now use if statement to check if the entered number is odd or not.

  • If it is odd Display "Entered number is Invalid"

  • Else using for loops find each prime numbers till the entered number

  • Now use if statement and store only odd prime numbers in both the arrays

  • Now Display "Prime Pairs are:-"

  • Using for loop store sum of two odd prime numbers in a variable (say 's')

  • Using if condition compare the sum with the original number

  • If it satisfy the condition than display the prime pairs.

  • Compile and run the program.


 

Program :-


import java.io.*; import java.util.*; class Goldbach {     public static void main(String args[])     {         Scanner in= new Scanner(System.in);         int i,j,n,p,b=0,c=0,s=0;         System.out.println("Enter Number");         n=in.nextInt(); // Input Number         p=n; // Storing input number         int ar[]=new int[n];// array defined with capacity equal to number         int br[]=new int[n];// array defined with capacity equal to number         if(n%2!=0)// if number is odd         {             System.out.println("Invalid Input, Number is Odd "+n); // printing Invalid         }         else         {             for(i=1;i<=n;i++) //loop used for finding prime number             {                 for(j=1;j<=i;j++)                 {                     if(i%j==0) //condition if number is divisible                     {                         c++; // increasing value of c initially it is 0                     }                 }                 if((c==2)&&(i%2!=0)) // condition for odd Prime numbers                 {                     ar[b]=i; // storing odd prime number                     br[b]=i; // storing odd prime number                     b++; // increasing value of b                 }                 c=0; // initializing c=0 again.             }             System.out.println("Prime Pairs are :- ");             for(i=0;i<b;i++)             {                 for(j=i;j<b;j++)                 {                     s=ar[i]+br[j]; //storing sum of two odd prime numbers                     if(s==p) // condition if the sum is equal to the number                     {                         System.out.print(ar[i]+" , "+br[j]); // printing prime pairs                         System.out.println();                     }                 }             }             System.out.println("It is GoldBach Number "+p);         }     } // end of main method } // end of class


 

Note : The program is written in the easiest way possible so try to understand it. If you face any problem in understanding this program than feel free to contact/comment.


All the Best :)

Keep Learning :)

3,563 views0 comments

Recent Posts

See All
Post: Blog2_Post
bottom of page