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;
}

Saturday, November 22, 2014

ISC Computer Science Practical 2011 Number to Words

 ISC Computer Science Practical 2011

Write a program to input a natural number less than 1000 and display it in words.
Test your program on the sample data and some random data.
Sample input and output of the program.Input: 29
Output: TWENTY NINE
Input: 17001
Output: OUT OF RANGE
Input: 119
Output: ONE HUNDRED AND NINETEEN
Input: 500
Output: FIVE HUNDRED 


import java.util.*;
public class NumberWords {
    public static void main(String[] args) {
        ToWords numbObject = new ToWords();
        Scanner in = new Scanner(System.in);
        while (true) {
            System.out.println("Input:");
            int i = in.nextInt();
            if (i >= 1000) {
                System.out.println("Output: OUT OF RANGE");
                continue;
                }
            numbObject.toWord(i);
            System.out.println("Want to input another number? Y/N:");
            String c = in.next();
            if(c.equals("y") || c.equals("Y"))
                continue;
            else
                break;   
            }
        in.close();  
        }
    }
class ToWords {
    public void toWord(int numb) {
        this.number = numb;
        this.word = "";
        String temp = "";
        temp +=number;
        switch(temp.length()) {
            case 1:
                word = oneTwoDigit[number-1];
                          
            break;
          
            case 2:
                if ( number <= 20)
                    word = oneTwoDigit[number-1];
                  
                else {
                    if ( (temp.charAt(1)-48) == 0 )
                         word += tens[(temp.charAt(0)-48)-1];
                      
                    else   
                         word += tens[(temp.charAt(0)-48)-1]+ " "+oneTwoDigit[(temp.charAt(1)-48)-1];
                      
                    }
            break;
          
            case 3:
                if ( (temp.charAt(1)-48) == 0 && (temp.charAt(2)-48) == 0)
                    word += hundreds[(temp.charAt(0)-48)-1];
                  
                else if ( (temp.charAt(2)-48) == 0 )   
                    word += hundreds[(temp.charAt(0)-48)-1]+" AND "+tens[(temp.charAt(1)-48)-1];
                  
                else {
                    if ( (temp.charAt(1)-48) == 0 )
                        word += hundreds[(temp.charAt(0)-48)-1]+" AND "+oneTwoDigit[(temp.charAt(2)-48)-1];
                      
                    else  
                        if ( (temp.charAt(1)-48) == 1 )
                             word += hundreds[(temp.charAt(0)-48)-1]+" AND "+oneTwoDigit[(temp.charAt(2)-48)-1+10];
                        else  
                            word += hundreds[(temp.charAt(0)-48)-1]+" AND "+tens[(temp.charAt(1)-48)-1]+"-"+oneTwoDigit[(temp.charAt(2)-48)-1];  
                      
                    }
            break;
          
            case 4:
                word = "One Thousand";
              
            break;
            }  
        System.out.println(number+" in words: "+word);
        }  
    private String[] oneTwoDigit =  {"ONE","TWO","THREE","FOUR","FIVE","SIX","SEVEN",
                    "EIGHT","NINE","TEN","ELEVEN","TWELVE",
                    "THIRTEEN","FOURTEEN","FIFTEEN","SIXTEEN",
                    "SEVENTEEN","EIGHTEEN","NINETEEN","TWENTY"};  
                  
    private String[] tens =     {"TEN","TWENTY","THIRTY","FORTY","FIFTY","SIXTY",
                     "SEVENTY","EIGHTY","NINETY"};
              
    private String[] hundreds =     {"ONE HUNDRED","TWO HUNDRED","THREE HUNDRED","FOUR HUNDRED","FIVE HUNDRED",
                         "SIX HUNDRED","SEVEN HUNDRED","EIGHT HUNDRED","NINE HUNDRED"};              
    private int number;
    private String word;  
    }  

Friday, November 21, 2014

ISC COMPUTER SCIENCE PRACTICALS 2009 BOUNDARY ELEMENTS

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 both m and n must be greater than 2 and less than 20.
Allow the user to input positive integers into this matrix. Perform the following tasks on the
matrix:
(a) Sort the elements of the outer row and column elements in ascending order using any
standard sorting technique.
(b) Calculate the sum of the outer row and column elements.
(c) Output the original matrix, rearranged matrix, and only the boundary elements of the
rearranged array with their sum.
Test your program for the following data and some random data.
1. Example :
INPUT : M=3, N=3
1 7 4
8 2 5
6 3 9
OUTPUT :
ORIGINAL MATRIX :
1 7 4
8 2 5
6 3 9
REARRANGED MATRIX :
1 3 4
9 2 5
8 7 6
BOUNDARY ELEMENTS :
1 3 9
8   4
5 7 6
SUM OF OUTER ROW AND OUTER COLUMN = 43

    ISC COMPUTER SCIENCE PRACTICALS 2009 

import java.util.*;
public class OuterRowColumn {
    public static void main(String[] args) {
        int M,N;
        Scanner in = new Scanner(System.in);
        System.out.println("INPUT THE VALUE OF ROW:");
        M = in.nextInt();
        System.out.println("INPUT THE VALUE OF COLUMN:");
        N = in.nextInt();
        while( M > 20 || M < 2 || N > 20 || N < 2) {
            System.out.println("OUT OF RANGE, INPUT AGAIN:");
            M = in.nextInt();
            N = in.nextInt();
            }
        Matrix m = new Matrix(M,N);   
        m.inputMatrix();
        System.out.println("OUTPUT:");
        m.outputMatrix();
        System.out.println();
        m.rearrangedMatrix();
        m.outputMatrix();
        System.out.println();
        m.boundaryElements();
        in.close();
        }
    }   
class Matrix {
    Matrix(int M,int N) {
        row = M;
        col = N;
        Mat = new int[M][N];
        outerRowColSum = 0;
        boundary = new int[2*col+2*(row-2)];
        }
    public void outputMatrix() {
        for(int i = 0; i < row; i++) {
            for(int j = 0; j < col; j++) {
                System.out.print(Mat[i][j]+" ");
                }
            System.out.println();
            }
        }   
    public void rearrangedMatrix() {
        System.out.println("REARRANGED MATRIX :");
        int k = 0;
        for(int i = 0; i < row; i++)
            for(int j = 0; j < col; j++)
                if(i == 0 || j == 0 || i == row-1 || j == col-1)
                    boundary[k++] = Mat[i][j];
        Arrays.sort(boundary);    // sorting the boundary elements
        k = 0;       
        int i=0,j=0;
        // inserting the sorted boundary elements clockwise
        for ( i = 0; i < col; i++)
            Mat[j][i] = boundary[k++];
        for ( i = 1; i < row; i++)
            Mat[i][col-1] = boundary[k++];    
        for ( i = col-2; i >= 0; i--)
            Mat[row-1][i] = boundary[k++];   
        for ( i = row-2; i >= 1; i--)
            Mat[i][0] = boundary[k++];   
        }   
    public void boundaryElements() {
        System.out.println("BOUNDARY ELEMENTS :");
        int k = 0;
        for(int i = 0; i < row; i++) {
            for(int j = 0; j < col; j++) {
                if(i == 0 || j == 0 || i == row-1 || j == col-1) { // extracting the boundary elements
                    System.out.print(Mat[i][j]+" ");
                    boundary[k++] = Mat[i][j];
                    outerRowColSum += Mat[i][j];
                    }   
                else
                    System.out.print(" "+" ");       
                }
            System.out.println();
            }   
        System.out.println("SUM OF OUTER ROW AND OUTER COLUMN = "+outerRowColSum);   
        }   
    public void inputMatrix() {
        Scanner in = new Scanner(System.in);
        for(int i = 0; i < row; i++) {
            for(int j = 0; j < col; j++) {
                System.out.println("Input the element:");
                Mat[i][j] = in.nextInt();
                }
            }
        }   
    private int[] boundary;   
    private int row;   
    private int col;
    private int[][] Mat;
    private int outerRowColSum;
    }       

Tuesday, November 18, 2014

ISC COMPUTER SCIENCE PRACTICAL 2009 DAY NUMBER

Design a program to accept a day number (between 1 and 366), year (in 4 digits) from the user
to generate and display the corresponding date. Also accept 'N' (1<=N<=100) from the user to
compute and display the future date corresponding to 'N' days after the generated date.
Display error message if the value of the day number, year and N are not within the limit or
not according to the condition specified. Test your program for the following data and some
random data.

Example:

INPUT : DAY NUMBER : 233 YEAR : 2008 DATE AFTER(N) : 17
OUTPUT: 20TH AUGUST 2008 DATE AFTER 17 DAYS : 6TH SEPTEMBER 2008
INPUT : DAY NUMBER : 360 YEAR : 2008 DATE AFTER(N) : 45
OUTPUT: 25TH DECEMBER 2008 DATE AFTER 45 DAYS : 8TH FEBRUARY 2009

ISC Computer Science Practical 2009 Java Code

import java.util.*;
public class DayNumber {
    public static void main(String[] args) {
        int dayNumber,year;
        int dayAfter;
        boolean flag;
        Scanner in = new Scanner(System.in);
        DaysCalculation date = new DaysCalculation();
        do {
            System.out.println("INPUT THE DAY NUMBER:");
            dayNumber = in.nextInt();
            System.out.println("INPUT YEAR:");
            year = in.nextInt();
            System.out.println("INPUT THE VALUE OF N:");
            dayAfter = in.nextInt();
            flag = date.checkDate(dayNumber,year,dayAfter);
            if (flag == false)
                System.out.println("INVALID INPUTS:");
            } while(flag != true);
        date.setDate(dayNumber,year,dayAfter);   
        date.dateSpecifier();   
        in.close();
        }   
    }
class DaysCalculation {
    public void setDate(int dayNumber, int year, int dayAfter) {
        this.dayNumber = dayNumber;
        this.year = year;
        this.dayAfter = dayAfter;
        }
    public boolean checkDate(int dayNumber, int year, int dayAfter) {
        if ( dayNumber < 1 || dayNumber > 365 )
            return false;
            else if ( dayAfter < 1 || dayAfter > 100)
                return false;
                else if (String.valueOf(year).length() != 4)
                    return false;
                    else
                    return true;
        }
    public void dateSpecifier() {
        int m = 0,k=1;
        for(int i = 1; i <= dayNumber; i++) {
            if( checkLeap(year) == true ? k > ldays[m] : k > mdays[m] ) {
                k = 1;
                m++;
                }
            k++;   
            }
        String prefix;   
        prefix = (k-1)%10 == 1 ? "st" : (k-1)%10 == 2 ? "nd" : (k-1)%10 == 3 ? "rd" : "th";
        System.out.println(k-1+prefix+" "+months[m]);   
        for (int i = 1; i <= dayAfter; i++) {
            if( checkLeap(year) == true ? k > ldays[m] : k > mdays[m] ) {
                k = 1;
                m++;
                if(m > 11) {
                    year++;
                    m = 0;
                    }
                }
            k++;   
            }
        prefix = (k-1)%10 == 1 ? "st" : (k-1)%10 == 2 ? "nd" : (k-1)%10 == 3 ? "rd" : "th";
        System.out.println("DATE AFTER "+dayAfter+" DAYS:"+(k-1)+""+prefix+" "+months[m]);       
        }   
    private boolean checkLeap(int year) {
        if(year%400==0)
           leap=true;
        else if (year%100==0)
           leap=false;
        else if (year%4==0)
           leap=true;
        else
           leap=false;
           return leap;
        }    
    private boolean flag;
    private static boolean leap;   
    private int dayNumber,year;
    private int dayAfter;
    String[] months = {"January","Feburary","March","April","May","June","July","August","Sepetember","October","November","December"};
    int[] mdays={31,28,31,30,31,30,31,31,30,31,30,31};
    int[] ldays={31,29,31,30,31,30,31,31,30,31,30,31};   
    }