GOLDBATCH NUMBER | ISC COMPUTER 2024 | survnor.blogspot.com
A Goldbach number is a positive even integer that can
be expressed as the sum of two odd primes(all even integer number greater than
four are goldbach number). Example:- 6=3+3,
10=3+7, 10=5+5
Therefore, 6 has one odd prime pair 3 and 3. Similarly
10 has two odd prime pair i.e., 3 and 7, 5 and 5. Write a program to accept an
even integer ‘N’ where N>9 and N<50. Find all odd prime pair whose sum is
equal to the number ‘N’.
Example 1:- Input:- N=30
Output:- PRIME PAIRS ARE:-
7+23
11+19
13+17
ALGORITHM
STEP 1:- Start.
STEP 2:- Declare Goldbach as a class.
STEP 3:- Declare isprime as a method with
return type Boolean and passing the formal parameter N with type int.
STEP 4:- Now check whether N<1 or not,
then return false.
STEP 5:- Declare i as a variable of type
int. Start iteration where i initialize by 2 upto N/2 and incremented in each
step
STEP 6:- Now check whether n divided by i
and remainder equal to 0, if satisfy then return false.
STEP 7:- Return true, if the condition which
is mentioned in step 4 not satisfied. Now close isprime method.
STEP 8:- Declare print as a method with
return type void with passing n as a formal parameter. Declare ‘i’ and ‘j’ as a
variable.
STEP 9:- Now start iteration, where ‘i’
initialize by 2 up to N and incremented 1 in each step. Start a nested loop
where ‘j’ initialize by ‘i’ up to less than equal to and incremented 1 in each step.
STEP 10:- Now check whether isprime(i) and
isprime(i) and i+jßN. Now print the value of ‘i’ and ‘j’ and
separate them by ‘,’.
STEP
11:- Now close the
method print.
STEP
12:- Now declare
main function with return type void. Declare N as a variable.
STEP
13:- Now accept the
number from user and store it into ‘N’. Now check if N/2 and remainder then show the message(“INVALID INPUT NUMBER
IS ODD”) exit from the if condition.
STEP
14:- Now check
whether N<=9 or N>=50 if satisfied then print the message(“INVALID INPUT
NUMBER IS OUT OF RANGE") and exit from this condition.
STEP
15:- Create an
object ob of class Goldbach and call the print method through object.ob through
actual parameter. ‘N’ under the message “PRIME PAIRS ARE”.
STEP
16:- Now close the
main function and close class.
STEP
17:- Stop.
SOURCE CODE
import
java.util.*;
class
goldbach{
boolean isprime(int N){
if(N<=1)
return false;
int i;
for(i=2;i<N;i++){
if(N%i==0)
return false;}
return true;}
void print(int N){
int i,j;
for(i=2;i<=N;i++){
for(j=i;j<=N;j++){
if(isprime(i)&&isprime(j)&&i+j==N)
System.out.println(i+","+j);}}}
public static void main(){
Scanner sc=new Scanner(System.in);
int N;
System.out.println("ENTER THE
LIMIT:");
N=sc.nextInt();
if(N%2==1){
System.out.println("INVALID
INPUT,NUMBER IS ODD");
System.exit(0);}
if(N<=9||N>=50){
System.out.println("INVALID
INPUT,NUMBER OUT OF RANGE");
System.exit(0);}
goldbach ob=new goldbach();
System.out.println("PRIME PAIRS
ARE:");
ob.print(N);}}
VARIABLE DESCRIPTION
|
S.L.No. |
Name |
Data Type |
Description |
|
1 |
N |
int |
Use to accept the value from user
and store it into N. |
|
2 |
i,j |
int |
Use as a loop control variables. |
INPUT AND
OUTPUT