The Programming Project: Computer Science
Showing posts with label Computer Science. Show all posts
Showing posts with label Computer Science. Show all posts

Monday, March 11, 2019

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, August 2, 2014

ISC COMPUTER SCIENCE PRACTICALS 2008 ~ Vowels & Words Count



ISC COMPUTER SCIENCE PRACTICALS 2008 ~ Vowels & Words Count in Strings
A sentence is terminated by either “ . ” , “ ! ” or “ ? ” followed by a space.
Input a piece of text consisting of sentences. Assume that there will be a
maximum of 10 sentences in a block.
Write a program to:
(i)  Obtain the length of the sentence (measured in words) and the frequency
     of vowels in each sentence.
(ii) Generate the output as shown below using the given data

Sample data:

INPUT
HELLO! HOW ARE YOU? HOPE EVERYTHING IS FINE. BEST OF LUCK.
OUTPUT

Sentence    No. of Vowels    No. of words
----------------------------------------------------------
1        2        1
2        5        3 
3        8        4
4        3        3       

import java.util.*;
import java.io.*;
public class StringVowelcount {
    public static void main(String[] args) throws IOException
        {
        Sorting fc = new Sorting();
        String msg;
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));   
        System.out.println(" Enter the sentence:");
        msg = br.readLine();
        if ( msg.charAt(msg.length()-1) != '.' && msg.charAt(msg.length()-1) != '?' && msg.charAt(msg.length()-1) != '!' ) {
            System.out.println("Paragraph terminated incorrectly, appending '.' at the end");
            msg += '.';   
            }
        Vowel v = new Vowel(msg);
       
        v.senCount();
        v.sentenceExtraction();
        System.out.println("Sentence    No. of Vowels    No. of words");
        System.out.println("--------------------------------------------------------");
        v.vowelWordCount();
        System.out.println("");
        }
    }
class Vowel {
    Vowel(String msg) {
    paragraph  = msg;
    wordCount  = new int[paragraph.length()-1];
    vowelCount = new int[paragraph.length()-1];
    sentenceCount = 0;
    }
    public void senCount() {
        for ( int i = 0; i < paragraph.length(); i++) {
            if( paragraph.charAt(i) == '.' || paragraph.charAt(i) == '?' || paragraph.charAt(i) == '!' )
                sentenceCount++;
            }
        sentences = new String[sentenceCount];
        }
    public void sentenceExtraction() {
        int j = 0;
        System.out.println("The sentences are:");
        for ( int i = 0; i < paragraph.length(); i++) {
            sentences[j] = "";
            while(paragraph.charAt(i) != '.' && paragraph.charAt(i) != '?' && paragraph.charAt(i) != '!') {
                sentences[j] += paragraph.charAt(i);
                i++;
                if ( i == paragraph.length())
                    break;
                }
            sentences[j] += paragraph.charAt(i);   
            System.out.println(sentences[j]);
            j++;
            i++;
            }
         }   
    public void vowelWordCount() {
        String tmp;
        for (int i = 0; i < sentenceCount; i++) {
            wordCount[i] = 1;
            vowelCount[i] = 0;
            tmp = sentences[i].toUpperCase();
            for (int j =0; j < sentences[i].length(); j++) {
                if ( tmp.charAt(j) == ' ')
                    wordCount[i]++;
                if ( tmp.charAt(j) == 'A' || tmp.charAt(j) == 'E' || tmp.charAt(j) == 'I' ||tmp.charAt(j) == 'O' ||tmp.charAt(j) == 'U')   
                    vowelCount[i]++;
                }
            System.out.println(i+1+"        "+vowelCount[i]+"        "+wordCount[i]);   
            }
        System.out.println("");   
        }   
    private int sentenceCount; 
    private int[] wordCount;
    private int[] vowelCount;
    private String paragraph;
    private String[] sentences;   
    }

ISC COMPUTER SCIENCE PRACTICAL 2005 ~ WORD SORT on Length

ISC 2005 Computer Science practical solved paper

Write a program which takes a string (maximum 80 characters) terminated by a full stop.
The words in this string are assumed to be separated by one or more blanks.
Arrange the words of the input string in descending order of their lenghts.
Same length words should be sorted alphabetically.
Each word must start with an uppercase letter and the sentence should be terminated by a full stop.

Test your program for the following data and some random data.

SAMPLE DATA:
INPUT:
This is human resource department.

OUTPUT:
Department Resource Human This Is.

INPUT:
To handle yourself use your head and to handle others use your heart.

OUTPUT:
Yourself Handle Handle Others Heart Head Your Your And Use Use To To. 


import java.util.*;
import java.io.*;
public class LengthSort {
    public static void main(String[] args) throws IOException
        {
        Sorting fc = new Sorting();
        String msg;
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));   
        System.out.println(" Enter the sentence:");
        msg = br.readLine();
        if ( msg.charAt(msg.length()-1) != '.')
            System.out.println("Incorrect format, still we are sorting for you!");
        fc.setMsg(msg);
        System.out.println("OUTPUT:");
        fc.totalWords();
        fc.wordExtraction();
        fc.output();
        System.out.println("");
        }
    }   
class Sorting {
    Sorting()
        {
        msg = " ";
        words = 0;
        }
    public void setMsg(String s) {   
        msg = s;
        len = s.length();
        }
    public void totalWords() {
        for ( int i =0; i<len ; i++)
            if(msg.charAt(i) == ' ')
                words++;
        wrdsCollection = new String[words+1];   
        for(int i = 0; i< words+1 ; i++ )
            wrdsCollection[i]= " ";
        }   
    public void wordExtraction() { 
        int j=0;
        for ( int i =0; i<len ; i++) {
            while(msg.charAt(i) != ' ')
                {
                if(msg.charAt(i) == '.')
                    { i++;
                    if(i>len-1)
                        break;
                    continue;
                    }
                wrdsCollection[j] +=msg.charAt(i);
                i++;
                if(i>len-1)
                    break;
                if((msg.charAt(i) == ' ')|| i >len-1)
                    break;
                }
            if(i>len-1)
                break;   
            j++;
            }
        }   
    public void output() {
    System.out.println("Rearranged Sentence:");
        for(int j = 0; j < wrdsCollection.length; j++) {
            for(int i = j+1; i < wrdsCollection.length; i++) {
                if(wrdsCollection[i].length() >= wrdsCollection[j].length()  ) {
                    if ( wrdsCollection[i].length() == wrdsCollection[j].length()) {
                        if(wrdsCollection[i].compareTo(wrdsCollection[j]) > 0) {
                            String temp = wrdsCollection[j];
                            wrdsCollection[j] = wrdsCollection[i];
                            wrdsCollection[i] = temp;
                            }
                        }
                    String temp = wrdsCollection[j];
                    wrdsCollection[j] = wrdsCollection[i];
                    wrdsCollection[i] = temp;
                    }
                }
            System.out.print(wrdsCollection[j]+" ");
            }   
        }   
    private String[] wrdsCollection;
    private int[] freCount;           
    private int len;
    private String msg;
    private int words;
    private boolean flag;   
    }

ISC COMPUTER SCIENCE PRACTICALS SOLVED ~ 2008 ~ SMITH Number

/*A smith number is a composite number, the sum of whose digits is the sum of
the digits of its prime factors obtained as a result of prime factorization
(excluding 1). The first few such numbers are 4, 22, 27, 58, 85, 94, 121 ...

Example;
1. 666
Prime factors are 2, 3, 3 and 37
Sum of the digits are (6+6+6) = 18
Sum of the digits of the factors (2+3+3+(3+7) = 18

2. 4937775
Prime factors are 3, 5, 5, 65837
Sum of the digits are (4+9+3+7+7+7+5) = 42
Sum of the digits of the factors (3+5+5+(6+5+8+3+7) = 42

Write a program to input a number and display whether the number is a Smith
number or not.
Sample data:

Input         Output
94        SMITH Number

Input        Output
102        NOT SMITH Number

Input        Output
666        SMITH Number

Input        Output
999        NOT SMITH Number

ISC COMPUTER SCIENCE PRACTICALS SOLVED ~ 2008 */

import java.util.*;
public class SmithNumber {
    public static void main(String[] args) {
        int N;
        Scanner in = new Scanner(System.in);
        System.out.println("Enter the number");
        N = in.nextInt();
        SmithCheck sc = new SmithCheck(N);
        if(sc.sumOfdigits(N) == sc.primeFactors())
            System.out.println("SMITH Number");
        else
            System.out.println("Not SMITH Number");   
        }
    }
class SmithCheck {
    SmithCheck(int N) {
        Numb = N;
        primefactorsSum = 0;
        }
    public int sumOfdigits(int n) {
        dSum = 0;
        sNumb = ""+n;
        for ( int i = 0; i < sNumb.length(); i++) {
            dSum += sNumb.charAt(i)-48;
            }
        return dSum;   
        }    
    public int primeFactors() {
        int Ntemp,count;
        for ( int j = 2; j <= Numb; j++) {       
            Ntemp = Numb;
            count = 0;
            if ( SmithCheck.isFactor(j) == true && SmithCheck.isPrime(j) == true ) {
                while ( Ntemp%j == 0) {
                    count++;
                    Ntemp /= j;
                    }
                primefactorsSum += count*sumOfdigits(j);       
                }
            }
        return primefactorsSum;   
        }   
    public static boolean isFactor(int n) {
        return ( Numb % n == 0 ? true : false );
        }   
    public static boolean isPrime(int n) {
        flag = false;
        for ( int j = 2; j <= Math.sqrt(n); j++) {
                if( n%j == 0) {
                    flag = true;
                    break;
                    }
                }
            return (flag == true ? false : true);
        }   
    private static boolean flag;   
    private static int Numb;
    private int dSum;
    private int primefactorsSum;
    private String sNumb;   
    }       

Friday, August 1, 2014

ISC COMPUTER SCIENCE PTRACTCALS ~ 2014 Symmetric Matrix

/*
Write a program to declare a square matrix A[][] of order MxM 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 this matrix. Perform the following tasks:
a) Display the original matrix.
b) Check if the given matrix is symmetric or not. A square matrix is said to be symmetric, if
the element in the i th row and j th column is equal to the element of the j th row and i th
column.
c)
Find the sum of the elements of left diagonal and the sum of the elements of right diagonal
of the matrix and display them.
Test your program for the following data and some random data:
Example 1INPUT:
M= 3
1 2 3
2 4 5
3 5 6
OUTPUT:
ORIGINAL MATRIX
1 2 3
2 4 5
3 5 6
THE GIVEN MATRIX IS SYMMETRIC
The sum of the left diagonal = 11
The sum of the right diagonal = 10
Example 2
INPUT:
M= 4
7 8 9 2
4 5 6 3
8 5 3 1
7 6 4 2
OUTPUT:
ORIGINAL MATRIX
7 8 9 2
4 5 6 3
8 5 3 1
7 6 4 2
THE GIVEN MATRIX IS NOT SYMMETRIC
The sum of the left diagonal = 17
The sum of the right diagonal = 20
Example 3
INPUT:
M= 12
OUTPUT:
THE MATRIX SIZE IS OUT OF RANGE
*/
    /* ISC COMPUTER SCIENCE PRACTICALS SOLVED 2014 ~ Symmetric Matrix */
import java.util.*;
public class SymmetricMatrix {
    public static void main(String[] args) {
        int M;
        Scanner in = new Scanner(System.in);
        System.out.println("INPUT THE VALUE OF M:");
        M = in.nextInt();
        while( M > 10 || M < 2) {
            System.out.println("OUT OF RANGE, INPUT AGAIN:");
            M = in.nextInt();
            }
        Matrix m = new Matrix(M);   
        m.inputMatrix();
        System.out.println("OUTPUT:");
        m.displayMatrix();
        m.diagonalSum();
        in.close();
        }
    }   
class Matrix {
    Matrix(int M) {
        odr = M;
        Mat = new int[M][M];
        leftDsum = 0;
        rightDsum = 0;
        }
    public void inputMatrix() {
        Scanner in = new Scanner(System.in);
        for(int i = 0; i < odr; i++) {
            for(int j = 0; j < odr; j++) {
                System.out.println("Input the element:");
                Mat[i][j] = in.nextInt();
                }
            }
        }   
    public void displayMatrix() {
        flag = true;
        System.out.println("ORIGINAL MATRIX:");
        for(int i = 0; i < odr; i++) {
            for(int j = 0; j < odr; j++) {
                if( Mat[i][j] != Mat[j][i])
                    flag = false;
                if(j == i)
                    leftDsum += Mat[i][j];
                if( i+j == odr-1)
                    rightDsum += Mat[i][j];       
                System.out.print(Mat[i][j]+"  ");
                }
            System.out.println();
            }
        if( flag == true )
            System.out.println("THE GIVEN MATRIX IS SYMMETRIC");
        else
            System.out.println("THE GIVEN MATRIX IS NOT SYMMETRIC");   
        }   
    public void diagonalSum() {
        System.out.println();
        System.out.println("The sum of the left diagonal: "+leftDsum);
        System.out.println();
        System.out.println("The sum of the right diagonal: "+rightDsum);
        System.out.println();   
        }
    private int odr;   
    private int[][] Mat;
    private boolean flag;
    private int leftDsum;
    private int rightDsum;
    }   

ISC COMPUTER SCIENCE PRACTICAL 1998 SPIRAL MATRIX using recursion in JAVA, Python and C++


Spiral Matrix
Write a program which takes N as input, where N is the order of an square matrix. Generate the spiral matrix of order N.
INPUT:
N = 1
OUTPUT:
1

INPUT:
N=2
OUTPUT
1 2
4 3

INPUT:
N=3
OUTPUT
1 2 3
8 9 4
7 6 5

INPUT:
N=4
OUTPUT
1     2    3    4
12  13  14   5
11  16  15   6
10  9    8     7

INPUT:
N=7
OUTPUT
1   2    3   4  5   6   7
24 25 26 27 28 29 8
23 40 41 42 43 30 9
22 39 48 49 44 31 10
21 38 47 46 45 32 11
20 37 36 35 34 33 12
19 18 17 16 15 14 13

Logic: Go through the code!!!!!! All the best!

JAVA CODE

import java.util.*;

public class ISC1998Spiral {
    public static void main(String[] args) {
        int N, val = 0, row, col, ri, ci;
        int i, j;
        int[][] matrix;
        Scanner in = new Scanner(System.in);
        System.out.println("Enter the order of the matrix:");
        N = in.nextInt();
        matrix = new int[N + 1][N + 1];
        for (i = 1; i <= N; i++) {
            for (j = 1; j <= N; j++) {
                matrix[i][j] = 1;
            }
        }
        row = col = 1;
        ri = (N - (N - 2));
        ci = (N - (N - 1));
        SpiralGeneration sp = new SpiralGeneration();
        System.out.println("OUTPUT:");
        sp.spiral(matrix, N, row, col, val, ri, ci);
        for (i = 1; i <= N; i++) {
            for (j = 1; j <= N; j++) {
                String s = String.valueOf(matrix[i][j]);
                while (s.length() < 4) {
                    s += " ";
                }
                System.out.print(s);
            }
            System.out.println();
        }
    }
}

class SpiralGeneration {
    public void spiral(int[][] matrix, int N, int row, int col, int val, int ri, int ci) {
        int a, b, tmp, i, j, l = 0;
        a = row;
        b = col;
        tmp = val + 1;
        if (N == 1 || N == 2) // stopping condition
        {
            if (N == 1) {
                matrix[row][col] = tmp;
            } else {
                matrix[row][col] = tmp;
                matrix[row][col + 1] = tmp + 1;
                matrix[row + 1][col + 1] = tmp + 2;
                matrix[row + 1][col] = tmp + 3;
            }
        } else {
            for (b = 1; b <= N; b++) {
                matrix[row][b - 1 + col] = tmp++;
                if (row != b - 1 + col)
                    matrix[b - 1 + col][row] = (4 * N - 2) - (matrix[row][b - 1 + col] - val) + val;
            }
            b--;
            for (a = 1; a <= N - 1; a++) {
                matrix[a + row][b - 1 + col] = tmp++;
                if (a + row != b - 1 + col)
                    matrix[b - 1 + col][a + row] = (4 * N - 2) - (matrix[a + row][b - 1 + col] - val) + val;
            }
            val = matrix[ri][ci];
            spiral(matrix, N - 2, ++row, ++col, val, ++ri, ++ci);
        }
    }
}




C code

#include<stdio.h>
#include<malloc.h>
void spiral(int **matrix,int N, int row, int col, int val,int ri,int ci);
int main() {
    int N,val=0,row,col,ri,ci;
    int i,j;
    int **matrix;
    printf("\n Enter the order of the matrix:");
    scanf("%d",&N);
    matrix=(int **)malloc((N+1)*sizeof(int*));
        for(i=0;i<=N;i++)
            matrix[i]=(int *)malloc((N+1)*sizeof(double));
    for(i=1;i<=N;i++) {
        for(j=1;j<=N;j++) {
            matrix[i][j]=1;
            }
        }   
    row=col=1;   
    ri = (N-(N-2));
    ci = (N-(N-1));
    spiral(matrix,N,row,col,val,ri,ci);
    printf("\n");
    for(i=1;i<=N;i++) {
        for(j=1;j<=N;j++) {
            if ( i== 1)
            printf("%d          ",matrix[i][j]);
            else
            printf("%d         ",matrix[i][j]);
            }
        printf("\n");
        }
    return 0;
}
void spiral(int **matrix,int N,int row,int col,int val,int ri,int ci) {
    int a,b,tmp,i,j,l=0;
    a = row;
    b = col;
    tmp = val +1;
    if (N== 1 || N==2 ) // stopping condition
        {
            if ( N == 1) {
            matrix[row][col] = tmp;
            }
            else {
            matrix[row][col] = tmp;
            matrix[row][col+1] = tmp+1;
            matrix[row+1][col+1] = tmp+2;
            matrix[row+1][col] = tmp+3;
            }
        }
    else    {       
        for( b = 1 ; b <= N ; b++) {
            matrix[row][b-1+col] = tmp++;
            if( row!=b-1+col )
            matrix[b-1+col][row]  = (4*N-2)-(matrix[row][b-1+col]-val) + val ;
            }
        b--;
        for( a = 1 ; a <= N-1; a++) {
            matrix[a+row][b-1+col] = tmp++;
            if( a+row!=b-1+col)
            matrix[b-1+col][a+row]  = (4*N-2)-(matrix[a+row][b-1+col]-val) +val;
            }
        val = matrix[ri][ci];   
        spiral(matrix,N-2,++row,++col,val,++ri,++ci);   
    }   
}


Python Code

Passing List to function, recursion in python


def SpiralMatrix():
    N = int(input("Enter the order of the Magic Square (odd):"))
    matrix=[[ 0 for row in range(N+2)]for col in range(N+2)]
    for i in range (N+2):
        for j in range (N+2):
            matrix[i][j]=1
    row=col=1  
    val = 0
    ri = (N-(N-2))
    ci = (N-(N-1))  
    def spiral(matrix,N,row,col,val,ri,ci):
        a = row
        b = col
        tmp = val +1
        #print a,b,tmp
        if N== 1 or N==2:
            if N == 1:
                matrix[row][col] = tmp;
            else:
                matrix[row][col] = tmp
                matrix[row][col+1] = tmp+1
                matrix[row+1][col+1] = tmp+2
                matrix[row+1][col] = tmp+3
           
        else:      
            for b in range(N):
                b = b+1
                matrix[row][b-1+col] = tmp
                tmp = tmp+1
                if row != b-1+col:
                    matrix[b-1+col][row]  = (4*N-2)-(matrix[row][b-1+col]-val) + val
               
            for a in range(N-1):
                a = a+1
                matrix[a+row][b-1+col] = tmp
                tmp = tmp+1
                if a+row != b-1+col:
                    matrix[b-1+col][a+row]  = (4*N-2)-(matrix[a+row][b-1+col]-val) +val
            val = matrix[ri][ci]
            row = row+1
            col = col+1
            ri = ri+1
            ci = ci+1    
            spiral(matrix,N-2,row,col,val,ri,ci)
    spiral(matrix,N,row,col,val,ri,ci)      
    print ("OUTPUT~~ SPIRAL MATRIX:")
    for i in range (N):
        for j in range (N):
            print (matrix[i+1][j+1],end =' ')
        print()
SpiralMatrix()