The Programming Project

Monday, August 24, 2020

ISC COMPUTER SCIENCE PRACTICAL SOLVED 2020 QUESTION 2: Matrix-Octal

 

Question 2:

Write a program to declare a matrix A[][]of order ( M*N) where 'M' is the number of rows

 and 'N' is the number of columns such that the value of "M' must be greater than 0 and 

 less than  10 and the value of 'N' must be greater than 2 and less than 6. Allow the 

user to input digits (0-7) only at each location, such that each row represents an octal 

number.

 

Example:

 

231(decimal equivalent of 1st row=153 i.e. 2*8² +3*8¹ +1*8⁰)   

405(decimal equivalent of 1st row=261 i.e.4*8¹+0*8¹+5*8⁰ )

156(decimal equivalent of 1st row=110 i.e. 1*8²+5*8¹+6*8⁰)

 

Perform the following tasks on the matrix:

 

Display the original matrix.

Calculate the decimal equivalent for each row and display as per the format given below.

 

Example 1:

Input:

ENTER ROW SIZE M=3          

ENTER COLUMN SIZE  N=4           

ENTER ELEMENTS FOR ROW 1: 1 1 3 7          

ENTER ELEMENTS FOR ROW 2: 2 1 0 6           

ENTER ELEMENTS FOR ROW 3: 0 2 4 5

Output:           

FILLED MATRIX   DECIMAL EQUIVALENT                         

1 1 3 7       607                            

2 1 0 6         1094                            

0 2 4 5         165

 

Example 2:

Input:

ENTER ROW SIZE M=3         

ENTER COLUMN SIZE N=3           

ENTER ELEMENTS FOR ROW 1: 0 2 8

Output:

INVALID INPUT

 

Example 3:

Input:

ENTER ROW SIZE  M=3          

ENTER COLUMN SIZE N=9

Output:OUT OF RANGE




import java.util.*;

public class ISC2020Q2 {

       public static void main(String[] args) {

       int M,N;

       System.out.println("Input:");

       Scanner in = new Scanner(System.in);

       do {

             System.out.println("ENTER ROW SIZE:");

             M = in.nextInt();

             System.out.println("ENTER COLUMN SIZE:");

             N = in.nextInt();

             if(M<0 || M>9 || N <3 || N > 5)

                    System.out.println("OUT OF RANGE, TRY AGAIN:");

             }while(M<0 || M>9 || N <3 || N > 5);

       Matrix obj = new Matrix(M,N);

       obj.inputElements();

       System.out.println("Filled Matrix       Decimal Equivalent");

       obj.display();

       in.close();

 

      }

}

class Matrix {

       private void convertsRowToOctalEquivalent() {

              int temp;

              for(int i=0;i<M;i++) {

                     rowOctalEquivalent[i]=0;

                     temp=N-1;

            for(int j=0;j<N;j++) {

              rowOctalEquivalent[i] += mat[i][j]*(int)Math.pow(8,temp);

              temp--;

              }

            }

             

       }

   

      public void display() {

            for(int p=0;p<M;p++)

                   for(int q=0;q<N;q++)

                         if(MAX_MATRIX < mat[p][q])

                                MAX_MATRIX = mat[p][q];

            String s,element;

            s=Integer.toString(MAX_MATRIX);

            for(int i=0;i<M;i++) {

                   for(int j=0;j<N;j++) {

                          element=Integer.toString(mat[i][j]); // for formatted output

                         int tmp=element.length();

                         while(tmp !=s.length()) {

                                       element +=" ";

                                       tmp++;

                                       }

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

                         }

                 System.out.print("       "+rowOctalEquivalent[i]); 

                 System.out.println();

            }

           

      }

      public void inputElements() {

             Scanner in = new Scanner(System.in);

            System.out.println("Enter the values of the matrix:");

            for(int i=0;i<M;i++) {

                     System.out.println("Enter the elements of row :"+(i+1));

                   for(int j=0;j<N;j++) {

                         mat[i][j]=in.nextInt();

                         if(mat[i][j]<0 || mat[i][j] > 7) {

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

                                j--;   // Or you can simply break, as asked in the question

                          }

                   }

            }

            in.close();

            convertsRowToOctalEquivalent(); // Calculating the octal equivalent for each row

      }

      Matrix(int M, int N){

            this.M = M;

            this.N = N;

            mat = new int[M][N];

            rowOctalEquivalent = new int[M];

            MAX_MATRIX =0;

             }

       private int[][] mat;

       private int M,N;

       private int MAX_MATRIX;

       private int[] rowOctalEquivalent;

}

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;

       }