Monday, March 23, 2020

BlueJ Program on Goldbach Number


Details of The Program: CLICK HERE

import java.io.*;
public class Goldbach

{

boolean isPrime(int n)
{

    if(n<=1) 
    return false; 
int i; 
for(i=2;i

{
if(n%i==0)

break; 
}
if(i==n) 
return true; 
else 
return false;

} 
void show(int n)

{
     int i, j; 
 for(i=2;i<=n;i++) 
{

for(j=i;j<=n;j++)
{
if(i+j==n) 
if(isPrime(i) && isPrime(j)) 
System.out.println(i+", "+j);

}
}

} 
public static void main(String args[])
{

Goldbach ob = new Goldbach();

BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); int n; System.out.println("Enter the limit: ");
 n = Integer.parseInt(br.readLine()) 
if(n%2==1) 
System.out.println("INVALID INPUT. NUMBER IS ODD."); 
else if(n<=9||n>=50)
 System.out.println("INVALID INPUT. NUMBER OUT OF RANGE."); 
else

{
System.out.println("Prime Pairs are: "); 
ob.show(n);

}
}

}


Variable Description

Type
Name
Use
BufferedReader
br
Intakes limit from user
int
n
Limit value is stored

Int
i
Loop control variable
int
j
Loop control variable




Algorithm


Step 1: Create BufferedReader object ‘br’ and an int type variable ‘n’ to store the limit

Step 2: Value of ‘n’ is checked for validity and if it is invalid, terminate the program otherwise proceed.

Step 3: Each 2 values whose sum is equal to the limit value are checked for prime or not. If the values are prime then they are displayed otherwise search for next pair is continued.



Back To 2018 Computer Practical Paper: CLICK HERE

No comments:

Post a Comment

Subscribe via email

Enter your email address:

Delivered by FeedBurner