The Programming Project

Thursday, March 28, 2019

ISC COMPUTER SCIENCE PRACTICAL 2004 SOLVED QUESTION 2 : BASE EQUALITY

 Numbers have different representations depending on the bases on which they are expressed. For example in base 3, the number 12 is written as 110 (1 x 32 + 1 x 31 + 0 x 30), but in base 8 it is written as 14(1 x 81 + 4 x 80).
Consider, for example, the integers 12 and 5. Certainly these are not equal if base 10 is used for each. But suppose 12 was a base 3 number and 5 was a base 6 number then what happens, 12 base 3 = 1 x 31 + 2 x 30, or 5 base 6 or 5 base 10 (5 in any base is equal to 5 base 10). So 12 and 5 can be equal if you select the right bases for each of them.
Write a program to input two integers, X and Y, and calculate the smallest base for X and smallest base for Y (likely different from X) so that X and Y represent the same value. The base associated with X and Y will be between 1 and 20 (both inclusive). In representing these numbers the digits 0 to 9 have their usual decimal interpretations.
Test your program for the following data and some random data:
The logic is quite simple just imput two integers, starting from base 1 find its value and compare it
with the value of the second number from bases 1 to 20 (minus the base of first number). 
Look for equality and print!


import java.util.*;
public class ISC2004Q2 {
       public static void main(String[] args) {
       int X,Y;
       Scanner in = new Scanner(System.in);
       System.out.println("Enter the value of X:");
       X = in.nextInt();
       System.out.println("Enter the value of Y:");
       Y = in.nextInt();
       Base obj = new Base(X,Y);
       obj.display();
       in.close();
       }
}
class Base {
       public void display() {
             for(int i=1;i<=20;i++) {
                    for(int j=1;j<=20;j++) {
                           if(baseRepresentation(X,i)==baseRepresentation(Y,j) && i!=j) {
                                 flag=true;
                                 System.out.println(X+" in base "+i+" equals "+Y+" in base "+j);
                                 break;
                          }
                          if(flag==true)
                                 break;
                    }
             }
             if(!flag)
                    System.out.println("NEVER EQUAL IN ANY BASE LESS THAN 20");
       }
       private int baseRepresentation(int numb,int b) {
             int tmp=0,j=0,N=numb;
             do {
                    tmp=tmp+(N%10)*(int)Math.pow(b, j);
                    j++;
                    N /=10;
                    }while(N>=1);
             return tmp;
       }
       Base(int X,int Y){
             this.X=X;
             this.Y=Y;
             flag=false;
       }
       private boolean flag;
       private int X;
       private int Y;
}



Wednesday, March 20, 2019

ISC COMPUTER SCIENCE PRACTICAL 2020 SPECIMEN PAPER SOLVED QUESTION 3: MATRIX


Write a program to declare a square matrix M [ ] [ ] of order ‘N’ where ‘N’ must be greater than 3 and less than 10. Allow the user to accept three different characters from the keyboard and fill the array according to the instruction given below:

(i)                  Fill the four corners of the square matrix by character 1.
(ii)                Fill the boundary elements of the matrix (except the four corners) by character 2.
(iii)               Fill the non-boundary elements of the matrix by character 3. Test your program with the following data and some random data:

Example 1:
 INPUT: N = 4
 FIRST CHARACTER: @
SECOND CHARACTER: ?
THIRD CHARACTER: #
OUTPUT:
@  ?  ?  @
?   #   #  ?
?   #   #  ?
@ ?  ?  @

Example 2:
INPUT:
 N = 5
FIRST CHARACTER: A
SECOND CHARACTER: C
THIRD CHARACTER: X
OUTPUT:
A C C C A
C X X X C
C X X X C
C X X X C
A C C C A

The logic is quite simple, first fill all the elements of the matrix by the second charcter.
then overwrite the elements with row position raging from 1 to less than N-1 and coloumn
position ranging from 1 to less than N-1 with the third character.
Finally overwrite the corner elemets with the first charcter.
      for(i=0;i<order;i++) 
            for(j=0;j<order;j++)
                mat[i][j]=characters[1]; //filling all the elements with SECOND char
      for(i=1;i<order-1;i++) //filling the non-boundary elements with THIRD char
            for(j=1;j<order-1;j++)
                 mat[i][j]=characters[2];
      mat[0][0]=mat[0][order-1]=mat[order-1][0]=mat[order-1][order-1]=characters[0]; // filling the corner elements



import java.util.*;
public class ISC2020Q1 {
       public static void main(String[] args) {
       int N;
       Scanner in = new Scanner(System.in);
       do {
             System.out.println("Enter the order of the square matrix:");
             N = in.nextInt();
             if(N>10 || N <3)
                    System.out.println("OUT OF RANGE,TRY AGAIN");
             }while(N>10 || N <3);
       MatrixPattern obj = new MatrixPattern(N);
       obj.inputCharacters();
       obj.inputElementsInMatrix();
       obj.display();
       in.close();
       }
}
class MatrixPattern {
       public void inputElementsInMatrix() {
             int i,j;
             for(i=0;i<order;i++) 
            for(j=0;j<order;j++)
            mat[i][j]=characters[1];
             for(i=1;i<order-1;i++) //filling the non-boundary elements
            for(j=1;j<order-1;j++)
                 mat[i][j]=characters[2];
             mat[0][0]=mat[0][order-1]=mat[order-1][0]=mat[order-1][order-1]=characters[0]; // filling the corner elements
             for(i=0;i<3;i++)
                    System.out.println(disp[i]+" CHARACTER IS:"+characters[i]);
       }
       public void inputCharacters() {
             Scanner in = new Scanner(System.in);
             System.out.println("Enter thecharacters");
             s=in.nextLine();
             characters =s.toCharArray();
             in.close();
       }
       public void display() {
                for(int i=0;i<order;i++) {
                    for(int j=0;j<order;j++)
                          System.out.print(mat[i][j]+"  ");  
                 System.out.println();
                }
       }
       MatrixPattern(int N){
             order = N;
             mat = new char[order][order];
             }
       private String s;
       private String[] disp = {"FIRST","SECOND","THIRD"};
       private char[] characters;
       private char[][] mat;
       private int order;
}