Python,C,C++ and JAVA programs for CBSE, ISC, B.Tech and I.T Computer Science and MCA students

The Programming Project: ISC COMPUTER SCIENCE PRACTICAL 2018 QUESTION 1

Tuesday, March 12, 2019

ISC COMPUTER SCIENCE PRACTICAL 2018 QUESTION 1

QUESTION -1
A Goldbach number is a positive even integer that can be expressed as the sum of two odd primes.
Note: All even integer numbers greater than 4 are Goldbach numbers. Example: 6 = 3 + 3
10 = 3 + 7
10 = 5 + 5
Hence, 6 has one odd prime pair 3 and 3. 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 prime 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
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 = 126
OUTPUT: INVALID INPUT. NUMBER OUT OF RANGE.

JAVA SOURCE CODE


import java.util.*;
public class ISC2018Q1 {
       public static void main(String[] args) {
             int N;
             Scanner in = new Scanner(System.in);
             System.out.println("Enter the value of N:");
             N = in.nextInt();
             while( N < 9 || N >= 50 || N%2 !=0) {
                    if(N%2 !=0)
                          System.out.println("INVALID INPUT. NUMBER IS ODD.");
                    else
                          System.out.println("INVALID INPUT. NUMBER OUT OF RANGE.");
                    System.out.println("Enter the value of N:");
                    N = in.nextInt();  
             }
             Goldbach obj = new Goldbach(N);
             System.out.println("OUTPUT: PRIME PAIRS ARE");
             obj.PrimePairGenerator();
             in.close();
             }
}

class Goldbach{
public void PrimePairGenerator() {
       for (int i=3; i<Numb;i++) {
             for(int j=3; j<Numb/2+1;j++) {
                    if(i+j == Numb) // if i+j equals N then we check whether both i and j are prime or not
                          if(IsPrime(i))
                                 if(IsPrime(j))
                                       System.out.println(i+","+j);
                    } // end of inner-for
             } // end of outer-for
       }

private boolean IsPrime(int N) {
       boolean flag=false;
       for(int x=0; x<Prime_Numbers.length;x++) {
             if(N==Prime_Numbers[x])
                    flag=true;
             }
       return(flag);
       }
Goldbach(int N){
       Numb=N;
       }
       private int Numb; // since the range of N is too small no need to write down the prime function
       private int[] Prime_Numbers = {3,5,7,11,13,17,19,23,29,31,37,41,43,47};
}           // this is will save time! 2 is not included as only odd pair primes are needed



2 comments: