ISC PRACTICAL PROGRAM

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
/**
* The class sortParagraph inputs a paragraph and arranges the
* sentences in ascending order of their number of words
* @author : agarwalrajhans@gmail.com
* @Program Type : BlueJ Program - Java
*/

import java.util.*;
class sortParagraph
{
    // Function to count no. of words in every sentence
    int countWords(String s)
    {
        StringTokenizer str = new StringTokenizer(s," .,?!");
        int c = str.countTokens();
        return c;
    }
   
    // Function to sort the sentences in ascending order of their no. of words
    void sort(String w[], int p[])
    {
        int n = w.length, t1 = 0;
        String t2 = "";
       
        for(int i=0; i<n-1; i++)
        {
            for(int j=i+1; j<n; j++)
            {
                if(p[i]>p[j]) // for descending use p[i]<p[j]
                {
                    t1 = p[i];
                    p[i] = p[j];
                    p[j] = t1;
                    t2 = w[i];
                    w[i] = w[j];
                    w[j] = t2;
                }
            }
        }
        printResult(w,p);    // Calling function for printing the result
    }
   
    void printResult(String w[], int p[]) // Function to print the final result
    {
        int n = w.length;
        for(int i=0; i<n; i++)
        {
            System.out.println(w[i]+"\t=\t"+p[i]);
        }
    }
   
    public static void main(String args[])
    {
        sortParagraph ob = new sortParagraph();
        Scanner sc = new Scanner(System.in);
       
        System.out.print("Enter a paragraph : "); //Inputting a paragraph
        String pg = sc.nextLine();
       
        StringTokenizer str = new StringTokenizer(pg,".?!");
        int count = str.countTokens(); //Counting no. of sentences in the paragraph
        if(count > 10)
            System.out.println("A maximum of 10 sentences are allowed in the paragraph");
        else
        {
            String sent[] = new String[count]; //Array to store the sentences separately
            int p[] = new int[count]; //Array to store no. of words of each sentence
           
            for(int i=0; i<count; i++)
            {
                sent[i] = str.nextToken().trim(); // Saving sentences one by one in an array
                p[i] = ob.countWords(sent[i]); // Saving no. of words of every sentence
            }
            ob.sort(sent,p);
        }
    }
}

Output:
Enter a paragraph : Please come and attend the party. Hello! How are you?
OUTPUT :
Hello = 1
How are you = 3
Please come and attend the party = 6


Question:
Write a Program in Java to input a number and check whether it is a Bouncy Number or not.
Increasing Number : Working from left-to-right if no digit is exceeded by the digit to its left it is called an increasing number; for example, 22344.
Decreasing Number : Similarly if no digit is exceeded by the digit to its right it is called a decreasing number; for example, 774410.
Bouncy Number : We shall call a positive integer that is neither increasing nor decreasing a bouncy number; for example, 155349. Clearly there cannot be any bouncy numbers below 100.
Solution:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
/**
* The class BouncyNumber inputs a number and checks whether it is a Bouncy Number or not
* @author : agarwalrajhans@gmail.com
* @Program Type : BlueJ Program - Java
*/

import java.util.*;
class BouncyNumber
{
    boolean isIncreasing(int n) //Function to check whether a number is Increasing
    {
        String s = Integer.toString(n);
        char ch;
        int f = 0;
        for(int i=0; i<s.length()-1; i++)
        {
            ch = s.charAt(i);
            if(ch>s.charAt(i+1))// If any digit is more than next digit then we have to stop checking
            {
                f = 1;
                break;
            }
        }
        if(f==1)
            return false;
        else
            return true;
    }
   
    boolean isDecreasing(int n) //Function to check whether a number is Decreasing
    {
        String s = Integer.toString(n);
        char ch;
        int f = 0;
        for(int i=0; i<s.length()-1; i++)
        {
            ch = s.charAt(i);
            if(ch<s.charAt(i+1))// If any digit is less than next digit then we have to stop checking
            {
                f = 1;
                break;
            }
        }
        if(f==1)
            return false;
        else
            return true;
    }
   
    void isBouncy(int n)
    {
        if(isIncreasing(n)==true)
            System.out.println("The number " + n + " is Increasing and Not Bouncy");
        else if(isDecreasing(n)==true)
            System.out.println("The number " + n + " is Decreasing and Not Bouncy");
        else
            System.out.println("The number " + n + " is bouncy");
    }
   
    public static void main(String args[])
    {
        Scanner sc = new Scanner(System.in);
        BouncyNumber ob = new BouncyNumber();
        System.out.print("Enter a number : ");
        int n = sc.nextInt();
        ob.isBouncy(n);
    }
}

Output:
Enter a number : 22344
The number 22344 is Increasing and Not Bouncy
Enter a number : 774410
The number 774410 is Decreasing and Not Bouncy
Enter a number : 155349
The number 155349 is bouncy


Question:
Given a square matrix M [ ] [ ] of order n. The maximum value possible for n is 10. Accept three different characters from the keyboard and fill the array according to the instruction given below.
Fill the upper and lower elements formed by the intersection of the diagonals by character 1.
Fill the left and right elements formed by the intersection of the diagonals by character 2.
Fill both the diagonals by character 3.
Output the result in format given below:
Example 1
ENTER SIZE : 4
INPUT : FIRST CHARACTER : *
SECOND CHARACTER : ?
THIRD CHARACTER : #
OUTPUT :
# * * #
? # # ?
? # # ?
# * * #

Example 2
ENTER SIZE : 5
INPUT : FIRST CHARACTER : $
SECOND CHARACTER : !
THIRD CHARACTER : @
OUTPUT :
@ $ $ $ @
! @ $ @ !
! ! @ ! !
! @ $ @ !
@ $ $ $ @

Example 3
ENTER SIZE : 65
OUTPUT : SIZE OUT OF RANGE
Programming Code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
/**
* The class MatrixFill creates a matrix using 3 characters taken as inputs
* Upper and lower elements formed by the intersection of the diagonals are filled by character 1.
* Left and right elements formed by the intersection of the diagonals are filled by character 2.
* Both the diagonals are filled by character 3.
* @author : agarwalrajhans@gmail.com
* @Program Type : BlueJ Program - Java
* @ISC Computer Science Practical Specimen Paper - Question 2
*/

import java.util.*;
class MatrixFill
{
    public static void main(String args[])
    {
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter size of the matrix : ");
        int n = sc.nextInt();
       
        if(n<2 || n>10)
            System.out.println("Size out of Range");
        else
        {
            char A[][]=new char[n][n];
            System.out.print("Enter the 1st character : ");
            char c1 = sc.next().charAt(0);
            System.out.print("Enter the 2nd character : ");
            char c2 = sc.next().charAt(0);
            System.out.print("Enter the 3rd character : ");
            char c3 = sc.next().charAt(0);
           
            for(int i=0; i<n; i++)
            {
                for(int j=0; j<n; j++)
                {
                    if(i==j || (i+j)==(n-1))
                        A[i][j] = c3; // Filling the diagonals with 3rd character
                    else
                        A[i][j] = c2; // Filling all other positions with 2nd character
                }
            }
           
            for(int i=0; i<n/2; i++)
            {
                for(int j=i+1; j<n-1-i; j++)
                {
                    A[i][j] = c1; // Filling the upper positions formed by intersection of diagonals
                    A[n-1-i][j] = c1; // Filling the lower positions formed by intersection of diagonals
                }
            }
           
            // Printing the Matrix
            System.out.println("\nOutput : \n");
            for(int i=0; i<n; i++)
            {
                for(int j=0; j<n; j++)
                {
                    System.out.print(A[i][j]+" ");
                }
                System.out.println();
            }
        }
    }
}

Output:
Enter size of the matrix : 7
Enter the 1st character : @
Enter the 2nd character : #
Enter the 3rd character : %

Output :

% @ @ @ @ @ %
# % @ @ @ % #
# # % @ % # #
# # # % # # #
# # % @ % # #
# % @ @ @ % #
% @ @ @ @ @ %





Question:
The encryption of alphabets are to be done as follows:
A = 1
B = 2
C = 3
.
.
.
Z = 26
The potential of a word is found by adding the encrypted value of the alphabets.
Example: KITE
Potential = 11 + 9 + 20 + 5 = 45
Accept a sentence which is terminated by either  .  ,  ?  or  ! . Each word of sentence is separated by single space. Decode the words according to their potential and arrange them in ascending order.
Output the result in format given below:
Example 1
INPUT       :   THE SKY IS THE LIMIT.

POTENTIAL   :   THE     = 33
                SKY     = 55
                IS      = 28
                THE     = 33
                LIMIT   = 63

OUTPUT      :   IS THE THE SKY LIMIT

Example 2
INPUT       :   LOOK BEFORE YOU LEAP.

POTENTIAL   :   LOOK    = 53
                BEFORE  = 51
                YOU     = 61
                LEAP    = 34

OUTPUT      :   LEAP BEFORE LOOK YOU

Programming Code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
/**
* The class WordPotential inputs a sentence and arranges the words
* in ascending order of their potential
* @author : agarwalrajhans@gmail.com
* @Program Type : BlueJ Program - Java
* @ISC Computer Science Practical Specimen Paper - Question 2
*/

import java.util.*;
class WordPotential
{
    int findPotential(String s) // Function to find potential of a word
    {
        s = s.toUpperCase();
        int p = 0, l = s.length();
        char ch;
        for(int i=0; i<l; i++)
        {
            ch = s.charAt(i);
            p = p + (ch-64); // if ch = 'A', then 'A'-64 = ASCII value of 'A' - 64 = 65-64 = 1
        }
        return p;
    }
   
    // Function to sort the words in ascending order of their potential
    void sortPotential(String w[], int p[])
    {
        int n = w.length, t1 = 0;
        String t2 = "";
       
        for(int i=0; i<n-1; i++)
        {
            for(int j=i+1; j<n; j++)
            {
                if(p[i]>p[j])
                {
                    t1 = p[i];
                    p[i] = p[j];
                    p[j] = t1;
                    t2 = w[i];
                    w[i] = w[j];
                    w[j] = t2;
                }
            }
        }
       
        printResult(w,p);  
    }
   
    void printResult(String w[], int p[]) // Function to print the final result
    {
        int n = w.length;
        String ans = "";
        for(int i=0; i<n; i++)
        {
            ans = ans + " " + w[i];
        }
        ans = ans.trim();
        System.out.println("\nOutput\t\t :  \t"+ans);
    }
   
    public static void main(String args[])
    {
        WordPotential ob = new WordPotential();
        Scanner sc = new Scanner(System.in);
       
        System.out.print("Enter a sentence : \t");
        String s = sc.nextLine();
       
        StringTokenizer str = new StringTokenizer(s," .,?!");
        int n = str.countTokens();
       
        String words[] = new String[n];
        int potential[] = new int[n];
       
        for(int i=0; i<n; i++)
        {
            words[i] = str.nextToken(); // Saving words one by one in an array
            potential[i] = ob.findPotential(words[i]); // Saving potential of every word
        }
       
        // Printing the words along with their potential
        System.out.print("\nPotential\t : \t");
        for(int i=0; i<n; i++)
        {
            System.out.println(words[i]+"\t= "+potential[i]);
            System.out.print("\t\t\t");
        }
       
        ob.sortPotential(words,potential);
    }
}

Output:
Enter a sentence :  Look before you leap.

Potential        :  Look    = 53
                    before  = 51
                    you = 61
                    leap    = 34
           
Output       :      leap before Look you


Enter a sentence :  The sky is the limit.

Potential        :  The = 33
                    sky = 55
                    is  = 28
                    the = 33
                    limit   = 63
           
Output       :      is The the sky limit




Question:
Write a Program in Java to input a number and check whether it is an Evil Number or not.
Evil Number : An Evil number is a positive whole number which has even number of 1s in its binary equivalent.
Example: Binary equivalent of 9 is 1001, which contains even number of 1s.
A few evil numbers are 3, 5, 6, 9.
Design a program to accept a positive whole number and find the binary equivalent of the number and count the number of 1s in it and display whether it is a Evil number or not with an appropriate message. Output the result in format given below:
Example 1
INPUT : 15
BINARY EQUIVALENT : 1111
NO. OF 1s : 4
OUTPUT : EVIL NUMBER
Example 2
INPUT : 26
BINARY EQUIVALENT : 11010
NO. OF 1s : 3
OUTPUT : NOT AN EVIL NUMBER
Programming Code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
/**
* The class EvilNumber inputs a number and checks whether it is an Evil Number or not
* @author : agarwalrajhans@gmail.com
* @Program Type : BlueJ Program - Java
* @ISC Computer Science Practical Specimen Paper - Question 1
*/
import java.util.*;
class EvilNumber
{
    String toBinary(int n) // Function to convert a number to Binary
    {
        int r;
        String s=""; //variable for storing the result
 
        char dig[]={'0','1'}; //array storing the digits (as characters) in a binary number system
 
        while(n>0)
            {
                r=n%2; //finding remainder by dividing the number by 2
                s=dig[r]+s; //adding the remainder to the result and reversing at the same time
                n=n/2;
            }
        return s;
    }
   
    int countOne(String s) // Function to count no of 1's in binary number
    {
        int c = 0, l = s.length();
        char ch;
        for(int i=0; i<l; i++)
        {
            ch=s.charAt(i);
            if(ch=='1')
            {
                c++;
            }
        }
        return c;
    }
   
    public static void main(String args[])
    {
        EvilNumber ob = new EvilNumber();
        Scanner sc = new Scanner(System.in);
       
        System.out.print("Enter a positive number : ");
        int n = sc.nextInt();
       
        String bin = ob.toBinary(n);
        System.out.println("Binary Equivalent = "+bin);
       
        int x = ob.countOne(bin);
        System.out.println("Number of Ones = "+x);
       
        if(x%2==0)
            System.out.println(n+" is an Evil Number.");
        else
            System.out.println(n+" is Not an Evil Number.");
    }
}

Output:
Enter a positive number : 26
Binary Equivalent = 11010
Number of Ones = 3
26 is Not an Evil Number.

Enter a positive number : 420
Binary Equivalent = 110100100
Number of Ones = 4
420 is an Evil Number.

Enter a positive number : 659
Binary Equivalent = 1010010011
Number of Ones = 5
659 is Not an Evil Number.


Question:
The MOBIUS function M(N) for a natural number N is defined as follows:
M(N) = 1                          if N = 1 [Condition 1]
M(N) = 0                          if  any prime factor of N is contained in N more than once [Condition 2]
M(N) = (-1)p                    if N is a product of p distinct prime factors [Condition 3]
Example :
M(78) = -1                ( for 78 = 2 * 3 * 13     M(78) = ( -1)3 = -1 )
M(34) = 1                 ( for 34 = 2 * 17           M(34) = ( -1)2 = 1 )
M(12) = 0                 ( for 12 = 2 * 2 * 3       M(12) = 0 for 2 appears two times)
M(17) = -1                ( for 17 = 17                 M(17) = ( -1)1 = -1 )
Design a class MobiusFn to define Mobius function for a natural number n.
Class name : MobiusFn
Data members/Instance variables:
n : stores an integer number
Member functions:
MobiusFn() : default constructor
void input() : input value of n
int primeFac() : to check and count prime factors of n
void display() : to find and print values of Mobius function
Specify the class MobiusFn giving details of the constructors, void input(), int primeFac(), and void display(). Also define the main function to create an object and call methods accordingly to enable the task.

Programming Code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
/**
* The class MobiusFn inputs a number and calculates the value of Mobius Function
* @author : agarwalrajhans@gmail.com
* @Program Type : BlueJ Program - Java
* @Question Year : ISC Theory 1999
*/

import java.util.*;
class MobiusFn
{
    int n;
   
    MobiusFn()
    {
        n = 0;
    }
   
    void input()
    {
        Scanner sc = new Scanner(System.in);  
        System.out.print("Enter a number : ");
        n = sc.nextInt();
    }
   
    /*  The function primefac() either returns '0' if prime factors are repeated
     *  or returns the no.of prime factors */
    int primeFac()
    {
        int a=n, i=2, m=0, c=0, f=0;
           
        while(a > 1) // loop to generate prime factors
        {
            c = 0; // variable to store frequency of every prime factor
            while(a%i == 0) // if 'i' is a prime factor
            {
                c++; // counting frequency of 'i'
                f++; // counting no of prime factors
                a=a/i;
            }
                i++;

            if(c > 1) // returning '0' if prime factors are repeated
                return 0;
        }
        return f; // returning no. of prime factors
    }
   
    void display() // function to display value of mobius function
    {
        int mob,x;
        if(n == 1) // condition 1
            mob = 1;
        else
        {
            x = primeFac();
            if(x == 0) // condition 2
                mob = 0;
            else // condition 3
                mob = (int)Math.pow(-1,x);
        }
        System.out.println("Value of Mobius Function : "+mob);
    }
   
    public static void main(String args[])
    {
        MobiusFn ob = new MobiusFn();    
        ob.input();
        ob.display();    
    }
}

Output:
Enter a number : 78
Value of Mobius Function : -1

Enter a number : 12
Value of Mobius Function : 0

Enter a number : 34
Value of Mobius Function : 1

Enter a number : 17
Value of Mobius Function : -1

No comments:

Post a Comment

any problem in any program comment:-

Second largest number in java using ternary operator

 //Largest second number using ternary operator public class Main { public static void main(String[] args) { int a=5,b=6,c=7; int ...