The Programming Project

Monday, March 11, 2019

ISC COMPUTER SCIENCE PRACTICAL 2015 QUESTION 2

/*Write a program to declare a square matrix A[][] of order M x M where ‘M’ is the
number of rows and the number of columns, such that M must be greater than 2 and less
than 10. Accept the value of M as user input. Display an appropriate message for an
invalid input. Allow the user to input integers into the matrix. Perform the following
tasks:
a) Display the original matrix.
b) Rotate the matrix 90ยบ clockwise as shown below:
Original matrix Rotated matrix
1 2 3 7 4 1
4 5 6 8 5 2
7 8 9 9 6 3
c) Find the sum of the elements of the four corners of the matrix. Test your program
for the following data and some random data:
Example 1:
INPUT : M=3
 3 4 9
 2 5 8
 1 6 7
OUTPUT:
ORIGINAL MATRIX
3 4 9
2 5 8
 1 6 7
MATRIX AFTER ROTATION
 1 2 3
 6 5 4
 7 8 9
Sum of the corner elments = 20
Example 2:
INPUT : M=4
 1 2 4 9
 2 5 8 3
 1 6 7 4
 3 7 6 5
OUTPUT:
ORIGINAL MATRIX
 1 2 4 9
 2 5 8 3
 1 6 7 4
 3 7 6 5
MATRIX AFTER ROTATION
 3 1 2 1
 7 6 5 2
 6 7 8 4
 5 4 3 9
Sum of the corner eements= 18
EXAMPLE 3.
INPUT : M = 14
OUTPUT:
 SIZE OUT OF RANGE*/


import java.util.*;
public class ISC2015Q2 {
       public static void main(String[] args) {
             int M;
             Scanner in = new Scanner(System.in);
             do {
                    System.out.println("Enter the order of the matrix");
                    M = in.nextInt();
                    if(M < 2 || M > 10)
            System.out.println("INVALID INPUT, TRY AGAIN:");
             }while(M < 2 || M > 10);
             MatrixRotation Mat_Object = new MatrixRotation(M);
             Mat_Object.MatrixInput();
             Mat_Object.DisplayInputMatrix();
             Mat_Object.RotateMatrix();
             Mat_Object.DisplayRotatedMatrix();
             System.out.println("Sum of corner elements:"+Mat_Object.SumOfCornerElements());
             }
}

class MatrixRotation {

       MatrixRotation(int M) {
             order = M;
             Sum_of_Corner_Elements = 0;
             Mat = new int[order][order];
             Rotated_Mat = new int[order][order];
       }

       public int SumOfCornerElements() {
             return (Rotated_Mat[0][0]+Rotated_Mat[0][order-1]+Rotated_Mat[order-1][0]+Rotated_Mat[order-1][order-1]);
       }

       public void DisplayRotatedMatrix() {
             System.out.println("OUTPUT");
             System.out.println("Rotated matrix:");
             for(int i=0;i<order;i++) {
                    for(int j=0;j<order;j++)
                          System.out.print(Rotated_Mat[i][j]+" ");
                    System.out.println();
             }
       }

       public void RotateMatrix(){
             int k;
             k=order;
             for(int i=0; i<order;i++) {
                    for(int j=0;j<order;j++)
                          Rotated_Mat[j][k-1]=Mat[i][j];
                    k--;
             }
       }
       public void MatrixInput() {
             Scanner in = new Scanner(System.in);
             System.out.println("Enter the elements of the matrix(row-wise)"+order+"X"+order);
             for(int i=0;i<order;i++) {
                    for(int j=0;j<order;j++)
                          Mat[i][j]=in.nextInt();
             }
       }
      
       public void DisplayInputMatrix() {
             System.out.println("INPUT MATRIX:");
             for(int i=0;i<order;i++) {
                    for(int j=0;j<order;j++)
                          System.out.print(Mat[i][j]+" ");
                    System.out.println();
             }
       }
       private int order;
       private int[][] Mat;
       private int[][] Rotated_Mat;
       private int Sum_of_Corner_Elements;
       }

ISC COMPUTER SCIENCE PRACTICAL 2015 Question 1


Program 1: Given two positive numbers M and N, such that M is between 100 and 10000 and N is less than 100. Find the smallest integer that is greater than M and whose digits add up to N. For example, if M = 100 and N = 11, then the smallest integer greater than 100 wose digits add up to 11 is 119.

Write a program to accept the numbers M and N from the user and print the smallest required number whose sum of all its digits is equal to N. Also, print the total number of digits present in the required number. The program should check for the validity of the inputs and display an appropriate message for an invalid input.
Test your program with the sample data and some random data:

Example 1
INPUT :M = 100
N = 11
OUTPUT :
The required number = 119
Total number of digits = 3

Example 2
INPUT :
M = 1500
N = 25
OUTPUT :
The required number = 1699
Total number of digits = 4

Example 3
INPUT :
M = 99
N = 11
OUTPUT :
INVALID INPUT

Example 4
INPUT :
M = 112
N = 130
OUTPUT :
INVALID INPUT

JAVA SOURCE CODE


public class ISC2015Q1 {
       public static void main(String[] args) {
             int M,N;
             Scanner in = new Scanner(System.in);
             while(true) {
                    System.out.println("Enter the value of M:");
                    M=in.nextInt();
                    System.out.println("Enter the value of N:");
                    N=in.nextInt();
                    if( M < 100 || M > 10000 || N >= 100)  {
                          System.out.println("INVALID INPUT:");
                          continue;
                    }
                   
                    else
                          break;

                    }
             in.close();
             SumOfDigitsToN obj = new SumOfDigitsToN();
             System.out.println("The required number ="+obj.SumOfDigitsCalculation(M,N));
             System.out.println("Total number of digits="+obj.TotalNumberOfDigits());
             }
}

class SumOfDigitsToN {
       public int SumOfDigitsCalculation (int M,int N) {
             int sum,tmp,rem,k;
             do {
                    tmp=M;
                    sum=0;
                    k=0;
                    while(tmp!=0) {
                          rem = tmp%10;
                          sum +=rem;
                          tmp /=10;
                          k++;
                          }
                    M++;
             }while(sum!=N);
             numb = M-1;
             total_no_digits = k;
             return (numb);
       }

       public int TotalNumberOfDigits() {
             return (total_no_digits);
       }
       private int total_no_digits;
       private int numb;
}