20 Computer Programs ISC JAVA

Embed Size (px)

DESCRIPTION

ISC JAVA

Citation preview

  • 5/22/2018 20 Computer Programs ISC JAVA

    1/69

    Computer Project

    Class XII

  • 5/22/2018 20 Computer Programs ISC JAVA

    2/69

    Note:

    This project consists of 20 solved programs. Each program starts with a detailed question and

    the sample input/output followed by its algorithm and solution. The solutions are well

    commented so there shouldnt be any problem in understanding them. The programs aretested to run on the sample inputs provided, however, you must validate the program with

    different inputs keeping in mind the assumptions for each question. If you find any program not

    working (not giving the desired output for a particular input) kindly let us know by mailing us

    the question and the sample input case [email protected] posting it in the

    forum.

    If you find this project useful and it saved your time, you can return the favour in two ways:

    1. Since youve got the completed project and you have time to spare, try to write theseprograms in your favourite IDE (preferably BlueJ) and see what output youre getting.Give some time to understand the solution. This will not only help you in your ISC exams

    but will also come in handy if the examiner asks you to explain a particular program.

    Examiners choose random students for explanation and the less prepared you are, the

    more chances youll be picked. So its better to go prepared.

    2. Secondly, go through the tutorial section and read about different parts of programminglike loops, arrays, functions, classes etc. These topics are explained in detail. There are

    solved questions on each topic some left unsolved for practice. If you have queries or

    any program that you couldnt solve, post it in the forum. Well explain you in detail how

    it can be solved.

    - Mentors

    mailto:[email protected]:[email protected]:[email protected]:[email protected]
  • 5/22/2018 20 Computer Programs ISC JAVA

    3/69

    Index

    Sno. Question Page no

    1. Write a program to input a natural number less than 1000 and display it in words. 4

    2. Write a program to change the sentence of the odd rows with an encryption of

    two characters ahead of the original character and even rows by storing the

    sentence in reverse order.

    8

    3. Write a program to print the denominations in an amount entered by the user. 12

    4. Write a program to print Kaprekar numbers between a given range. 15

    5. Write a program to check if a number is Smith number or not. 18

    6. Write a program to print the unique-digit numbers between a given range. 21

    7. Write a program to print the frequency of each element in an array. 25

    8. Write a program to sort an array using insertion sort. 28

    9. Write a program to check is a number is magic number or not. 31

    10. Write a program to insert a given element at the given position in an array. 34

    11. Write a program to delete a given element from the array 37

    12. Write a program to find the no of times a substring is present in the main string. 40

    13. Write a program to print the number of vowels in each word of a sentence. 43

    14. Write a program to remove consecutively repeating characters from a string. 46

    15. Write a program to print the frequency of each word in a string. 49

    16. Write a program to arrange a sentence in ascending order of its word lengths. 53

    17. Write a program to sort a sentence in alphabetical order. 57

    18. Write a program to print the sum of border elements of a n x n matrix. 60

    19. Write a program to sort the border elements of a n x n matrix in ascending order. 63

    20. Write a program to print the prime elements of a matrix along with their position. 67

  • 5/22/2018 20 Computer Programs ISC JAVA

    4/69

    Question 1

    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.

    Example

    INPUT: 29

    OUTPUT: TWENTY NINE

    INPUT: 17001

    OUTPUT: OUT OF RANGE

    INPUT: 119

    OUTPUT: ONE HUNDRED AND NINETEEN

    INPUT: 500

    OUTPUT: FIVE HUNDRED

  • 5/22/2018 20 Computer Programs ISC JAVA

    5/69

    Algorithm

    Step-1: INPUT n

    Step-2: IF n>=1000 THEN exit

    Step-3: Create three string arrays ones[], teens[] and tens[] and store ones, teens andtens in words.

    Step-4: IF n>=100 AND n20 AND n10 AND n

  • 5/22/2018 20 Computer Programs ISC JAVA

    6/69

    Solution

    import java.util.*;

    class Question1{

    public static void main(String args[])

    throws InputMismatchException{

    Scanner scan=new Scanner(System.in);

    int n;

    System.out.println("Enter a number less than 1000");n=scan.nextInt();

    if(n>=1000)

    {

    System.out.println("OUT OF RANGE");

    }else{

    String result,h="",t="",o="";

    int a,b,c;

    String ones[]={"one", "two","three","four","five","six","seven","eight","nine"};

    String teens[]={"eleven","twelve","thirteen","fourteen",

    "fifteen","sixteen","seventeen","eighteen","nineteen"};

    String tens[]={"ten","twenty","thirty","forty","fifty",

    "sixty","seventy","eighty","ninety"};

    if(n>=100 && n

  • 5/22/2018 20 Computer Programs ISC JAVA

    7/69

    }

    else{

    result=tens[a-1];

    }

    n=n%10;

    }

    if(n>20 && n10 && n

  • 5/22/2018 20 Computer Programs ISC JAVA

    8/69

    Question 2

    Encryption is a technique of coding messages to maintain their secrecy. A String array of

    size 'n' where 'n' is greater than 1 and less than 10, stores single sentences (each sentence

    ends with a full stop) in each row of the array.

    Write a program to accept the size of the array.

    Display an appropriate message if the size is not satisfying the given condition.

    Define a string array of the inputted size and fill it with sentences row-wise.

    Change the sentence of the odd rows with an encryption of two characters ahead of the

    original character. Also change the sentence of the even rows by storing the sentence in

    reverse order.

    Display the encrypted sentences as per the sample data given below.

    Test your program on the sample data and some random data.

    INPUT: n=4

    IT IS CLOUDY. IT MAY RAIN. THE WEATHER IS FINE. IT IS COOL.

    OUTPUT: KV KU ENQWFA. RAIN MAY IT. VJG YGCVJGT KU HKPG. COOL IS IT.

    INPUT: n=13

    OUTPUT: INVALID ENTRY

  • 5/22/2018 20 Computer Programs ISC JAVA

    9/69

    Algorithm

    Step-1: Store n sentences in an array.

    Step-2: Start a loop and add a blank space at the end of each sentence so that the last word canbe extracted.

    Step-3: If the sentence is on the odd row GOTO Step 4 else GOTO Step 8

    Step-4: Run a loop and extract each character of the sentence one by one

    Step-5: If the character is not a blank or a sentence terminator add 2 to its ASCII value

    Step-6: If the result exceeds 90 subtract 26 from it.

    Step-7: Add the changed character to a temporary string and GOTO Step

    Step-8: store the last index of the sentence in pStep-9: Run a reverse loop

    Step-10: When a blank space is found extract the word from position+1 to p

    Step-11: Store the word with a space in a temporary string

    Step-12: Keep on updating p to point to the last character of previous word until all the words

    are extracted

    Step-13: Display the temporary string

    Step-14: End

  • 5/22/2018 20 Computer Programs ISC JAVA

    10/69

    Solution

    import java.io.*;

    class Question2{

    public static void main(String args[])

    throws IOException{BufferedReader br=new BufferedReader(

    new InputStreamReader(System.in));

    int nos;

    System.out.print("Enter number of sentences : ");

    nos=Integer.parseInt(br.readLine());

    if(nos=10)

    System.out.println("\nInvalid Entry");else{

    int i,j,p,l;

    String s[]=new String[nos];

    System.out.print("\nEnter "+nos+" sentences : ");

    for(i=0;i< nos;i++)

    s[i]=(br.readLine()).toUpperCase();

    for(i=0;i< nos;i++)

    {

    String t;

    s[i]=" "+s[i];// add a blank space before each sentence

    l=s[i].length();

    if(i%2==0){

    t="";for(j=0;j< l;j++){

    // store the ASCII code of the character

    int ch=s[i].charAt(j);

    if(ch!=32 && ch!='.'){

    ch=ch+2;//shift the letter two spaces

    if(ch>90)//to maintain cyclic order

  • 5/22/2018 20 Computer Programs ISC JAVA

    11/69

    ch=ch-26;// subtract 26

    }

    //convert to character and add to a temporary string

    t=t+(char)ch;

    }

    s[i]=t.trim();// remove leading or trailing spaces

    }else{

    t="";

    p=l-1;

    for(j=l-1;j>=0;j--){

    //reverse loop to start extraction of words

    //from last to first

    char ch=s[i].charAt(j);

    if(ch==' '){

    t=t+s[i].substring(j+1,p)+" ";

    // add the extracted word and a space

    p=j;

    }

    }

    t=t+".";

    s[i]=t;

    }

    }

    System.out.println("\nOUTPUT:");

    for(i=0;i< nos;i++)

    System.out.print(s[i]);

    }

    } //end of main

    } //end of class

  • 5/22/2018 20 Computer Programs ISC JAVA

    12/69

    Question 3

    A bank intends to design a program to display the denomination of an input amount, upto 5

    digits. The available denomination with the bank are of rupees 1000, 500, 100, 50, 20, 10 ,5,

    2 and 1.

    Design a program to accept the amount from the user and display the break-up indescending order of denominations. (i,e preference should be given to the highest

    denomination available) along with the total number of notes. [Note: only the

    denomination used should be displayed]. Also print the amount in words according to the

    digits.

    Example 1:

    INPUT: 14836

    OUTPUT: ONE FOUR EIGHT THREE SIX

    DENOMINATION:1000 X 14 =14000500 X 1 =500100 X 3 =300

    50 X 1 =505 X 1 =51 X 1 =1

    EXAMPLE 2:

    INPUT: 235001OUTPUT: INVALID AMOUNT

  • 5/22/2018 20 Computer Programs ISC JAVA

    13/69

    Algorithm

    Step-1: Enter the amount in n

    Step-2: Store the basic denominations (1000, 500, 100, 50, 20, 10, 5, 2, 1) in an array

    Step-3: Run a loop to access the array

    Step-4: Divide the amount n by each value in the array to get the quotient

    Step-5: If the quotient is not zero, display the denomination and update amount.

    Step-6: To display the denomination digits in words, create an array and store the digits in

    words

    Step-7: Now run a while loop to reverse the original number.

    Step-8: Run another loop and extract each digit of the reversed number.

    Step-9: Print each digit in words using the array just created.

    Step-10: End

  • 5/22/2018 20 Computer Programs ISC JAVA

    14/69

    Solution

    import java.util.*;

    class Question3{

    public static void main(String args[])throws InputMismatchException{

    Scanner scan=new Scanner(System.in);

    int amt;

    System.out.print("Enter a five-digit amount : ");

    amt=scan.nextInt();

    if(amt>99999)

    {

    System.out.println("INVALID AMOUNT.");

    }else{

    int a[]={1000,500,100,50,20,10,5,2,1};

    int i,p,r,b,t;

    p=amt;

    for(i=0;i0){

    r=r*10+p%10;

    p/=10;

    }

    while(r>0){

    b=r%10;

    System.out.print(ones[b-1].toUpperCase()+" ");

    r/=10;

    }

    }//end of if

    } //end of main

    } //end of class

  • 5/22/2018 20 Computer Programs ISC JAVA

    15/69

    Question 4

    Given the two positive integers p and q, where p < q. Write a program to determine how

    many kaprekar numbers are there in the range between 'p' and 'q'(both inclusive) and

    output them. About 'kaprekar' number:

    A positive whole number 'n' that has 'd' number of digits is squared and split into 2 pieces,

    a right hand piece that has 'd' digits and a left hand piece that has remaining 'd' or 'd-1'digits. If sum of the pieces is equal to the number then it's a kaprekar number.

    SAMPLE DATA:

    INPUT:p=1

    Q=1000

    OUTPUT:

    THE KAPREKAR NUMBERS ARE:1,9,45,55,99,297,999

    FREQUENCY OF KAPREKAR NUMBERS IS:8

  • 5/22/2018 20 Computer Programs ISC JAVA

    16/69

    Algorithm

    Step-1: Enter the limits

    Step-2: Run a loop using the entered limits

    Step-3: For each number in the range, count the number of digits(d) in it

    Step-4: Find the square of the number

    Step-5: Now, divide the square in two parts

    Step-6: To do this, first store 10 raised to the power d in t

    Step-7: For first part, calculate square mod t

    Step-8: For second part, calculate square/t

    Step-9: Now add the two parts

    Step-10: If the sum is equal to the original number then it is a kaprekar number, print it and

    count it.

    Step-11: If the sum is not equal to the original number it is not a kaprekar number

    Step-12: Continue the process till all the numbers in the range are checked

    Step-13: Display the frequency of kaprekar numbers.

    Step-14: End

  • 5/22/2018 20 Computer Programs ISC JAVA

    17/69

    Solution

    import java.util.*;

    class Question4{

    public static void main(String args[])throws InputMismatchException{

    Scanner scan=new Scanner(System.in);

    System.out.println("Enter the range : ");

    int p=scan.nextInt();

    int q=scan.nextInt();

    int d,i,n,a,b,s,freq;

    freq=0; // to find the frequency of kaprekar numbers

    System.out.println("The Kaprekar numbers are : ");

    for(i=p;i0){

    d++;

    n/=10;

    }

    s=i*i; // find the square of the number

    //extract 'd' digits from the right of the square of the number

    a=s%(int)Math.pow(10,d);

    //extract 'd' or 'd-1' digits from the left of the square of the number

    b=s/(int)Math.pow(10,d);

    //Check if the two parts add up to the original number i.e. Condition for Kaprekar number

    if(a+b==i){System.out.print(i+" ");

    freq++;

    }

    }

    System.out.println("\nFREQUENCY OF KAPREKAR NUMBER IS : "+freq);

    } //end of main

    } //end of class

  • 5/22/2018 20 Computer Programs ISC JAVA

    18/69

    Question 5

    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

    Write a program to input a number and check whether it is a smith number or not.

    Sample data:

    Input: 94 Output: SMITH Number

    Input: 102 Output: NOT SMITH Number

  • 5/22/2018 20 Computer Programs ISC JAVA

    19/69

    Algorithm

    Step-1: Input n

    Step-2: Run a while loop to find the sum of digits of n

    Step-3: Now run a for loop from 2 to the number

    Step-4: Look for the factors of n

    Step-5: If a factor is found, run a while loop to store its sum of digits

    Step-6: Update the number by dividing it with the factor

    Step-7: Decrement the loop counter so that the same factor is checked again

    Step-8: Outside the loop compare if the two sums are equal or not

    Step-9: If they are equal display Smith number else display Not Smith number

    Step-10: End

  • 5/22/2018 20 Computer Programs ISC JAVA

    20/69

    Solution

    import java.util.*;

    class Question5{

    public static void main(String sr[])throws InputMismatchException{Scanner sc=new Scanner(System.in);

    System.out.println("Enter a number: ");

    int n=sc.nextInt();

    int p,q,i,sod=0,sopf=0,t;

    p=q=n;

    //Find the sum of all the digits of the number

    while(p>0){sod+=p%10;

    p/=10;

    }

    for(i=2;i0){ //find the sum of the digits of the factor

    sopf+=t%10;t/=10;

    }

    q=q/i;

    i--; //decrement the factor so that next time the same factor is checked again and

    again until it is not a factor. This is the prime factorization method.

    }

    }

    if(sod==sopf) // if sum of digits and sum of prime factors are equal, it is smith number

    System.out.println("Smith number");

    else

    System.out.println("Not Smith number");

    } //end of main

    } //end of class

  • 5/22/2018 20 Computer Programs ISC JAVA

    21/69

    Question 6

    A unique-digit integer is a positive integer (without leading zeros) with no duplicate digits.

    For example 7, 135, 214 are all unique-digit integers whereas 33, 3121, 300 are not.

    Given two positive integers m and n, where m< n, write a program to determine how many

    unique-digit integers are there in the range between m and n (both inclusive) and output

    them.

    The input contains two positive integers m and n. Assume m< 30000 and n< 30000.

    You are to output the number of unique-digit integers in the specified range along with

    their values in the format specified below:

    SAMPLE DATA:

    INPUT: m = 100 n = 120OUTPUT: THE UNIQUE- DIGIT INTEGERS ARE : 102, 103, 104, 105, 106, 107, 108,109, 120.

    FREQUENCY OF UNIQUE-DIGIT INTEGERS IS: 9.

  • 5/22/2018 20 Computer Programs ISC JAVA

    22/69

    Algorithm

    Step-1: Input the limits m and n

    Step-2: Run a loop from m to n

    Step-3: For each value of the loop, extract its digits

    Step-4: Store the digits in an array

    Step-5: Set flag as true

    Step-6: Run a two loops till the length of the array

    Step-7: If the array values of the two loops match, set the flag to false and break the loops

    Step-8: If the flag is true display the number and count

    Step-9: Outside the loop display the count as frequency of unique-digit numbers

    Step-10:End

  • 5/22/2018 20 Computer Programs ISC JAVA

    23/69

    Solution

    import java.util.*;

    class Question6{

    public static void main(String ar[])throws InputMismatchException{

    Scanner sc=new Scanner(System.in);

    System.out.println("Enter the range: ");

    int m=sc.nextInt();

    int n=sc.nextInt();

    int a[]=new int[10];

    int i,j,k,p,x,freq=0;

    boolean flag;

    System.out.println("Unique-Digit numbers are:");

    for(i=m;i0){

    a[x++]=p%10;

    p/=10;

    }

    flag=true;

  • 5/22/2018 20 Computer Programs ISC JAVA

    24/69

    for(j=0;j

  • 5/22/2018 20 Computer Programs ISC JAVA

    25/69

    Question 7

    Write a program to create an array of n integers and display the frequency of each element of

    the array.

    Example:

    Input:Enter the number of terms: 10

    Enter 10 integers: 1 2 2 2 3 4 3 4 5 6

    Output:Frequency of 1: 1

    Frequency of 2: 3

    Frequency of 3: 2

    Frequency of 4: 2Frequency of 5: 1

    Frequency of 6: 1

  • 5/22/2018 20 Computer Programs ISC JAVA

    26/69

    Algorithm

    Step-1: Input the size of the array as n

    Step-2: Create an array of n elements

    Step-3: Run a loop i from 0 to n

    Step-4: Run a loop j from i+1 to n

    Step-5: If the elements a[i] and a[j] are equal and a[j] is not zero, count and put zero in a[j]

    Step-6: Outside the inner loop, print frequency of a[i] if it is not zero

    Step-7: End

  • 5/22/2018 20 Computer Programs ISC JAVA

    27/69

    Solution

    import java.util.*;

    class Question7{

    public static void main(String args[])throws InputMismatchException{

    Scanner sc=new Scanner(System.in);

    System.out.println("Enter the number of terms: ");

    int n=sc.nextInt();

    int a[]=new int[n];

    System.out.println("Enter "+n+ " integers");

    int i;

    for(i=0;i

  • 5/22/2018 20 Computer Programs ISC JAVA

    28/69

    Question 8

    Write a program to create an array of n integers and sort the array in ascending order using

    Insertion sort technique.

    Example

    INPUT:

    Enter the size of the array: 5

    Enter 5 elements: 5 9 7 3 -4

    OUTPUT:

    -4 3 5 7 9

  • 5/22/2018 20 Computer Programs ISC JAVA

    29/69

    Algorithm

    Step-1: Input the size of the array n

    Step-2: Create an array a[] and enter n integers in it.

    Step-3: Now to apply insertion sort on this array, run a loop i from 1 to n

    Step-4: Store the element a[i] in k

    Step-5: Run a reverse loop j from i-1 till j>=0 and k

  • 5/22/2018 20 Computer Programs ISC JAVA

    30/69

    Solution

    import java.util.*;

    class Question8 {

    public static void main(String args[])throws InputMismatchException{

    Scanner sc=new Scanner(System.in);

    System.out.println("Enter the size of the array: ");

    int n=sc.nextInt();

    int a[]=new int[n];

    int i,j,k;

    System.out.println("Enter "+n+" integers:");

    for(i=0;i

  • 5/22/2018 20 Computer Programs ISC JAVA

    31/69

    Question 9

    Write a program to enter a number and check if it is a magic number or not.

    A number whose sum of digits ultimately yields 1 is known as a Magic number.

    Example:

    n = 109109 = 9+0+1

    =10 (Still a number, find the sum again)

    10 =0+1

    =1

    The result is 1. Hence, 109 is a magic number.

    n=18

    18 =8+1

    =9The result is 9, which is not equal to 1, hence 18 is not a magic number.

    Input: n=109

    Output: 109 is a magic number

    Input: n=18

    Output: 18 is not a magic number.

  • 5/22/2018 20 Computer Programs ISC JAVA

    32/69

    Algorithm

    Step-1: Input a number n

    Step-2: Copy the number in p

    Step-3: Run a loop as long as p is not equal to zero

    Step-4: Run another loop to find the sum of digits of p

    Step-5: Outside the loop check if sum is greater than 9

    Step-6: If it is greater than 9 it means its a number, copy it in p and reset the sum

    Step-7: The loop will run until p becomes 0

    Step-8: If the sum is equal to 1, display Magic number else Not a magic number

    Step-9:End

  • 5/22/2018 20 Computer Programs ISC JAVA

    33/69

    Solution

    import java.util.*;

    public class Question9 {

    public static void main(String args[])throws InputMismatchException{

    Scanner sc=new Scanner(System.in);

    System.out.println("Enter a number: ");

    int n=sc.nextInt();

    int s=0,p,a;

    p=n;

    do{

    while(p>0){

    a=p%10;

    s=s+a;

    p=p/10;

    }

    if(s>9){

    p=s;

    s=0;

    }

    }while(p>0);

    if(s==1)

    System.out.println(n+" is a magic number");

    else

    System.out.println(n+" is not a magic number");

    } //end of main

    } //end of class

  • 5/22/2018 20 Computer Programs ISC JAVA

    34/69

    Question 10

    Write a program to create an array of n elements and insert a given element at the given

    position in the array. Your program should display appropriate error message if the position is

    invalid.

    Example

    INPUT:

    Size of the array: 5

    Input 5 integers: 1 2 4 5 6

    Enter the element to be inserted: 3

    Enter the position at which the element should be inserted: 3

    OUTPUT:

    1 2 3 4 5 6

    INPUT:

    Size of the array: 7

    Input 5 integers: 4 3 5 8 6 2 7

    Enter the element to be inserted: 13

    Enter the position at which the element should be inserted: 10

    OUTPUT:

    Invalid position entered.

  • 5/22/2018 20 Computer Programs ISC JAVA

    35/69

    Algorithm

    Step-1: Enter the size in n

    Step-2: Create an array of size (n+1)

    Step-3: Enter n elements from the user

    Step-4: Enter the element(e) and the position(p)

    Step-5: Run a loop i to access array

    Step-6: Check if the position is invalid by comparing it with i+1

    Step-7: If match not found keep checking with the rest of the array

    Step-8: If match found run a loop from n to p and shift all the elements one place to the right,

    store the element e in the desired position, raise a flag and break out of loop

    Step-9: Outside the loop, check if the flag is raised

    Step-10: If it is display the modified array else display error message

    Step-11: End

  • 5/22/2018 20 Computer Programs ISC JAVA

    36/69

    Solution

    import java.util.*;

    public class Question10{

    public static void main(String args[])throws InputMismatchException{

    Scanner sc=new Scanner(System.in);

    int i,j,k;

    System.out.println("Enter the size of the array: ");

    int n=sc.nextInt();

    int a[]=new int[n+1];

    System.out.println("Enter "+n+" integers:");

    for(i=0;i

  • 5/22/2018 20 Computer Programs ISC JAVA

    37/69

    Question 11

    Write a program to create an array of n elements and delete a given element from the array.

    Your program should display appropriate error message if the element to be deleted is not

    found in the array.

    Example

    Input:

    Size of the array: 5

    Input 5 integers: 1 2 4 5 6

    Enter the element to be deleted: 4

    Output:

    1 2 5 6

    Input:

    Size of the array: 7

    Input 5 integers: 4 3 5 8 6 2 7

    Enter the element to be deleted: 1

    Output:

    Element not found in the array.

  • 5/22/2018 20 Computer Programs ISC JAVA

    38/69

    Algorithm

    Step-1: Enter the size in n

    Step-2: Create an array of size (n+1)

    Step-3: Enter n elements from the user

    Step-4: Enter the element(e) to be deleted

    Step-5: Run a loop i to access array

    Step-6: Check if the element(e) is present in the array

    Step-7: If match not found goto step 11

    Step-8: If match found run a loop from i to n-1

    Step-9: Shift all the elements one place to the left

    Step-10: decrease the size of loop, raise a flag and break out of loop

    Step-11: Keep checking the rest of the array until the end

    Step-12: If the flag is raised display the modified array else display error message

    Step-13: End

  • 5/22/2018 20 Computer Programs ISC JAVA

    39/69

    Solution

    import java.util.*;

    public class Question11{

    public static void main(String args[])throws InputMismatchException{

    Scanner sc=new Scanner(System.in);

    System.out.println("Enter the size of the array: ");

    int n=sc.nextInt();

    int a[]=new int[n];

    int i,j,k;

    System.out.println("Enter "+n+" integers:");

    for(i=0;i

  • 5/22/2018 20 Computer Programs ISC JAVA

    40/69

    Question 12

    Write a program to enter a main string and a substring and find the number of times the

    substring is present in the main string ignoring case considerations.

    Assume that substring is present only as a single word. It is neither found inside the string nor

    as a group of words.

    Example

    Input:

    Enter a main string: The man goes to the theatre.

    Enter a substring: the

    Output:

    No of times substring is present in the main string: 2

  • 5/22/2018 20 Computer Programs ISC JAVA

    41/69

    Algorithm

    Step-1: Enter a main string and the substring.

    Step-2: Find the length of the main string

    Step-3: Run a loop to access the main string

    Step-4: Extract a character from the main string

    Step-5: If it is not blank goto step 9

    Step-6: Extract the word from the main string

    Step-7: Compare it with the substring using function to ignore case

    Step-8: If the strings are equal increment a counter

    Step-9: Keep checking the rest of the string until the end

    Step-10: Display the counter along with the message

    Step-11: End

  • 5/22/2018 20 Computer Programs ISC JAVA

    42/69

    Solution

    import java.io.*;

    public class Question12{

    public static void main(String args[])throws IOException{

    BufferedReader br=new BufferedReader(new InputStreamReader(System.in));

    System.out.println("Enter a main string: ");

    String ms=br.readLine();

    System.out.println("Enter a substring: ");

    String ss=br.readLine();

    int l=ms.length();

    int i,p=0,freq=0;

    for(i=0;i

  • 5/22/2018 20 Computer Programs ISC JAVA

    43/69

    Question 13

    Write a program to enter a string and print each word along with the number of vowels in it.

    Example

    INPUT:

    Enter a string: These are wonderful times

    OUTPUT:

    Word Vowel

    These 2

    are 2

    wonderful 3

    times 2

  • 5/22/2018 20 Computer Programs ISC JAVA

    44/69

    Algorithm

    Step-1: Enter a string

    Step-2: Find the length of the string

    Step-3: Run a loop to access the string

    Step-4: Extract a character from the string

    Step-5: If it is not blank goto step 11

    Step-6: Extract the word from the string

    Step-7: Run a loop to access the word

    Step-8: Check for vowels

    Step-9: If vowel found, increment a counter

    Step-10: Once the inner loop terminates display the word and the counter

    Step-11: Keep checking the rest of the string until the end

    Step-12: End

  • 5/22/2018 20 Computer Programs ISC JAVA

    45/69

    Solution

    import java.io.*;

    class Question13

    {

    public static void main(String fh[])throws IOException

    {

    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

    System.out.println("Enter a sentence : ");

    String str=br.readLine();

    str+=" ";

    int l=str.length();

    String t=" ";

    int i,j,c=0,p=0;

    for(i=0;i

  • 5/22/2018 20 Computer Programs ISC JAVA

    46/69

    Question 14

    Write a program to enter a string and remove consecutively repeating characters from a string.

    Example

    Input:

    Enter a string: The freedom of expression is immensely overused.

    Output:

    The fredom of expresion is imensely overused.

  • 5/22/2018 20 Computer Programs ISC JAVA

    47/69

    Algorithm

    Step-1: Enter a string

    Step-2: Find the length of the string

    Step-3: Copy the string to a character array

    Step-4: Run a loop i to access the array

    Step-5: Compare ithand (i+1)thelement of the array

    Step-6: If both the elements are not equal goto step 9

    Step-7: Run a loop from i to the end

    Step-8: Shift all the characters one place to the left

    Step-9: Decrease the length of the array

    Step-10: Keep checking the rest of the string until the end

    Step-11: Set original string to null

    Step-12: Run a loop and add all the elements of the array to the string

    Step-13: Display the string

    Step-14: End

  • 5/22/2018 20 Computer Programs ISC JAVA

    48/69

    Solution

    import java.io.*;

    public class Question14{

    public static void main(String fh[])throws IOException

    {

    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

    System.out.println("Enter a string : ");

    String str = br.readLine();

    char a[]=str.toCharArray();

    int i,j,l=a.length;

    for(i=0;i

  • 5/22/2018 20 Computer Programs ISC JAVA

    49/69

    Question 15

    Write a program to enter a string and print the frequency of each word in a string.

    Example

    Input:

    Enter a string: The need for enlightenment is the need of the hour.

    Output:

    Word Frequency

    The 3

    need 2

    for 1

    enlightenment 1

    is 1

    of 1

    hour 1

  • 5/22/2018 20 Computer Programs ISC JAVA

    50/69

    Algorithm

    Step-1: Enter a string

    Step-2: Find the length of the string

    Step-3: Create a string array

    Step-4: Run a loop i to access the string

    Step-5: Extract word from the string and store it in the string array

    Step-6: Continue the process until all the words are stored in the array

    Step-7: Now run a loop to access the array

    Step-8: Set f=1

    Step-9: Run another loop to check equality of elements of the array

    Step-10: If equal, increment f by 1 and shift all the elements to the left

    by one place and decrease the size of the array

    Step-11: Display the element along with its frequency f

    Step-12: Repeat the process for all the elements of the array

    Step-13: End

  • 5/22/2018 20 Computer Programs ISC JAVA

    51/69

    Solution

    import java.io.*;

    class Question15

    {

    public static void main(String args[])throws IOException

    {

    BufferedReader br=new BufferedReader(new InputStreamReader(System.in));

    System.out.print("Enter a String");

    String s=br.readLine();

    s=s+" ";

    int i,t=0,p=0,f=0,j,k;

    int l=s.length();

    String a[]=new String[l];

    String b;

    for(i=0;i

  • 5/22/2018 20 Computer Programs ISC JAVA

    52/69

    for(i=0;i

  • 5/22/2018 20 Computer Programs ISC JAVA

    53/69

    Question 16

    Write a program to enter a sentence and print it in ascending order of its word lengths.

    A sentence may either terminate with a period (.), exclamation mark (!) or a question mark

    (?).

    Example

    INPUT:

    Enter a sentence: Days are longer in summers.

    OUTPUT:

    in are days longer summers.

  • 5/22/2018 20 Computer Programs ISC JAVA

    54/69

    Algorithm

    Step-1: Enter a string

    Step-2: Find the length of the string

    Step-3: Create a string array

    Step-4: Run a loop i to access the string

    Step-5: Extract word from the string and store it in the string array

    Step-6: Continue the process until all the words are stored in the array

    Step-7: Now run a loop to access the array

    Step-8: Run two loops for sorting, say i and j

    Step-9: If length of the ithelement is greater than the length of the jthelement swap them.

    Step-10: Continue the process till the entire array is sorted

    Step-11: Display the words in the array along with a blank space

    Step-12: End

  • 5/22/2018 20 Computer Programs ISC JAVA

    55/69

    Solution

    import java.io.*;

    class Question16

    {

    public static void main (String args[])throws IOException

    {

    BufferedReader br=new BufferedReader(new InputStreamReader (System.in));

    System.out.print("enter the string");

    String s = br.readLine();

    s=s.toLowerCase();

    int l= s.length();

    String a[]= new String[l];

    int t=0,i,p,j;

    String g;

    char k;

    p=0;

    for(i=0;i

  • 5/22/2018 20 Computer Programs ISC JAVA

    56/69

    //Using bubble sort to arrange the string array in ascending order of word length

    for(i=0;i

  • 5/22/2018 20 Computer Programs ISC JAVA

    57/69

    Question 17

    Write a program to enter a sentence and sort it in alphabetical order. You can convert the

    sentence in either uppercase or lowercase before sorting.

    Example

    INPUT:

    Enter a sentence: Every cloud has a silver lining.

    OUTPUT:

    a cloud every has lining silver

  • 5/22/2018 20 Computer Programs ISC JAVA

    58/69

    Algorithm

    Step-1: Enter a sentence

    Step-2: Convert it into lowercase

    Step-3: Take the length of the string

    Step-4: Run a loop to access the string

    Step-5: Extract a character

    Step-6: If character is blank space or a period, extract the word

    and store it in an array

    Step-7: Continue the process until all the words are stored in the array

    Step-8: Now run two loops i and j for sorting

    Step-9: Compare the strings in the array

    Step-10: If ithstring is greater than jthstring swap them

    Step-11: Continue the process till the entire array is sorted in ascending order

    Step-12: Run a loop and display array elements with a blank space.

    Step-13: End

  • 5/22/2018 20 Computer Programs ISC JAVA

    59/69

    Solution

    import java.io.*;

    public class Question17 {

    public static void main(String args[])throws IOException{

    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

    System.out.println("Enter a string : ");

    String str = br.readLine();

    str=str.toLowerCase();

    int l=str.length();

    String a[]=new String[l];

    int i,j,p=0,x=0;

    for(i=0;i

  • 5/22/2018 20 Computer Programs ISC JAVA

    60/69

    Question 18

    Write a program to print the sum of border elements of an n x n matrix.

    Example

    INPUT:

    Enter the number of rows for a square matrix: 3

    Enter 9 elements:

    1 2 3

    4 5 6

    7 8 9

    OUTPUT:

    Sum of the border elements: 40

  • 5/22/2018 20 Computer Programs ISC JAVA

    61/69

    Algorithm

    Step-1: Input the number of rows n for a square matrix

    Step-2: Create an nxn matrix

    Step-3: Enter nxn elements from the user

    Step-4: Run a loop for the number of rows

    Step-5: Run a loop for the number of columns

    Step-6: If row is first or last or if column if first or last add the element

    Step-7: Continue the process till the entire matrix is checked

    Step-8: Display the sum

    Step-9: End

  • 5/22/2018 20 Computer Programs ISC JAVA

    62/69

    Solution

    import java.util.*;

    public class Question18 {public static void main(String args[])throws InputMismatchException{

    Scanner sc=new Scanner(System.in);

    System.out.println("Enter the number of rows for a sqaure matrix: ");

    int n=sc.nextInt();

    int a[][]=new int[n][n];

    int i,j,s=0;

    System.out.println("Enter "+(n*n)+" elements:");

    for(i=0;i

  • 5/22/2018 20 Computer Programs ISC JAVA

    63/69

    Question 19

    Write a program to sort the border elements of a n x n matrix in ascending order.

    Example

    INPUT:

    Enter the number of rows for a square matrix: 3

    Enter 9 elements:

    4 9 3

    1 7 6

    5 8 2

    OUTPUT:

    Matrix with border in ascending order:

    1 2 3

    9 5 4

    8 7 6

  • 5/22/2018 20 Computer Programs ISC JAVA

    64/69

    Algorithm

    Step-1: Input the number of rows for a square matrix

    Step-2: Create an n x n matrix and enter elements in it

    Step-3: Run two loops to access the matrix

    Step-4: Check for border elements

    Step-5: If found, store them in a single dimension array, say b[]

    Step-6: Continue the process till all the border elements are stored in b[]

    Step-7: Now, sort the array b[] in ascending order using any sorting technique

    Step-8: To store the sorted border elements in the matrix, run a loop for the

    first row and store elements from b[] in the matrix

    Step-9: Run a loop for last column and do the same

    Step-10: Run a loop for last row and then first column to store the elements from b[] to matrix

    Step-11: Finally, print the matrix that now has border elements sorted in ascending order.

    Step-12: End

  • 5/22/2018 20 Computer Programs ISC JAVA

    65/69

    Solution

    import java.util.*;

    public class Question19 {

    public static void main(String args[])throws InputMismatchException{

    Scanner sc=new Scanner(System.in);

    System.out.println("Enter the number of rows for a sqaure matrix: ");

    int n=sc.nextInt();

    int a[][]=new int[n][n];

    int i,j,s=0;

    System.out.println("Enter "+(n*n)+" elements:");

    for(i=0;i

  • 5/22/2018 20 Computer Programs ISC JAVA

    66/69

    //Now store the sorted border elements back to the matrix

    int c=0;

    for(i=0;i0;i--) //Store the sorted elements in the first column

    a[i][0]=b[c++];

    for(i=0;i

  • 5/22/2018 20 Computer Programs ISC JAVA

    67/69

    Question 20

    Write a program to create a m x n matrix and print the prime elements in it along with the row

    and column in which they are present.

    Example

    INPUT:

    Enter the row and column for the matrix: 3

    4

    Enter 12 elements:

    4 5 1 6

    8 25 30 2

    16 9 45 3

    OUTPUT:

    Prime Element Row Column

    5 0 1

    2 1 3

    3 2 3

  • 5/22/2018 20 Computer Programs ISC JAVA

    68/69

    Algorithm

    Step-1: Input the number of rows and columns as m and n

    Step-2: Create a m x n matrix

    Step-3: Enter m x n elements in the matrix

    Step-4: Run a loop for the rows m

    Step-5: Run a loop for the columns n

    Step-6: Now run a loop to find the number of factors of the matrix element

    Step-7: If the number of factors is 2 print the element, its row index and column index

    Step-8: Continue process for all the elements

    Step-9: End

  • 5/22/2018 20 Computer Programs ISC JAVA

    69/69

    Solution

    import java.util.*;

    public class Question20{

    public static void main(String args[])throws InputMismatchException{

    Scanner sc=new Scanner(System.in);

    System.out.println("Enter the number of rows and columns of a matrix: ");

    int m=sc.nextInt();

    int n=sc.nextInt();

    int a[][]=new int[m][n];

    int i,j,k,s=0;

    System.out.println("Enter "+(m*n)+" elements:");

    for(i=0;i