0% found this document useful (0 votes)
5 views5 pages

Program Two

The document describes a program that identifies all odd prime pairs that sum to a given even integer 'N' within the range of 10 to 50. It includes examples of valid inputs and outputs, as well as error messages for invalid inputs. The algorithm and source code for the program are provided, detailing the steps to check for evenness, range, and primality of numbers.

Uploaded by

aditish59
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views5 pages

Program Two

The document describes a program that identifies all odd prime pairs that sum to a given even integer 'N' within the range of 10 to 50. It includes examples of valid inputs and outputs, as well as error messages for invalid inputs. The algorithm and source code for the program are provided, detailing the steps to check for evenness, range, and primality of numbers.

Uploaded by

aditish59
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Program Two

A Goldbach number is a positive even integer that an be expressed as the sum


of two odd primes. Note: All even numbers greater than 4 are Goldbach
numbers.
Example: 6 = 3+3
10 = 7+3
10 = 5+5
Hence, 6 has one odd prime pair 3 and 3 similarly. Similarly, 10 has two odd
prime pairs, 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 the odd prome pairs whose sum is equal to the
number ‘N.’Test your program with the following data and some random data.
Example 1: Input: n=14
Output: Prime pairs are:
3, 11
7, 7
11,3
Example 2: Input: n=30
Output: Prime pairs are:
7, 23
11, 19
13, 17
Example 3: Input: n=17
Output: Invalid Input, Number is Odd
Example 4: Input: n=17
Output: Invalid Input, Number out of range
Algorithm

1. START!

2. Creating a Scanner object, enter a number of user’s choice.


3. Create an object of the class.
4. Check if the number is even. If true, go to next step, else go to step 12.
5. Check if the number is less 50 but more than 9. If true, go to next step,
else go to step 11.
6. Print the message of output
7. Run a loop from 3 to the number.
8. Check if the current loop variable is prime.
9. Check if the number which is obtained on subtracting the original
number by the current loop variable is prime.
10. If the condition is satisfied, print the pair primes.
11. Print the message of “Invalid Input, Number is out of range,” and use
the exit function to terminate the program
12. Print the message “Invalid Input, Number is Odd.”
13. Create a function prime of boolean type with an integer parameter.
14. Create and initialize a counter variable c as zero.
15. Run a loop from one to the integer parameter, and check if the current
loop divides the integer paramenter. If so, go to next step.
16. Increament variable c by one.
17. Check if the counter variable c is equals to 2. If so, return true.
18. If the counter variable c is not equals to 2. If so, return false.

19. END!

Source Code
import java.util.*; //importing utility packages
public class ProgramTwo { //start of class
public static void main(String[] args) { //start of main
Scanner sc = new Scanner (System.in);
System.out.print("Input: N=");
int N = sc.nextInt();
ProgramTwo ob = new ProgramTwo();
if(N%2==0)
{ //check if the number is even
if(N>9 && N<50)
{ //checking if its within range
System.out.println("Output: Prime Pairs are:");
for(int i=3; i<=N; i++)
{ //finding all prime pairs
if(ob.prime(i))
{
if(ob.prime(N-i))
{
System.out.println("\t" + i + "," + (N-i));
}
}
}
}
else
{
System.out.println("Output: Invalid Input, Number out of range");
System.exit(0);
}
}
else System.out.println("Output: Invalid Input, Number is Odd");
} //end of main
boolean prime(int x)
{ //start of function to check if the number is prime
int c=0;
for(int i=1; i<=x; i++)
{
if(x%i==0)c++;
}
if(c==2) return true;
else return false;
} //end of function
} //end of class

Variable Description Table


Variable Data Type Description
N Int Stores the user’s input
I int It is a loop variable
C Int It is a counter variable

Output

You might also like