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

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

Monday, July 28, 2014

ISC COMPUTER SCIENCE PRACTICAL 2014~ COMPOSITE MAGIC

/*
A Composite Magic number is a positive integer which is composite as well as a magic number.
Composite number : A composite number is a number which has more than 2 factors.
For example : 10
Factors are : 1,2,5,10.
Magic number : A magic number is a number in which the eventual sum of the
digits is equal to 1.
For example:- 28 = 2+ 8 =10 = 1+0=1
Accept 2 positive integers m and n, where m is less than n as user input. Display the number of
composite magic integers that are in the range between m and n (both inclusive) and output them
along with the frequency, in the format specified below.
Test your program with the sample data and some random data:

Example 1:
INPUT:
m=10
n=100
OUTPUT:
THE COMPOSITE MAGIC INTEGERS ARE :
10,28,46,55,64,82,91,100
FREQUENCY OF COMPOSITE MAGIC INTEGERS IS :8

Example 2:INPUT:
m=1200
n=1300
OUTPUT:
THE COMPOSITE MAGIC INTEGERS ARE :
1207,1216,1225,1234,1243,1252,1261,1270,1288
FREQUENCY OF COMPOSITE MAGIC INTEGERS IS :9

Example 3:
INPUT:
m=120
n=99
OUTPUT:
INVALID INPUT
*/

/* ISC COMPUTER SCIENCE PRACTICALS SOLVED, 2014 */
import java.util.*;
public class CompositeMagic {
    public static void main(String[] args) {
        int m,n;
        Scanner in = new Scanner(System.in);
        System.out.println("INPUT THE VALUE OF m & n:");
        m = in.nextInt();
        n = in.nextInt();
        while( m > n || n < 1 || m < 1) {
            System.out.println("INVALID CHOICE:");
            m = in.nextInt();
            n = in.nextInt();
            }
        CheckCompositeMagic cm = new CheckCompositeMagic(m,n);
        System.out.println("THE COMPOSITE MAGIC NUMBERS ARE:");
        cm.checkComposite();
        System.out.println("FREQUENCY OF COMPOSITE MAGIC INTEGERS IS:"+cm.fCount());
        System.out.println();
        }
    }
class CheckCompositeMagic {
    CheckCompositeMagic (int m, int n) {
        M = m; N = n;
        freqCompositeMagic = 0;
        }
    public int fCount() {
        return     freqCompositeMagic;
        }
    public void checkComposite() {
        for ( int i = M; i <= N ;i++) {
            flag = false;
            for ( int j = 2; j <= Math.sqrt(i); j++) {
                if( i%j == 0) {
                    flag = true;
                    break;
                    }
                }
            if(flag == true) {
                 checkMagic(i);
                 if(fg == true)   
                     System.out.println(i);
                 }   
            }
        }    
    private void checkMagic(int mg) {
        fg = false;
        int tmp,rem;
        tmp = mg;
        sum = 0;
        while( tmp > 0) {
            rem = tmp % 10;
            sum += rem;
            tmp = tmp/10;
            }
        if( sum == 1 ) {
            freqCompositeMagic++;
            fg = true;
            }   
        else {
            if(sum < 10)
                fg = false;
            else
                checkMagic(sum);
            }   
           
        }   
    private int M;
    private int N;   
    private int freqCompositeMagic;
    private int sum;
    private boolean flag,fg;
    }   

ISC Computer Science Practical 2014 solved



/*
Question 3.
Write a program to accept a sentence which may be terminated by either ‘.’, ‘?’ or ‘!’ only. Any
other character may be ignored. The words may be separated by more than one blank space and are
in UPPER CASE.
Perform the following tasks:
a) Accept a sentence and reduce all the extra blank space between 2 words to a single blank space.
b) Accept a word from the user which is a part of the sentence along with its position number
and delete the word and display the sentence.

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

Example 1
INPUT: A   MORNING WALK IS A BLESSING FOR   THE    WHOLE DAY.
WORD TO BE DELETED : IS
WORD POSITION IN THE SENTENCE : 4
OUTPUT: A MORNING WALK A BLESSING FOR THE WHOLE DAY.

Example 2
INPUT: AS YOU SOW, SO SO YOU REAP.
WORD TO BE DELETED : SO
WORD POSITION IN THE SENTENCE : 4
OUTPUT: AS YOU SOW, SO YOU REAP.

Example 3
INPUT: STUDY WELL ##.
OUTPUT: INVALID INPUT.
/*

/* ISC COMPUTER SCIENCE PRACTICAL~2014 */
import java.util.*;
import java.io.*;
public class SpaceWord {
    public static void main(String[] args) throws IOException
        {
        String msg;
        String wordDelete;
        int pos;
        Scanner in = new Scanner(System.in);
        System.out.println("WORD POSITION IN THE SENTENCE:");
        pos = in.nextInt();
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));   
        System.out.println(" Enter the sentence:");
        msg = br.readLine();
        br.close();
        OutputString os = new OutputString(msg);
        if(os.validInvalid() == false)
            System.out.println("INVALID INPUT");
        else    {
            os.extraSpaceDeletion();
            os.wordDeletion(pos);
            }
               
        }
    }
class OutputString {
    OutputString(String mg) {
        msg = mg;
        lengthMsg = msg.length();
        outputmsg = "";
        }
    public boolean validInvalid() {
        return (msg.charAt(lengthMsg-1) == '.'  ? true : msg.charAt(lengthMsg-1) == '!' ? true : msg.charAt(lengthMsg-1) == '?' ? true : false);
        }   
    public void extraSpaceDeletion() {
        for ( int i = 0; i < lengthMsg; i++) {
            if(msg.charAt(i) != ' ') {
                outputmsg += msg.charAt(i);
                }
            else {
                outputmsg += msg.charAt(i);
                while(msg.charAt(i) == ' ' && i < lengthMsg)
                    i++;
                i--;   
                }   
            }
        System.out.println();   
        }   
    public void wordDeletion(int pos) {
        int tmp = 0;
        for ( int i = 0; i < outputmsg.length(); i++) {
            if(outputmsg.charAt(i) == ' ')
                tmp++;
            if(tmp == pos-1) {
                while(outputmsg.charAt(i+1) != ' ' && i < outputmsg.length())
                    i++;
                }   
            else
            System.out.print(outputmsg.charAt(i));   
            }
        System.out.println();
        }   
    private String msg;
    private String outputmsg;
    private int lengthMsg;
    }