The Programming Project

Sunday, August 23, 2020

ISC COMPUTER SCIENCE PRACTICAL SOLVED 2020 QUESTION 1: PRIME-ADAM INTEGERS

Question 1 :

A Prime-Adam integer is a positive integer (without leading zeros) which is a prime as well as an Adam number.Prime number: A number which has only two factors, i.e. 1 and the number itself.                          

Example: 2, 3, 5, 7.. etc.

Adam number: The square of a number and the square of its reverse are reverse to each other.

Example: If n=13 and reverse of "n" =31, then,                       

(13)^2 = 169                       

(31)^2 =961 which is reverse of 169                        

Thus 13, is an Adam number.Accept two positive integers m and n, where m is less than n as user input.Display all Prime-Adam integers that are in the range between m and n (both inclusive) and output them along with the frequency, in the format given below:

Test your program with the following data and some random data:

Example 1

INPUT: m=5    n=100

OUTPUT:

THE PRIME-ADAM INTEGERS ARE:11 13 31                 

FREQUENCY OF PRIME-ADAM INTEGERS IS:3

 

Example 2

INPUT: m=100  n= 200

OUTPUT:

THE PRIME-ADAM INTEGERS ARE:101 103 113            

FREQUENCY OF PRIME-ADAM INTEGERS IS:3

 

Example 3

INPUT:m= 50   n = 70

OUTPUT:

THE PRIME-ADAM INTEGERS ARE:NIL                

FREQUENCY OF PRIME-ADAM INTEGERS IS:0

 

Example 4

INPUT: m=700  n =450

OUTPUT: INVALID INPUT.

 

import java.util.*;

public class ISC2020Q1 {

       public static void main(String[] args) {

          int m,n;

          Scanner in = new Scanner(System.in);

          do {

                 System.out.println("INPUT: Enter the value of m and n:");

                 m = in.nextInt();

                 n= in.nextInt();

                 if(m <=0 || n <= 0 || m > n )

                            System.out.println("Wrong input, please try again:");

          }while ( m <=0 || n <= 0 || m > n);

          

          PrimeAdam obj = new PrimeAdam(m,n); 

          obj.displayPrimeAdam();

          in.close();

      }

}

class PrimeAdam {

      

       public void displayPrimeAdam() {

              System.out.print("THE PRIME ADAM INTEGERS ARE:");

              for (int i=m; i<=n; i++) {

                     if(isPrime(i)==true)

                           if (i*i == reverseNumber(reverseNumber(i)*reverseNumber(i))) {

                                  System.out.print(i+" ");

                                  frequency++;

                           }

              }

              System.out.println();

              if (frequency ==0)

                     System.out.println("NIL");

             

              System.out.println("FREQUENCY OF PRIME-ADAM INTEGER(S) IS:"+frequency);

       }

       private int reverseNumber(int numb) {

              int temp=0;

              int rem,i;

              String s;

              s=Integer.toString(numb);

              i=s.length()-1;

              do {

                     rem = numb%10;

                     temp += rem*(int)Math.pow(10, i);

                     i--;

                     numb /= 10;

              }while( numb > 0);

       return temp;

       }

      

       private boolean isPrime(int numb) {

              PrimeFlag = true;

              for( int i = 2; i <= (int)Math.sqrt(numb); i++) {

                     if (numb%i == 0) {

                           PrimeFlag = false;

                           break;

                     }

              }

              return PrimeFlag;

       }

      

       PrimeAdam (int m, int n) {

              this.m = m;

              this.n = n;

              frequency = 0;

       }

       private int m;

       private int n;

       private int frequency;

       private boolean PrimeFlag;

       }

Sunday, March 31, 2019

ISC COMPUTER SCIENCE PRACTICAL SOLVED 2006 QUESTION 1: CONSECUTIVE INTEGERS

A positive natural number, (for e.g. 27), can be represented as follows –
2+3+4+5+6+7
8+9+10
13+14
where every row represents a combination of consecutive natural numbers, which add up to 27.
Write a program which inputs a positive natural number N and prints the possible consecutive number combinations, which when added give N.
Test your program for the following data and some random data.
SAMPLE DATA
INPUT:
N = 9
OUTPUT:
4 + 5
2 + 3+ 4
INPUT:
 N = 15
OUTPUT:
7 +8
1 +2+ 3+ 4+ 5
4 +5+ 6
INPUT:
N = 21
 OUTPUT:
10+ 11
1+ 2+ 3+ 4+ 5+ 6
6+ 7+ 8
The main logic of the program is as follows:
For every integer i less than half of the input number (since once the half path is crossed the sum will always exceeds the input number), add consutive integers till the sum
is either equal to the input number or exceeds it. If the sum equals the input number print the
consecutive integers starting from i. See the code below
 while(sum !=natural_numb) {
                                  sum +=k;
                                  if(sum>=natural_numb)
                                         break;
                                  k++;
                                  counter++;
                                  }
k++ helps to add consecutive integers to the sum and counter counts the number of times
the addition operation takes place. Rest is reveled by the code itself.





import java.util.*;
public class ISC2006Q1 {
       public static void main(String[] args) {
       int N;
       Scanner in = new Scanner(System.in);
       System.out.println("Enter the natural number:");
       N = in.nextInt();
       IntegerSum obj = new IntegerSum(N);
       System.out.println("OUTPUT:");
       obj.consecutiveIntegerSum();
       in.close();

      }
}
class IntegerSum {
       public void consecutiveIntegerSum() {
              int counter,sum,k;
              flag=false;
              for(int i=1;i<=natural_numb/2;i++) {
                     sum=0;
                     counter=0;
                     k=i;
                     while(sum !=natural_numb) {
                                  sum +=k;
                                  if(sum>=natural_numb)
                                         break;
                                  k++;
                                  counter++;
                                  }
                     if(sum==natural_numb) {
                           flag=true;
                           for(int j=0;j<counter+1;j++) {
                                  if(j==counter)
                                         System.out.print((j+i)+"="+natural_numb);
                                  else
                                         System.out.print((j+i)+"+");
                                  }
                           System.out.println();
                           }
                    
                     }
              if(flag==false)
                     System.out.println(natural_numb+" cannot be written as sum of consecutive natural numbers:");
       }
       IntegerSum(int N){
              natural_numb=N;
       }
       private int natural_numb;
       private boolean flag;
}