The Programming Project: 2008
Showing posts with label 2008. Show all posts
Showing posts with label 2008. Show all posts

Sunday, August 3, 2014

ISC COMPUTER SCIENCE PRACTICALS 2008 ~ Matrix Sort and Largest two elements



ISC COMPUTER SCIENCE PRACTICALS 2008 ~ Matrix Sort and Largest two elements

Given a square matrix list [ ] [ ] of order ‘ n ’. The maximum value possible for ‘ n ’
is 20. Input the value for ‘ n ’ and the positive integers in the matrix and perform the
following task:

1. Display the original matrix
2. Print the row and column position of the largest element of the matrix.
3. Print the row and column position of the second largest element of the
matrix.
4. Sort the elements of the rows in the ascending order and display the new
matrix.

Sample data:
INPUT
N = 3
List [] [ ]
5    1    3
7    4    6
9    8    2

OUTPUT

5    1    3
7    4    6
9    8    2

The largest element 9 is in row 3 and column 1
The second largest element 8 is in row 3 and column 2
Sorted list
1    3    5
4    6    7
2    8    9  

import java.util.*;
public class MatrixfirstSecond {
    public static void main(String[] args) {
        int M;
        Scanner in = new Scanner(System.in);
        System.out.println("INPUT THE ORDER OF THE MATRIX:");
        M = in.nextInt();
        while( M > 20 || M < 1 ) {
            System.out.println("OUT OF RANGE, INPUT AGAIN:");
            M = in.nextInt();
            }
        Sorting cm = new Sorting(M);
        cm.inputElements();
        System.out.println();
        cm.displayMatrix();
        System.out.println();   
        cm.maxMin();
        System.out.println();
        cm.rowSort();
        }
    }
class Sorting {
    Sorting(int M) {
        row = M;
        column = M;
        Mat = new int[row][column];
        }
    public void inputElements() {
        System.out.println("Enter the elements of the matrix:");
        Scanner in = new Scanner(System.in);
        for(int i = 0; i < row; i++) {
            for(int j = 0; j < column; j++) {
                System.out.println("Input the element:");
                Mat[i][j] = in.nextInt();
                }
            }
        }   
    public void displayMatrix() {
        System.out.println("ORIGINAL MATRIX:");
        for(int[] i : Mat) {
            for(int j : i) {
                System.out.print(j+"  ");
                }
            System.out.println();
            }
        }   
    public void maxMin() {
        boolean flag = false;
        max = secondMax = Mat[0][0];
        for(int i = 0; i < row; i++) { // largest
            for(int j = 0; j < column; j++) {
                if(Mat[i][j] >= max ) {
                    max = Mat[i][j];
                    maxrowPosition = i+1;
                    maxcolPosition = j+1;
                    }
                }
            }
        for(int i = 0; i < row; i++) { // Second largest
            for(int j = 0; j < column; j++) {
                if(Mat[i][j] >= secondMax && Mat[i][j] < max ) {
                    flag = true;
                    secondMax = Mat[i][j];
                    secondMaxrowPosition = i+1;
                    secondMaxcolPosition = j+1;
                    }
                }
            }   
        if( flag == false) {
            secondMax = max;
            secondMaxrowPosition = maxrowPosition;
            secondMaxcolPosition = maxcolPosition;
            }
        System.out.println("The largest Number: "+max);
        System.out.println("Row: "+maxrowPosition);
        System.out.println("Coloumn: "+maxcolPosition);   
        System.out.println("The second largest Number: "+secondMax);
        System.out.println("Row: "+secondMaxrowPosition);
        System.out.println("Coloumn: "+secondMaxcolPosition);
        }
    public void rowSort() {   
        int[] temp = new int[row];
        int k = 0;
        System.out.println("SORTED LIST:");
        for(int i = 0; i < row; i++) {
            for(int j = 0; j < column; j++) {
                temp[k++] = Mat[i][j];
                }
            k = 0;
            Arrays.sort(temp);
            for( int l = 0; l < row ;l++)
                System.out.print(temp[l]+"  ");    
            System.out.println();
            }   
        }   
    private int[][] Mat;   
    private int row;
    private int column;
    private int max;
    private int secondMax;
    private int maxrowPosition;
    private int maxcolPosition;
    private int secondMaxrowPosition;
    private int secondMaxcolPosition;
    }   

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