The Programming Project

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



ISC COMPUTER SCIENCE PRACTICAL 2015 QUESTION 3

/* Write a program to accept a sentence which may be terminated by either ‘.’ Or ‘?’ only.
The words are to be separated by a single blank space. Print an error message if the input
does not terminate with ‘.’ Or ‘?’. You can assume that no word in the sentence exceeds
15 characters, so that you get a proper formatted output. Perform the following tasks:
i) Convert the first letter of each word to uppercase.
ii) Find the number of vowels and consonants in each word and display them
with proper heading along with the words.
Test your program with the following inputs:

Example 1
INPUT : Intelligence plus character is education.
OUTPUT:
Intelligence plus character is education.
Word Vowels Consonants
Intelligence 5 7
Plus 1 3
Character 3 6
Is 1 1
Education 5 4
Example 2
INPUT : God is great.
OUTPUT:
God is great
Word Vowels Consonants
God 1 2
Is 1 1
Great 2 3   */



import java.io.*;
public class ISC2015Q3 {
       public static void main(String[] args) throws IOException{
             String Sentence;
             BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
             System.out.println("Enter the sentence:");
             Sentence=br.readLine();
             while(Sentence.charAt(Sentence.length()-1)!= '?' && Sentence.charAt(Sentence.length()-1)!= '.' ) {
                    System.out.println("INVALID INPUT:,TRY AGAIN");
                    Sentence=br.readLine();
             }
             br.close();
             WordExtractor we = new WordExtractor(Sentence);
             we.WordCount();
             we.ExtractWord();
             we.Output();
             }
}
class WordExtractor {
       WordExtractor(String s){
             msg=s;
             Word_Count=0;
             Vowel_Count=0;
             }
       public void Output() {
             System.out.println("WORD           "+"Vowels     "+"Consonants");
             for(int i=0;i<Word_Count;i++) { // Inspecting each word
                    char[] ch = Word_Storage[i].toCharArray(); // using library function- converting string to char array
                    if (ch[0] >= 'a' && ch[0] <= 'z')  
                          ch[0] = (char)(ch[0] - 'a' + 'A'); //First letter of each word to upper case
                    for(int j=0; j<Word_Storage[i].length();j++) { // counting vowels in individual word
                          for(int l=0; l<9;l++)
                                 if(Word_Storage[i].charAt(j) == vowels[l])
                                       Vowel_Count++;
                                 }
                    Word_Storage[i] = new String(ch);
                    String temp=Word_Storage[i];
                    for(int t=0;t<15-Word_Storage[i].length();t++)  // just for proper formatted output
                    temp +=" ";
                    System.out.println(temp+"     "+Vowel_Count+"          "+(Word_Storage[i].length()-Vowel_Count));
                    Vowel_Count=0;
             }
       }
       public void ExtractWord() {
             int k = 0;
             for(int i=0;i<msg.length();i++) {
                    Word_Storage[k]="";
                    while(msg.charAt(i) != ' ' && msg.charAt(i) != '?' && msg.charAt(i) != '.') {
                          Word_Storage[k] +=msg.charAt(i);
                          i++;
                          }
                    System.out.println();
                    k++;
             }
       }

       public void WordCount() {
             for(int i=0;i<msg.length();i++) {
                    if(msg.charAt(i)==' ')
                          Word_Count++;
                          }
              Word_Count +=1;
              Word_Storage = new String[Word_Count];
       }
       private char[] vowels = {'a','e','i','o','u','A','E','I','O','u'};
       private String msg;
       private String[] Word_Storage;
       private int Word_Count;
       private int Vowel_Count;
}