Computer Project

157
Page | 1 Acknowledgement I would like to express my special thanks of gratitude to our principal, Mrs. Rosamma Mathew and my teacher Mrs. Ani Sweety Babu who gave me the golden opportunity to do this wonderful project. Secondly, I would also like to thank my parents and friends, who helped me a lot in finishing my project within limited time. I cannot conclude without thanking God for keeping me healthy and making me able to complete my project

description

ISC Computer Science Project

Transcript of Computer Project

Page 1: Computer Project

P a g e | 1

Acknowledgement

I would like to express my special thanks of gratitude to our principal, Mrs. Rosamma Mathew and my teacher Mrs. Ani Sweety Babu who gave me the golden opportunity to do this wonderful project.

Secondly, I would also like to thank my parents and friends, who helped me a lot in finishing my project within limited time. I cannot conclude without thanking God for keeping me healthy and making me able to complete my project

ContentsS.N TOPIC PAGE

Page 2: Computer Project

P a g e | 2

O1 Arrange 42 Wondrous 7

3 Anagrams 15

4 NumberCombinations 20

5 MergeArray 23

6 CopyProtection 29

7 FindDay 34

8 Reverse 38

9 UniqueDigitIntegers 42

10 Smith 45

11 VowelCount 49

12 SortArray 54

13 FindDate 60

14 Boundary 65

15 WordArrange 76

16 Denominations 79

17 Kaprekar 84

18 Frequency 87

19 NumberName 92

20 Encryption 95

21 BirthDate 99

Page 3: Computer Project

P a g e | 3

22 PrimePalindrome 102

23 AlphabeticalOrder 105

24 Matrix 108

25 ISBN 114

26 MirrorImage 118

27 Palindrome 122

28 CompositeMagic 125

29 SymmetricMatrix 129

30 Delete 135

31 LongestWord 138

32 VowelFrequency 141

33 ReversePalin 143

34 Initials 145

35 Common 147

Question 1Write a program which takes a string (maximum 80 characters) terminated by a full stop. The words in this string are assumed to be separated to be separated by one or more blanks.

Page 4: Computer Project

P a g e | 4

Arrange the words of the input string in descending order of their lengths. 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.

ALGORITHM:1) Accept a sentence.2) Considering the first word as the longest word, compare its

length with other words.3) Find the number of occurrence of the largest word and display it.4) After displaying the largest word, replace it with space.

SOURCE CODE:import java.util.*;class Arrange{public static void main(String args[]){Scanner sc=new Scanner(System.in);System.out.println("Enter a sentence"); //Inputting the sentenceString s=sc.nextLine();int count=0,c=0;String s1,s2="",large="";System.out.println(“OUTPUT:”);do{StringTokenizer st=new StringTokenizer(s," .");

Page 5: Computer Project

P a g e | 5

count=st.countTokens();while(st.hasMoreTokens()){s1=st.nextToken();large=s1; //Consider first word as largest wordStringTokenizer str=new StringTokenizer(s," ."); while(str.hasMoreTokens()){s2=str.nextToken();if(s2.length()>large.length()) //comparing the length of words with the first wordlarge=s2;if(s2.length()==large.length()){if((int)s2.charAt(0)<(int)large.charAt(0))large=s2;}} } StringTokenizer sr=new StringTokenizer(s," .");while(sr.hasMoreTokens()){if(sr.nextToken().equalsIgnoreCase(large))c++; //counting number of occurence of the largest word}for(int i=1;i<=c;i++){int l=large.length();System.out.print(Character.toUpperCase(large.charAt(0))+large.substring(1)+" "); }s=s.replace(large," "); //replacing the printed word with spacec=0;}while(count!=0); //loop terminatesSystem.out.print(".");}

Page 6: Computer Project

P a g e | 6

}

Output:(i) Enter a sentence:

This is Human Resource Department.OUTPUT:Department Resource Human This Is .

(ii) Enter a sentence:To handle yourself use your head and to handle others use your heart.OUTPUT:Yourself Heart Handle Handle Others Head Your Your And Use Use To To.

Question 2A wondrous square is an n by n grid which fulfils the following conditions.

(i) It contains integers from 1 to n2 where each integer appears only once.

(ii) The sum of integers in any row or column must add up to 0.5 * n * (n2 + 1).

For example the following grid is a wondrous square where the sum of each row or column is 65 where n = 5:

17 24 2 8 1523 5 7 14 164 6 13 20 2210 12 19 21 311 18 25 2 9

Write a program to read n (2<=n<=10) and the values stored in these n by n cells and output if the grid represents a wondrous square or not.Also output all the prime numbers in the grid along with their row index and column index as shown in the output. A natural numbers is said to be prime if it has exactly two divisors. E.g. 2, 3, 5, 7, 11 …

Page 7: Computer Project

P a g e | 7

The first element of the grid i.e. 24 is stored at row index 0 and column index 1.Test your program for the following data and some random data.SAMPLE DATA:INPUT: N=4

16 15 1 26 4 10 149 8 12 53 7 11 13

OUTPUT:YES IT REPRESENTS A WONDROUS SQUARE.PRIME ROW INDEX COLUMN INDEX 2 0 3 3 3 0 5 2 3 7 3 1 11 3 2 13 3 3

INPUT: N=3 1 2 4 3 7 5

8 9 6OUTPUT:NOT A WONDROUS SQUAREPRIME ROW INDEX COLUMN INDEX 2 0 1 3 1 0 5 1 2 7 1 1

ALGORITHM:1) Input the elements of the grid and print the original grid.2) Convert the 2-D array to 1-D array to check for duplicate

elements.

Page 8: Computer Project

P a g e | 8

3) Find the sum of elements of each row and each column and count the number of rows and columns which satisfy the condition.

4) If all the rows and column satisfy the condition print positive result else negative result.

5) Find the elements which are prime by passing each element to the sub function and print them.

SOURCE CODE:import java.util.*;class Wondrous{public static void main(String args[]){Scanner sc=new Scanner(System.in);Wondrous obj=new Wondrous();System.out.println("Enter the dimension of the grid");int n=sc.nextInt();int i,j,c,sum=0,co=0,r=0,num;int size=n*n;int b[]=new int[size],a[][]=new int[n][n];if(n>=2&&n<=10){System.out.println("Enter the values"); //Inputting values of the gridfor(i=0;i<n;i++){for(j=0;j<n;j++){ a[i][j]=sc.nextInt();}}System.out.println("Grid:"); //Printing the original gridfor(i=0;i<n;i++){for(j=0;j<n;j++){ System.out.print(a[i][j]+"\t");

Page 9: Computer Project

P a g e | 9

}System.out.println();}int k=0;for(i=0;i<n;i++) //Converting 2-D array into 1-D to check for any duplicate elements{for(j=0;j<n;j++){ b[k]=a[i][j]; k++;}}for(i=0;i<size;i++) //Checking whether any number occurs more than once{c=0;for(j=0;j<size;j++){ if(b[i]==b[j]) c++;}if(c>1) { System.out.println("Element "+b[i]+" occurs "+c+" times"); break; }}for(j=0;j<n;j++){k=0;sum=0;for(i=0;i<n;i++){ sum+=a[k][i]; //Sum of 'k'th row}

Page 10: Computer Project

P a g e | 10

k++;if(sum==0.5*n*((n*n)+1))r++; //Counting the number of rows which satisfy the conditionk=0;sum=0;for(i=0;i<n;i++){ sum+=a[i][k]; //Sum of 'k'th column}k++;if(sum==0.5*n*((n*n)+1))co++; //Counting the number of columns which satisfy the condition}if(r==n&&co==n)System.out.println("YES IT REPRESENTS A WONDROUS SQUARE");elseSystem.out.println("NOT A WONDROUS SQUARE");System.out.println("PRIME\tROW INDEX\tCOLUMN INDEX");for(i=0;i<n;i++){for(j=0;j<n;j++){ num=a[i][j]; if(obj.isPrime(num)==true) System.out.println(num+"\t "+i+"\t\t "+j); }}}}public boolean isPrime(int num) //Function to find if an element is prime {int i,count=0;for(i=1;i<=num;i++){if(num%i==0)

Page 11: Computer Project

P a g e | 11

count++;}if(count==2)return true;elsereturn false;}}

OUTPUT:1) Enter the dimension of the grid

4Enter the values16151264101498125371113Grid:16 15 1 26 4 10 149 8 12 53 7 11 13YES IT REPRESENTS A WONDROUS SQUAREPRIME ROW INDEX COLUMN INDEX

Page 12: Computer Project

P a g e | 12

2 0 35 2 33 3 07 3 111 3 213 3 3

2) Enter the dimension of the grid3Enter the values124375896Grid:1 2 43 7 58 9 6NOT A WONDROUS SQUAREPRIME ROW INDEX COLUMN INDEX

2 0 13 1 07 1 15 1 2

Page 13: Computer Project

P a g e | 13

Question 3We would like to generate all possible anagrams of a word. For example if the given word is ‘TOP’, there will be six possible anagrams:TOPTPOOPTOTPPTOPOTAn anagram must be printed only once. You may output the anagrams in any order. Also output the total number of anagrams. You assume that the number of letter, n, in the word will be 7 at most, i.e. n<=7Test your program for the given data and some random data.SAMPLE DATA:

INPUT: TOOUTPUT: TO

OTTotal number of anagrams=2

INPUT: LEANOUTPUT: LEAN

LENALAENLANELNEALNAEEALNEANLELANELNAENLAENALALNEALENANLE

Page 14: Computer Project

P a g e | 14

ANELAENLAELNNLEANLAENELANEALNALENAEL

Total number of anagrams=24

ALGORITHM:1) Accept a word.2) Make the possible combinations by combining different letters of

the word using sub functions.3) Print the anagrams and the number of anagrams.

SOURCE CODE:import java.util.*;class Anagrams{ static int sum=0; public static void main(String args[]) { Scanner sc=new Scanner(System.in);

Page 15: Computer Project

P a g e | 15

System.out.println("Enter a word"); String str=sc.nextLine(); //Inputting the word str=str.toUpperCase(); String w=str.substring(0,str.length()); System.out.println("Output:"); perm(w); //calling the recursion function System.out.println("Total number of anagrams:"+sum);; } public static void perm(String s) //Indirect recursion { perm(" ",s); //calling the sub function } public static void perm(String prefix,String s) { int n=s.length(); if(n==0) { System.out.println(prefix); sum++; } else for(int i=0;i<n;i++) perm((prefix+s.charAt(i)),(s.substring(0,i)+s.substring(i+1,n))); }}

Output:1) Enter a word

TOOutput: TO OTTotal number of anagrams:2

2) Enter a word

Page 16: Computer Project

P a g e | 16

SUMIOutput: SUMI SUIM SMUI SMIU SIUM SIMU USMI USIM UMSI UMIS UISM UIMS MSUI MSIU MUSI MUIS MISU MIUS ISUM ISMU IUSM IUMS IMSU IMUSTotal number of anagrams:24

3) Enter a wordTOPOutput: TOP TPO OTP OPT PTO

Page 17: Computer Project

P a g e | 17

POTTotal number of anagrams:6

Question 4A positive natural number, (for example 27) can be represented as follows:

2+3+4+5+6+78+9+1013+14

Where every row represents a combination of consecutive natural number, which add up to 27.Write a program which inputs a positive natural number N and prints the possible consecutive number combinations, which when added give N.Test your program for the following data and some random data.

SAMPLE DATA:INPUT: N=9OUTPUT: 4 5

2 3 4INPUT: N=15OUTPUT: 1 2 3 4 5

4 5 67 8

INPUT: N=21OUTPUT: 1 2 3 4 5 6

Page 18: Computer Project

P a g e | 18

6 7 8 10 11Algorithm:

1) Input a number.2) Two loops are used: The outer loop determines the value of the

first number ‘i’ of the sum and the inner loop finds the sum from ‘i’ to n for each iteration of outer loop, till sum becomes equal or greater than n.

3) The consecutive numbers are stored in a 1-D array for each iteration of the outer loop.

4) When the sum becomes equal to the number, n the consecutive numbers are printed from the array and the loop terminates. Else the next iteration takes place with an incremented value of ‘i’.

Source code:import java.util.*;class NumberCombinations{public static void main(String args[]){Scanner sc=new Scanner(System.in);System.out.println("Enter a number");int n=sc.nextInt(); //Accepting the numberint i,j,sum=0,index=0;int a[]=new int[n];System.out.println(“Output:”);for(i=1;i<n;i++){index=0;for(j=i;j<n;j++){ sum+=j; //Finding sum of consecutive numbers a[index]=j; //The numbers are stored in an array index++;

Page 19: Computer Project

P a g e | 19

if(sum>=n) break;}if(sum==n) //When the sum equals n, printing the array { for(j=0;j<index;j++) System.out.print(a[j]+" "); System.out.println();}sum=0; //Sum becomes 0 again for next iteration}}}

Output:1) Enter a number

9Output:2 3 4 4 5

2) Enter a number15Output:1 2 3 4 5 4 5 6 7 8

3) Enter a number21Output:1 2 3 4 5 6 6 7 8 10 11

Question 5Write a program that inputs the names of people into different arrays, A and B. Array A has N number of names, while array B has M number

Page 20: Computer Project

P a g e | 20

of names, with no duplicates in either of them. Merge arrays A and B into a single array C, such that the resulting array is sorted alphabetically.Test your program for the given data and random data.SAMPLE INPUT:

INPUT:Enter number of names in Array A, N=2Enter number of names in Array B, M=3First array: (A)

Suman Anil

Second array: (B)UshaSachin John

OUTPUT:

Sorted merged array: (C)AnilJohn SachinSumanUsha

Sorted first array: (A)AnilSuman

Sorted second array: (B)John SachinUsha

Algorithm:1) Accept sizes of both the arrays and create the arrays. 2) Accept the names in both arrays.3) Merge the names in both arrays into a third array.

Page 21: Computer Project

P a g e | 21

4) Sort all the three arrays and print them.

Source code:import java.util.*;class MergeArray{ public static void main(String args[]){Scanner sc=new Scanner(System.in);int i,j,c1=0,c2=0;System.out.println("Enter the number of names in Array A");int N=sc.nextInt(); System.out.println("Enter the number of names in Array B");int M=sc.nextInt();String A[]=new String[N]; //Creating the arraysString B[]=new String[M];int L=N+M;String tmp;String C[]=new String[L]; //Creating the combined array of size M+NSystem.out.println("First Array:");for(i=0;i<N;i++) //Inputting the names of first array{A[i]=sc.next();}System.out.println("Second Array:");for(i=0;i<M;i++) //Inputting names of second array{B[i]=sc.next();}for(i=0;i<N;i++) //Checking for repitition of names in arrays{for(j=i+1;j<N;j++){if(A[i].equalsIgnoreCase(A[j]))c1++;

Page 22: Computer Project

P a g e | 22

if(B[i].equalsIgnoreCase(B[j]))c2++;}}if(c1>0)System.out.println("Names are being repeated in first array");if(c2>0)System.out.println("Names are being repeated in second array");if(c1==0&&c2==0) //If no names are repeated, both arrays are combined{for(i=0;i<N;i++)C[i]=A[i]; //First array added to the merged arrayj=0;for(i=N;i<L;i++){if(j<M){C[i]=B[j]; //Second array added to merged arrayj++;}}for(i=0;i<L;i++) //Sorting the names of merged array{for(j=0;j<L-1-i;j++){if(C[j].compareTo(C[j+1])>0){tmp=C[j];C[j]=C[j+1];C[j+1]=tmp;}}}for(i=0;i<M;i++) //Sorting first array{

Page 23: Computer Project

P a g e | 23

for(j=0;j<M-1-i;j++){if(B[j].compareTo(B[j+1])>0){tmp=B[j];B[j]=B[j+1];B[j+1]=tmp;}}}for(i=0;i<N;i++) //Sorting second array{for(j=0;j<N-1-i;j++){if(A[j].compareTo(A[j+1])>0){tmp=A[j];A[j]=A[j+1];A[j+1]=tmp;}}}System.out.println("OUTPUT:");System.out.println("Sorted Merged Array: (C)");for(i=0;i<L;i++)System.out.println(C[i]);System.out.println("Sorted first Array: (A)");for(i=0;i<N;i++)System.out.println(A[i]);System.out.println("Sorted second Array: (B)");for(i=0;i<M;i++)System.out.println(B[i]);}}}

Page 24: Computer Project

P a g e | 24

Output:Enter the number of names in Array A2Enter the number of names in Array B3First Array:Suman AnilSecond Array:UshaSachinJohnOUTPUT:Sorted Merged Array: (C)AnilJohnSachinSumanUshaSorted first Array: (A)AnilSumanSorted second Array: (B)JohnSachinUsha

Page 25: Computer Project

P a g e | 25

Question 6A new advanced Operating System, incorporating the latest hi-tech features has been designed by Opera Computer Systems.The task of generating copy protection codes to prevent software piracy has been entrusted to Security Department.The Security Department has decided to have codes containing a jumbled combination of alternate uppercase letters of the alphabet starting from A up to K (namely among A, C, E, G, I, K). The code may or may not be in consecutive series of alphabets. Each code should not exceed 6 characters and there should not be repetition of characters. If it exceeds 6 characters, display an appropriate error message.Write a program to input a code and its length. At the first instance of an error display “Invalid!” stating the appropriate reason. In case of no error, display the message “Valid!”Test your program for the following data and some random data.SAMPLE DATA:

INPUT: N=4ABCE

OUTPUT: Invalid! Only alternate letters permitted!INPUT: N=4

AcIKOUTPUT: Invalid! Only upper case letters permitted!INPUT: N=4

AAKEOUTPUT: Invalid! Repetition of characters not permitted!INPUT: N=7OUTPUT: Error! Length of string should not exceed 6 characters!INPUT: N=4

AEGIKOUTPUT: Invalid! String length not same as specified!INPUT: N=3

ACE

Page 26: Computer Project

P a g e | 26

OUTPUT: Valid!INPUT: N=5

GEAIKOUTPUT: Valid!

ALGORITHM:1) Accept the length of string and if length is less than or equal to

six, accept the code.2) Check for the given conditions one by one.3) If the code satisfies all conditions, print positive result that it is

valid.

Source code:import java.util.*;class CopyProtection{public static void main(String args[]){Scanner sc=new Scanner(System.in);System.out.println("Enter the value of N");int N=sc.nextInt(); //Inputting length of stringif(N>6)System.out.println("Error! Length of string should not exceed 6 characters!");else if(N<=6) {System.out.println("Enter the code");String s=sc.next(); //Inputting the codeint l=s.length();int i,j,count=0;char c,ch;if(l!=N)System.out.println("Invalid! String length not same as specified!");else if(l==N){

Page 27: Computer Project

P a g e | 27

count=0;for(i=0;i<l;i++){c=s.charAt(i);if(Character.isUpperCase(c))count++; //Counting the number of characters which are in capital letters}if(count!=l) //If count is not equal to length, it means some letters are not in caps System.out.println("Invalid! Only uppercase letters permitted!");else {count=0;for(i=0;i<l;i++){c=s.charAt(i);for(j=i+1;j<l;j++){ ch=s.charAt(j); if(c==ch) count++; //Counting repeated characters}}if(count>0)System.out.println("Invalid! Repitition of characters not permitted!");else{for(i=0;i<l;i++){ c=s.charAt(i); if(c=='A'||c=='C'||c=='E'||c=='G'||c=='I'||c=='K') count++; }if(count!=l)System.out.println("Invalid! Only alternate letters permitted!");

Page 28: Computer Project

P a g e | 28

elseSystem.out.println("Valid");}}}}}}

Output:1) Enter the value of N

4Enter the code

ABCEOUTPUT:Invalid! Only alternate letters permitted!

2) Enter the value of N4Enter the codeAcIKOUTPUT:Invalid! Only uppercase letters permitted!

3) Enter the value of N4Enter the codeAAKEInvalid! Repitition of characters not permitted!

4) Enter the value of N7Error! Length of string should not exceed 6 characters!

5) Enter the value of N4Enter the codeAEGIKInvalid! String length not same as specified!

Page 29: Computer Project

P a g e | 29

6) Enter the value of N3Enter the codeACEValid

7) Enter the value of N4Enter the codeSUMIInvalid! Only alternate letters permitted!

Question 7Write a program to accept a date in the string format dd/mm/yyyy and accept the name of the day on 1st of January of the corresponding year. Find the day for the given date.Example:Input: Date : 5/7/2001

Day on first January : MONDAYOutput:

Day on 5/7/2001 : THURSDAYTest run the program on the following inputs:INPUT DATE DAY ON 1 ST JANUARY OUTPUT DAY FOR INPUT DATE4/9/1998 THURSDAY FRIDAY31/8/1999 FRIDAY TUESDAY6/12/2000 SATURDAY WEDNESDAYThe program should include the part for validating the inputs namely the date and the day on 1st January of that year.

ALGORITHM:1) Accept a date in the format dd/mm/yyyy and also the day of 1st of

January of that year.

Page 30: Computer Project

P a g e | 30

2) Use string tokenizer to separate the date, month and year from inputted date.

3) Create a 1-D array to store total number of days of each month. The number of days in ‘i’th month will be at index ‘i+1’ in the array.

4) Find the number of days from January 1st to the inputted date.5) When we divide the above result by 7, its reminder will give us

the number of that day in the week. For e.g.: It gives 1 for Monday and 0 for Sunday.

6) We can find the number of the day in the week of the day on 1st

January from array. 7) According to the above value and the value obtained from step 5,

we can find the day of the required date from the array.

Source code:import java.util.*;class FindDay{public static void main(String args[]){Scanner sc=new Scanner(System.in);System.out.println("Enter date in the format dd/mm/yyyy");String s=sc.nextLine();StringTokenizer st=new StringTokenizer(s,"/ "); /*Divide the inputted

date into three parts seperated by '/' */int date,month,year;date=Integer.parseInt(st.nextToken()); month=Integer.parseInt(st.nextToken()); year=Integer.parseInt(st.nextToken()); System.out.println("Enter day on first January");String day=sc.next();int i,j,sum=date,find,pos=0,index;int m[]={31,28,31,30,31,30,31,31,30,31,30,31}; /*Creating an array which

holds total number of days per month*/

Page 31: Computer Project

P a g e | 31

for(i=0;i<month-1;i++){sum+=m[i]; //Finding the number of days from january 1st to the inputted date}if(year%4==0&&month>2)sum++; //For leap yearfind=sum%7; //Divide the sum by 7 to find number of the day of the weekString d[];//Array to hold the days of a weekd[]={"MONDAY","TUESDAY","WEDNESDAY","THURSDAY","FRIDAY","SATURDAY","SUNDAY"};for(i=0;i<7;i++){if(d[i].equalsIgnoreCase(day)){ pos=i; //Finding the position of the inputted day in the array break;}}index=pos+(find-1); //Index of required dateif(index>7)index=index-7;System.out.print("Day on "+s+":");System.out.println(d[index]);}}

Page 32: Computer Project

P a g e | 32

Output:1) Enter date in the format dd/mm/yyyy

5/7/2001Enter day on first JanuaryMONDAYOUTPUT:Day on 5/7/2001: THURSDAY

2) Enter date in the format dd/mm/yyyy4/9/1998Enter day on first JanuaryTHURSDAYOUTPUT:Day on 4/9/1998: FRIDAY

3) Enter date in the format dd/mm/yyyy31/8/1999Enter day on first JanuaryFRIDAYOUTPUT:Day on 31/8/1999:TUESDAY

4) Enter date in the format dd/mm/yyyy6/12/2000Enter day on first JanuarySATURDAYOUTPUT:Day on 6/12/2000:WEDNESDAY

Question 8

Page 33: Computer Project

P a g e | 33

The input in this problem will consist of a number of lines of English text consisting of the letters of the English alphabet, the punctuation marks (‘) apostrophe, (.) full stop, (,) comma, (;) semicolon, (:) colon and white space characters (blank, new line). Your task is to print the words of the text in reverse order without any punctuation marks other than blanks.For example consider the following input text:This is a sample piece of text to illustrate this problem.If you are smart you will solve this right.The corresponding output would read as:Right this solve will you smart are you If problem this illustrate to text of piece sample a is ThisThat is the lines are printed in reverse order.Note: Individual words are not reversed.

Input format:The first line of input contains a single integer N(<=20), indicating the number of lines in the input. This is followed by N lines of input text. Each line should accept a maximum of 80 characters.Output format:Output the text containing the input lines in reverse order without punctuation except blanks as illustrated above. Test your program for the following data and some random data.

SAMPLE DATA:INPUT: 2

Emotions, controlled and directed to word is character.By Swami Vivekananda.

OUTPUT:Vivekananda Swami By character is word to directed and controlled Emotions.

INPUT: 1Do not judge a book by its cover.

OUTPUT:

Page 34: Computer Project

P a g e | 34

Cover its by book a judge not Do

ALGORITHM:1) Accept a string of ‘N’ lines.2) Convert the string of ‘N’ lines into a single string.3) Remove the separators of the string and add each word of the

string into an array.4) Print the array in reverse order i.e. the last element first and the

first element last.

Source code:import java.util.*;class Reverse{public static void main(String args[]){Scanner sc=new Scanner(System.in);System.out.println("Enter the number of lines");int N=sc.nextInt();System.out.println("Enter the statement");int i,l;String str="";String a[];for(i=1;i<=N;i++){ String s=sc.nextLine(); str=str+" "+s; //Converting the string of N lines into a single string}StringTokenizer st=new StringTokenizer(str,",. :;'");l=st.countTokens();a=new String[l];while(st.hasMoreTokens()){ for(i=0;i<l;i++) {

Page 35: Computer Project

P a g e | 35

a[i]=st.nextToken(); //Adding each word of the string into an array }}System.out.println(“OUTPUT:”);for(i=l-1;i>=0;i--){ System.out.print(a[i]+" "); //Printing the array in reverse order}System.out.print(“.”);}}

Output:1) Enter the number of lines

2Enter the statementEmotions, controlled and directed to word is character.By Swami Vivekananda.OUTPUT:Vivekananda Swami By character is word to directed and controlled Emotions.

2) Enter the number of lines1Enter the statementDo not judge a book by its cover.

Page 36: Computer Project

P a g e | 36

OUTPUT:cover its by book a judge not Do .

3) Enter the number of lines3Enter the statementMy name is Sumathi.I am studying in seventh day school.This is my computer project.OUTPUT:project computer my is This school day seventh in studying am I Sumathi is name My .

Question 9A unique-digit integer is a positive integer (without leading zeroes) 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<3000 and n<3000. 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, 109, 120FREQUENCY OF UNIQUE-DIGIT INTEGERS IS: 9

Page 37: Computer Project

P a g e | 37

ALGORITHM:1) Accept the range.2) Take the numbers in between the range one by one and convert

them into string type.3) Take each digit of one number and check whether it occurs more

than once.4) If no digits are repeated, print the number else pass on to the

next number.

Source code:import java.util.*;class UniqueDigitIntegers{public static void main(String args[]){Scanner sc=new Scanner(System.in);System.out.println("Enter the range");int m=sc.nextInt();int n=sc.nextInt();int i,j,k,count=0,f=0;String s;char d,ch;if(m<3000&&n<3000){System.out.println("THE UNIQUE-DIGIT INTEGERS ARE:");for(i=m;i<=n;i++){s=Integer.toString(i); //Converting the number into string typefor(j=0;j<s.length();j++){d=s.charAt(j); //Taking the digits one by one

Page 38: Computer Project

P a g e | 38

for(k=j+1;k<s.length();k++){ch=s.charAt(k);if(ch==d) //If any digit occurs more than once, count is incrementedcount++;}}if(count==0){System.out.print(i+", ");f++;}count=0;}System.out.println();System.out.println("FREQUENCY OF UNIQUE-DIGIT INTEGERS IS: "+f);}elseSystem.out.println("Range should not exceed 3000");}}Output:

1) Enter the range100120THE UNIQUE-DIGIT INTEGERS ARE:102, 103, 104, 105, 106, 107, 108, 109, 120,FREQUENCY OF UNIQUE-DIGIT INTEGERS IS: 9

2) Enter the range

10003500Range should not exceed 3000

Page 39: Computer Project

P a g e | 39

Question 10A 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) 666Prime factors are 2, 3, 3, and 37.Sum of the digits are (6+6+6) =18Sum of the digits of the factors (2+3+3+ (3+7)) =18

2) 4937775Prime factors are 3, 5, 5, 65837Sum of the digits are (4+9+3+7+7+7+5) =42Sum of 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 94 Output SMITH NumberInput 102 Output NOT SMITH NumberInput 666 Output SMITH NumberInput 999 Output NOT SMITH Number

ALGORITHM:1) Accept a number.2) Find its factors and find their sum.3) Find the sum of digits of the number.4) If sum of factors and sum of digits are equal, then the number is

smith number.

Source code:import java.util.*;

Page 40: Computer Project

P a g e | 40

class Smith{public static void main(String args[]){ Scanner sc=new Scanner(System.in); System.out.println("Enter a number"); int num=sc.nextInt(); Smith obj=new Smith(); //Creating an object System.out.println("OUTPUT:”); if(obj.sumFactors(num)==obj.sumDigits(num)) //Calling the two sub functions System.out.println(num+" is a smith number"); else System.out.println(num+" is not a smith number");}public int sumFactors(int n) //Function to find sum of the factors{

int i,index=0,d;int a[]=new int[n];int sumf=0;for(i=2;i<=n;i++){while(n%i==0){a[index]=i; index++;n=n/i;}}for(i=0;i<=index;i++){if(a[i]%10==0)sumf+=a[i];else{while(a[i]!=0)

Page 41: Computer Project

P a g e | 41

{d=a[i]%10;sumf+=d;a[i]/=10;}}}return sumf;

}public int sumDigits(int no) //Function to find sum of digits of the number{

int sumd=0,d;while(no!=0){d=no%10;sumd+=d;no/=10;}return sumd;

}}

Output:1) Enter a number

4937775OUTPUT:4937775 is a smith number

2) Enter a number94OUTPUT:94 is a smith number

3) Enter a number102OUTPUT:102 is not a smith number

Page 42: Computer Project

P a g e | 42

4) Enter a number666OUTPUT:666 is a smith number

5) Enter a number999OUTPUT:999 is not a smith number

Question 11A 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 block lettersWrite 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:

INPUTHELLO! HOW ARE YOU? HOPE EVERYTHING IS FINE. BEST OF

LUCK.OUTPUTSentence No. of Vowels No. of Words----------------------------------------------------------------- 1 2 1 2 5 3 3 8 4

Page 43: Computer Project

P a g e | 43

4 3 3Sentence No. of Words/Vowels-----------------------------------------------------------------

1 VVVVVVWWW

2 VVVVVVVVVVVVVVVWWWWWWWWW

3 VVVVVVVVVVVVVVVVVVVVVVVVWWWWWWWWWWWW

4 VVVVVVVVVWWWWWWWWW

-------------------------------------------------------------------Scale used: 1:3

ALGORITHM:1) Accept a group of sentences separated by “.”, “!” or “?” followed

by a space.2) Separate the sentences.3) For each sentence, count the number of words and number of

vowels in it and print the result in the required format.

Source code:import java.util.*;class VowelCount{public static void main(String args[]){Scanner sc=new Scanner(System.in);System.out.println("Enter a sentence");String s=sc.nextLine();s=s.toUpperCase();StringTokenizer st=new StringTokenizer(s,".,!?");int sent=st.countTokens();String sen,vow;char ch;

Page 44: Computer Project

P a g e | 44

int i,j,k,words,vowels=0;System.out.println("OUTPUT:”);System.out.println("Sentence\tNo.of Vowels\tNo.of words");System.out.println("---------------------------------------------");for(i=1;i<=sent;i++){sen=st.nextToken(); //Seperating the sentencesStringTokenizer str=new StringTokenizer(sen);words=str.countTokens(); //Counting the number of wordsvowels=0;for(k=0;k<sen.length();k++){ch=sen.charAt(k);if(ch=='A'||ch=='E'||ch=='I'||ch=='O'||ch=='U')vowels++; //Counting number of vowels}System.out.println(i+"\t\t "+vowels+"\t\t "+words);}System.out.println("Sentence\tNo.of words/vowels");System.out.println("------------------------------------");StringTokenizer stri=new StringTokenizer(s,".,!?");for(i=1;i<=sent;i++){vow=stri.nextToken();vowels=0;for(k=0;k<vow.length();k++){ch=vow.charAt(k);if(ch=='A'||ch=='E'||ch=='I'||ch=='O'||ch=='U')vowels++; }System.out.print(i+"\t");for(j=1;j<=vowels;j++){System.out.print("VVV");}

Page 45: Computer Project

P a g e | 45

System.out.println();System.out.print("\t");StringTokenizer string=new StringTokenizer(vow);words=string.countTokens();for(j=1;j<=words;j++){System.out.print("WWW");}System.out.println();}System.out.println("-------------------------------------");System.out.println("Scale used=1:3");}}

Output:

1) Enter a sentenceHELLO! HOW ARE YOU? HOPE EVERYTHING IS FINE. BEST OF LUCK.

OUTPUT:Sentence No.of Vowels No.of words---------------------------------------------1 2 12 5 33 8 44 3 3Sentence No.of words/vowels------------------------------------1 VVVVVV

WWW2 VVVVVVVVVVVVVVV

WWWWWWWWW

Page 46: Computer Project

P a g e | 46

3 VVVVVVVVVVVVVVVVVVVVVVVVWWWWWWWWWWWW

4 VVVVVVVVVWWWWWWWWW

-------------------------------------Scale used=1:3

2) Enter a sentence

This is a program to count number of vowels! It also counts the number of words in it.OUTPUT:Sentence No.of Vowels No.of words---------------------------------------------1 13 92 12 9Sentence No.of words/vowels------------------------------------1 VVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVV

WWWWWWWWWWWWWWWWWWWWWWWWWWW2 VVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVV

WWWWWWWWWWWWWWWWWWWWWWWWWWW------------------------------------- Scale used=1:3

Question 12Given 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 tasks:

1) Display the original matrix.2) Print the row and column position of the largest element of the

matrix.

Page 47: Computer Project

P a g e | 47

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=3List[ ][ ] 5 1 3

7 4 69 8 2

OUTPUT:5 1 37 4 69 8 2

The largest element 9 is in row 3 and column 1The second largest element 8 is in row 3 and column 2Sorted List

1 3 54 6 72 8 9

ALGORITHM:1) Accept the elements of the grid and print the original grid.2) Find the largest element of the grid and find its position in the

grid.3) Similarly find the second largest element and print the result.4) Sort the array and print the sorted array.

Source code:import java.util.*;class SortArray{

Page 48: Computer Project

P a g e | 48

public static void main(String args[]){Scanner sc=new Scanner(System.in);System.out.println("Enter the dimension of the square matrix");int n=sc.nextInt();int list[][]=new int[n][n];int s=n*n;int a[]=new int[s];int i,j,in,large,sec;int tmp,r=0,c=0;System.out.println("Enter the elements");in=0;for(i=0;i<n;i++){for(j=0;j<n;j++){list[i][j]=sc.nextInt();a[in]=list[i][j]; //Adding the elements into a single array alsoin++;}}System.out.println("OUTPUT:");for(i=0;i<n;i++){for(j=0;j<n;j++){System.out.print(list[i][j]+"\t"); //Printing the original grid}System.out.println();}large=a[0];for(i=0;i<s;i++) //Finding the largest element{if(a[i]>large)large=a[i];}

Page 49: Computer Project

P a g e | 49

sec=a[0];for(i=0;i<s;i++) //Finding second largest element{if(a[i]>sec&&a[i]<large)sec=a[i];}for(i=0;i<n;i++){for(j=0;j<n;j++){if(list[i][j]==large){r=i+1; //Finding position of largest element in the gridc=j+1;break;}}}System.out.println("The largest element "+large+" is in row "+r+" and column "+c);

for(i=0;i<n;i++){for(j=0;j<n;j++){if(list[i][j]==sec) //Finding position of second largest element in the grid{r=i+1;c=j+1;break;}}}

System.out.println("The second largest element "+sec+" is in row "+r+" and column "+c);

r=0;

Page 50: Computer Project

P a g e | 50

System.out.println("Sorted list:");while(r<n){for(i=0;i<n;i++) //Sorting the grid{for(j=0;j<n-1;j++){if(list[r][j]>list[r][j+1]){tmp=list[r][j];list[r][j]=list[r][j+1];list[r][j+1]=tmp;}}}for(c=0;c<n;c++)System.out.print(list[r][c]+"\t");System.out.println();r++;}}}

Page 51: Computer Project

P a g e | 51

Output:Enter the dimension of the square matrix3

Enter the elements513746982

OUTPUT:5 1 37 4 69 8 2The largest element 9 is in row 3 and column 1The second largest element 8 is in row 3 and column 2Sorted list:1 3 54 6 72 8 9

Page 52: Computer Project

P a g e | 52

Question 13Design a program to accept a day number (between 1 and 366), year (in 4 digits) from the user to generate and display the corresponding date. Also accept ‘N’ (1<=N<=100) from the user to compute and display future date corresponding to ‘N’ days after the generated date. Display error message if the value of the day number, year and N are not within the limit or according to the condition specified.Test your program for the following data and some random data.Example:

1. INPUT: DAY NUMBER : 233YEAR : 2008DATE AFTER (N) : 17OUTPUT:20 TH AUGUST 2008DATE AFTER 17 DAYS : 6 TH SEPTEMBER 2008

2. INPUT: DAY NUMBER : 360YEAR : 2008DATE AFTER (N) : 45OUTPUT:25 TH DDECEMBER 2008DATE AFTER 45 DAYS : 8 TH FEBRUARY 2009

ALGORITHM:1) Accept the required inputs: ‘day’=number of day for which date

is to be found out, ‘year’, and ‘n’=number of days after ‘day’.

Page 53: Computer Project

P a g e | 53

2) Create two arrays: one to store the total number of days in a month and the other to store the names of the month.

3) If the year is a leap year, 2nd month should have 29 days. If ‘day’ is greater than 365, year is incremented by 1 and day is decreased by 365.

4) We find the sum of total number of days in each month from the array till it becomes greater than or equal to day.

5) The position of the last element of the sum gives the position of the month’s name in the array.

6) The ‘day’ subtracted by the difference of sum and the total number of days in that month gives our required date.

7) Steps 3 to 6 are repeated for day=day+n.

Source code:import java.util.*;class FindDate{public static void main(String args[]){Scanner sc=new Scanner(System.in);System.out.println("DAY NUMBER:");int day=sc.nextInt();System.out.println("YEAR:");int year=sc.nextInt();System.out.println("DAY AFTER(N):");int n=sc.nextInt();System.out.println("OUPUT:”);int i,sum=0,pos=0,date,num=1;String mon;if(day<=366&&n<=100&&n>=1){int month[]={31,28,31,30,31,30,31,31,30,31,30,31}; /*Total number of days in

a month*/if(year%4==0)

Page 54: Computer Project

P a g e | 54

month[1]=29; //Leap year has 29 days on februaryString m[]={"JANUARY","FEBRUARY","MARCH","APRIL","MAY","JUNE","JULY", "AUGUST","SEPTEMBER","OCTOBER","NOVEMBER","DECEMBER"};

//Names of the monthswhile(num<=2){if(day>365&&month[1]==28) {day=day-365;year++; //If the number of days exceeds 365, it means that it is next year}if(day>366&&month[1]==29) //Same case for leap year{day=day-366;year++;}for(i=0;i<12;i++){sum+=month[i]; if(sum>=day){pos=i; //position of the month is obtainedbreak;}}mon=m[pos];date=day-(sum-month[pos]); //finding dateif(date>month[pos]){mon=m[pos+1]; //Finding the monthdate=date-month[pos];}if(num==2)System.out.println("DAY AFTER "+n+" DAYS:");

Page 55: Computer Project

P a g e | 55

System.out.println(date+"TH "+mon+" "+year);num++;day=day+n; //Next iteration to find the date after n dayssum=0;}}else if(n>100||n<1)System.out.println("N should not be greater than 100 or less than 1");else if(day>366)System.out.println("Day should be less than 366");}}

Output:1) DAY NUMBER:

233YEAR:2008DAY AFTER(N):17OUTPUT:20TH AUGUST 2008DAY AFTER 17 DAYS:6TH SEPTEMBER 2008

2) DAY NUMBER:360YEAR:2008DAY AFTER(N):45OUTPUT:25TH DECEMBER 2008

Page 56: Computer Project

P a g e | 56

DAY AFTER 45 DAYS:8TH FEBRUARY 2009

3) DAY NUMBER:300YEAR:2014DAY AFTER(N):2027TH OCTOBER 2014DAY AFTER 20 DAYS:16TH NOVEMBER 2014

Question 14Write a program to declare a matrix A[ ][ ] of order (m*n) where ‘m’ is the number of rows and ‘n’ is the number of columns such that both m and n must be greater than 2 and less than 20. Allow the user to input positive integers into this matrix. Perform the following tasks on the matrix.

a) Sort the elements of the outer row and column elements in ascending order using any standard sorting technique.

b) Calculate the sum of the outer row and column elements. c) Output the original matrix, rearranged matrix and only the

boundary elements of the rearranged array with their sum.Test your program for the following data and some random data.

1. Example:INPUT: M=3

N=31 7 48 2 56 3 9

OUTPUT:ORIGINAL MATRIX:

1 7 48 2 5

Page 57: Computer Project

P a g e | 57

6 3 9REARRANGED MATRIX:

1 3 49 2 58 7 6

BOUNDARY ELEMENTS:1 3 49 58 7 6

SUM OF THE OUTER ROW AND COLUMN ELEMENTS= 432. Example:

INPUT: M=2N=3

7 1 68 9 2

OUTPUT:ORIGINAL MATRIX:

7 1 68 9 2

REARRANGED MATRIX:1 2 69 8 7

BOUNDARY ELEMENTS:1 2 69 8 7

SUM OF THE OUTER ROW AND COLUMN ELEMENTS = 333. Example:

INPUT: M=4N=4

9 2 1 58 13 8 415 6 3 117 12 23 8

OUTPUT:ORIGINAL MATRIX:

9 2 1 58 13 8 415 6 3 11

Page 58: Computer Project

P a g e | 58

7 12 23 8

REARRANGED MATRIX:1 2 4 523 13 8 715 6 3 812 11 9 8

BOUNDARY ELEMENTS:1 2 4 523 715 812 11 9 8

ALGORITHM:1) Accept the array.2) Add the boundary elements into a single 1-D array.3) Sort the elements of the 1-D array.4) Print the rearranged matrix.5) Print only the boundary elements. 6) Find the sum of boundary elements and print it.

Source code:import java.util.*;class Boundary{public static void main(String args[]){Scanner sc=new Scanner(System.in);System.out.println("Enter the number of rows");int M=sc.nextInt();System.out.println("Enter the number of columns");int N=sc.nextInt();int A[][]=new int[M][N];int s=M*N,sum=0;

Page 59: Computer Project

P a g e | 59

int a[]=new int[s];String b[][]=new String[M][N];int i,j,tmp,index=-1;String str[]=new String[s];System.out.println("Enter the array elements");for(i=0;i<M;i++){for(j=0;j<N;j++){ A[i][j]=sc.nextInt();}}System.out.println("ORIGINAL MATRIX"); //Printing original matrixfor(i=0;i<M;i++){for(j=0;j<N;j++){ System.out.print(A[i][j]+"\t");}System.out.println();}for(i=0;i<N;i++){index++;a[index]=A[0][i]; //Elements of 1st row added to 1-D matrix}for(i=1;i<M;i++){index++;a[index]=A[i][N-1]; //Elements of last column added to 1-D matrix}for(i=N-2;i>=0;i--){index++;a[index]=A[M-1][i]; //Elements of last row added to 1-D matrix}

Page 60: Computer Project

P a g e | 60

for(i=M-2;i>0;i--){index++;a[index]=A[i][0]; //Elements of 1st column also added to 1-D matrix}for(i=0;i<=index;i++) //Sorting the elements of boundary{for(j=0;j<=index-1-i;j++){ if(a[j]>a[j+1]) { tmp=a[j]; a[j]=a[j+1]; a[j+1]=tmp; }}}System.out.println("REARRANGED MATRIX"); //Printing rearranged matrixindex=-1;for(i=0;i<N;i++){index++;A[0][i]=a[index];}for(i=1;i<M;i++){index++;A[i][N-1]=a[index];}for(i=N-2;i>=0;i--){index++;A[M-1][i]=a[index]; }for(i=M-2;i>0;i--)

Page 61: Computer Project

P a g e | 61

{index++;A[i][0]=a[index];}for(i=0;i<M;i++){for(j=0;j<N;j++){ System.out.print(A[i][j]+"\t");}System.out.println();}for(i=0;i<=index;i++){str[i]=Integer.toString(a[i]);}for(i=0;i<M;i++){for(j=0;j<N;j++){ b[i][j]=" ";}}index=-1;for(i=0;i<N;i++){index++;b[0][i]=str[index];}for(i=1;i<M;i++){index++;b[i][N-1]=str[index];}for(i=N-2;i>=0;i--){

Page 62: Computer Project

P a g e | 62

index++;b[M-1][i]=str[index];}for(i=M-2;i>0;i--){index++;b[i][0]=str[index];}System.out.println("BOUNDARY ELEMENTS"); //printing boundary elementsfor(i=0;i<M;i++){for(j=0;j<N;j++){ System.out.print(b[i][j]+"\t");}System.out.println();}for(i=0;i<=index;i++)sum+=a[i];System.out.println("SUM OF THE OUTER ROW AND COLUMN ELEMENTS="+sum);}}

Output:1) Enter the number of rows

3Enter the number of columns3Enter the array elements1748

Page 63: Computer Project

P a g e | 63

25639ORIGINAL MATRIX1 7 48 2 56 3 9REARRANGED MATRIX1 3 49 2 58 7 6BOUNDARY ELEMENTS1 3 49 58 7 6SUM OF THE OUTER ROW AND COLUMN ELEMENTS=43

2) Enter the number of rows2Enter the number of columns3Enter the array elements716892

ORIGINAL MATRIX7 1 68 9 2REARRANGED MATRIX

Page 64: Computer Project

P a g e | 64

1 2 69 8 7BOUNDARY ELEMENTS1 2 69 8 7SUM OF THE OUTER ROW AND COLUMN ELEMENTS=33

3) Enter the number of rows4Enter the number of columns4Enter the array elements921581384156311712238ORIGINAL MATRIX9 2 1 58 13 8 415 6 3 117 12 23 8REARRANGED MATRIX1 2 4 523 13 8 7

Page 65: Computer Project

P a g e | 65

15 6 3 812 11 9 8BOUNDARY ELEMENTS1 2 4 523 715 812 11 9 8SUM OF THE OUTER ROW AND COLUMN ELEMENTS=105

Question 15Read a single sentence which terminates with a full stop (.)The words are to be separated with a single blank space and are in lower case. Arrange the words contained in the sentence according to the length of the words in ascending order. If two words are of same length then the word occurring first in the sentence should come first. For both input and output the sentence must begin in upper case.Test your program for the following data and some random data.INPUT : The lines are printed in reverse order.OUTPUT : In the are lines order printed reverse.INPUT : Print the sentence in ascending order.

Page 66: Computer Project

P a g e | 66

OUTPUT : In the print order sentence ascending.INPUT : I love my country.OUTPUT : I my love country.ALGORITHM:

1) Accept a sentence.2) Take the words of the sentence one by one and store it in a string

array.3) Sort the array and print the array.

Source code:import java.util.*;class WordArrange{public static void main(String args[]){Scanner sc=new Scanner(System.in);System.out.println("Enter a sentence");String s=sc.nextLine();s=s.toLowerCase();int token,i,j;String str,tmp,sub;char ch;StringTokenizer st=new StringTokenizer(s," .");token=st.countTokens(); String a[]=new String[token];i=0;while(st.hasMoreTokens()){str=st.nextToken();a[i]=str; //Taking each word and saving in an arrayi++;}for(i=0;i<token;i++){for(j=0;j<token-1-i;j++)

Page 67: Computer Project

P a g e | 67

{if(a[j].length()>(a[j+1]).length()) //Sorting the array{ tmp=a[j]; a[j]=a[j+1]; a[j+1]=tmp;}}}tmp=a[0];ch=tmp.charAt(0);sub=tmp.substring(1);System.out.println(“OUTPUT:”);System.out.print(Character.toUpperCase(ch)+sub+" "); //First letter of first word to be printed in capsfor(i=1;i<token;i++){System.out.print(a[i]+" "); //Printing the arranged sentence}System.out.print(".");}}

Output:1) Enter a sentence

The lines are printed in reverse order.OUTPUT:In the are lines order printed reverse .

2) Enter a sentencePrint the sentence in ascending order.OUTPUT:In the print order sentence ascending .

3) Enter a sentenceI love my country.

Page 68: Computer Project

P a g e | 68

OUTPUT:I my love country .

Page 69: Computer Project

P a g e | 69

Question 16A bank intends to design a program to display the denomination of input amount up to 5 digits. The available denominations 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 in descending order of denominations. (i.e. preference should be given to the highest denominations available) along with the total number of notes. [Note: Only denominations used should be displayed]. Also print the amount in words according to the digits.Test your program for the following data and some random data.Example 1: Input: 14856Output: ONE FOUR EIGHT FIVE SIX

DENOMINATIONS:1000 x 14 = 14000500 x 1 = 500100 x 3 = 30050 x 1 = 505 x 1 = 51 x 1 = 1

TOTAL = 14856TOTAL NUMBER OF NOTES = 21

Example 2: Input: 6043Output: SIX ZERO FOUR THREE

DENOMINATIONS:1000 x 6 = 600020 X 1 = 202 x 1 = 21 x 1 = 1

TOTAL = 6043TOTAL NUMBER OF NOTES =10

Page 70: Computer Project

P a g e | 70

Example 3: Input: 235001Output: INVALID AMOUNT

ALGORITHM:

1) Accept an amount.2) Check whether the amount has more than 5 digits.3) In the first sub-function, the digits of the amount are taken one

by one and using switch case, the number-names are displayed.4) In the second sub-function, the denominations are stored in an

array.5) The amount is divided by each denomination and the quotient

and remainder is found out.6) The quotient is added into a sum variable and the difference

between amount and quotient is used as the amount for the next iteration of the denomination.

7) The denominations are displayed along with the number of notes i.e., sum.

Source code:import java.util.*;class Denominations{static int amt; //Static variablepublic static void main(String args[]){Scanner sc=new Scanner(System.in);System.out.println("Enter the amount");amt=sc.nextInt();if(Integer.toString(amt).length()>5) //Checking if number has more than 5 digitsSystem.out.println("INVALID AMOUNT");else{

Page 71: Computer Project

P a g e | 71

Denominations obj=new Denominations(); //Object creation statementobj.Digits(amt); //Calling function through objectobj.Deno(amt);}}public void Digits(int a){int d,rev=0,d1;String ar[]={"ZERO","ONE","TWO","THREE","FOUR","FIVE","SIX","SEVEN","EIGHT","NINE"};while(a!=0) {d=a%10;rev=rev*10+d; a/=10;}while(rev!=0){d1=rev%10;System.out.print(ar[d1]+" ");rev/=10;}System.out.println();}public void Deno(int am){int r,q,i=0,sum=0;int a[]={1000,500,100,50,20,10,5,2,1}; //Array to hold denominationsSystem.out.println("Denominations:");while(am!=0&&i<a.length){q=am/a[i]; //Finding quotientr=am%a[i]; //Remainder if(q!=0)System.out.println(a[i]+"\tx\t"+q+"\t=\t"+q*a[i]);

Page 72: Computer Project

P a g e | 72

sum+=q;am=am-(q*a[i]); //The difference is used as the amount for next iterationi++;}System.out.println("\t\tTOTAL\t=\t"+amt);System.out.println("TOTAL NUMBER OF NOTES\t=\t"+sum);}}

Output:1) Enter the amount

14856ONE FOUR EIGHT FIVE SIXDenominations:1000 x14 = 14000500 x1 = 500100 x3 = 30050 x1 = 505 x1 = 51 x1 = 1

TOTAL = 14856 TOTAL NUMBER OF NOTES = 21

2) Enter the amount6043SIX ZERO FOUR THREEDenominations:1000 x6 = 600020 x2 = 402 x1 = 21 x1 = 1

TOTAL = 6043TOTAL NUMBER OF NOTES = 10

3) Enter the amount235001

Page 73: Computer Project

P a g e | 73

INVALID AMOUNT

Question 17A positive whole number ‘n’ that has ‘d’ number of digits is squared and split into two pieces, a right-hand piece that has ‘d’ digits and a left-hand piece that has remaining ‘d’ or ‘d-1’ digits. If the sum of the two pieces is equal to the number, then ‘n’ is a Kaprekar Number. The first few Kaprekar Numbers are: 9, 45, 297……Example 1: 9 (here the value of d=1 i.e. number of digits=1)

92=81, right-hand piece of 81=1 and left-hand piece of 81=8

Sum = 1 + 8 = 9, i.e. equal to the number, so number 9 is kaprekar number.

Example 2: 45 (here the value of d=2 i.e. number of digits=2)452=2025, right-hand piece of 2025 = 25 and left-hand

piece of 2025 = 20Sum = 25 + 20 = 45, i.e. equal to the number, so number 45 is kaprekar number.

Example 3: 297 (here the value of d=3 i.e. number of digits=3)2972=88209, right-hand piece of 88209 = 209 and left-hand piece of 88209 = 88

Page 74: Computer Project

P a g e | 74

Sum = 209 + 88 = 297, i.e. equal to the number, so number 297 is kaprekar number.

Given 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).The input contains two positive integers p and q. Assume p<5000 and q<5000. You are to output the number of kaprekar numbers in the specified range along with their values in the format specified below:

SAMPLE DATA:INPUT: p=1 and q=1000OUTPUT: THE KAPREKAR NUMBERS ARE:- 1,9,45,55,99,297,703,999FREQUENCY OF KAPREKAR NUMBERS IS: 8

ALGORITHM:1) Accept a limit.2) Take the numbers in between the limit one by one and convert it

into string type and find its length ‘d’.3) Take the square and convert it to string type.4) Extract ‘d’ number of characters from the square of the number.

This is the right-hand piece and the remaining left-hand piece. 5) Add the right-hand piece and left-hand piece.6) If the sum becomes equal to the number itself, then print the

number.

Source code:import java.util.*;class Kaprekar{public static void main(String args[]){Scanner sc=new Scanner(System.in);System.out.println("Enter a limit");

Page 75: Computer Project

P a g e | 75

int p=sc.nextInt();int q=sc.nextInt();int f=0;System.out.println("THE KAPREKAR NUMBERS ARE:");for(int n=p;n<=q;n++){int rh=0,lh=0,sum=0,digit;int sq=n*n; //Finding squaredigit=0;for(int i=n;i>0;i/=10){digit++; //Count number of digits in n}rh=(int)(Math.round(sq%(Math.pow(10,digit)))); //last 'd' digits of square is found lh=(int)(Math.round(sq/Math.pow(10,digit))); /*Remaining digits are stored as left -hand piece*/sum=rh+lh; //Sum of left and right hand piecesif(sum==n){System.out.print(n+", "); //displaying kaprekar numbersf++; //Finding frequency of kaprekar numbers in the range}}System.out.println();System.out.println("FREQUENCY OF KAPREKAR NUMBERS:"+f);}}

Output:Enter a limit11000THE KAPREKAR NUMBERS ARE:1, 9, 45, 55, 99, 297, 703, 999,FREQUENCY OF KAPREKAR NUMBERS: 8

Page 76: Computer Project

P a g e | 76

Question 18Input a paragraph containing ‘n’ number of sentences where (1<=n<4). The words are to be separated with a single blank space and are in UPPER CASE. A sentence may be terminated either with a ‘.’ Or a question mark ‘?’ only. Any other character may be ignored. Perform the following operations:(i) Accept the number of sentences. If the number of sentences

exceeds the limit, an appropriate error message should be displayed.

(ii) Find the number of words in the whole paragraph.(iii) Display the words in ascending order of their frequency. Words

with the same frequency may appear in any order.Example 1: INPUT:

Enter the number of sentences.1Enter sentenceTO BE OR NOT TO BE.OUTPUT:Total number of words : 6WORD FREQUENCY OR 1 NOT 1 TO 2 BE 2

Example 2: INPUT:Enter the number of sentences.3Enter sentence

THIS IS A STRING PROGRAM.IS THIS EASY?YES IT IS.OUTPUT:Total number of words : 11WORD FREQUENCY A 1

Page 77: Computer Project

P a g e | 77

STRING 1 PROGRAM 1 EASY 1 YES 1 IT 1 THIS 2 IS 3

Example 3: INPUT:Enter the number of sentences.5INVALID ENTRY

ALGORITHM:1) Accept a sentence and put each word of the sentence into an

array.2) Remove all duplicate elements and put the words into another

array.3) Add the frequency of the corresponding word into another array.4) Sort the two arrays and print them.

Source code:import java.io.*;class Frequency{public static void main(String args[])throws IOException{InputStreamReader isr=new InputStreamReader(System.in);BufferedReader br=new BufferedReader(isr);System.out.println("Enter the number of sentences");int N=Integer.parseInt(br.readLine());if(N<1||N>=4){System.out.println("INVALID ENTRY");}else

Page 78: Computer Project

P a g e | 78

{System.out.println("Enter the sentences");String sent=br.readLine(); //Inputting the sentencesent=sent.toUpperCase();int n=0;String str,temp="";char ch;int h,d;String w[]=sent.split("[ .?]"); //Adding the words of the sentence into an arrayString word[]=new String[w.length];int f[]=new int[w.length]; //Array to store frequencyfor(int i=0;i<w.length;i++){h=0;d=0;for(int j=0;j<i;j++){if(w[i].compareTo(w[j])==0)h=1; //Checking for duplicate elements}if(h==0){for(int k=i;k<w.length;k++){if(w[i].compareTo(w[k])==0) d++;}word[n]=w[i]; //Adding the words without repitition into another array f[n]=d; //Adding the corresponding frequencies into another arrayn++;}}for(int i=0;i<n-1;i++) //Sorting the arrays{for(int j=i+1;j<n;j++)

Page 79: Computer Project

P a g e | 79

{if(f[i]>f[j]){int u=f[i];f[i]=f[j];f[j]=u;String s=word[i];word[i]=word[j];word[j]=s;}}}System.out.println("TOTAL NUMBER OF WORDS:"+w.length);System.out.println("WORD\t\tFREQUENCY");System.out.println("------------------------------");for(int i=0;i<n;i++) //Printing the arrays{System.out.println(word[i]+"\t\t"+f[i]);}}}}

Output:1) Enter the number of sentences

3Enter the sentencesTHIS IS A STRING PROGRAM.IS THIS EASY?YES IT IS.TOTAL NUMBER OF WORDS:11WORD FREQUENCY------------------------------A 1STRING 1PROGRAM 1EASY 1YES 1

Page 80: Computer Project

P a g e | 80

IT 1THIS 2IS 3

Question 19Write a program to input a natural number less than 1000 and display it in words. Test your program for the given sample data and some random dataINPUT: 29OUTPUT: TWENTY NINEINPUT: 17001OUTPUT: OUT OF RANGEINPUT: 119OUTPUT: ONE HUNDRED AND NINETEENINPUT: 500OUTPUT: FIVE HUNDREDALGORITHM:

1) Accept a number, find its length and if it greater than 3, display appropriate message.

2) Create three arrays: to store names of numbers if it occurs in one’s place, ten’s place and hundred’s place respectively.

3) Find out the number in one’s place and find its corresponding name from the one’s array.

4) Similarly find out the names of numbers in ten’s and hundred’s place from their respective arrays.

Source code:

Page 81: Computer Project

P a g e | 81

import java.util.*;class NumberName{public static void main(String args[]){Scanner sc=new Scanner(System.in);System.out.println("Input a number");int n=sc.nextInt();int i;String s=Integer.toString(n); //Converting n to string typeint d=s.length(); //Number of digits in nif(d>3){System.out.println("INVALID ENTRY");}else{String o[]={"ONE","TWO","THREE","FOUR","FIVE","SIX","SEVEN","EIGHT","NINE"}; //Array of names in ones placeString h[]={"TEN","ELEVEN","TWELVE","THIRTEEN","FOURTEEN","FIFTEEN","SIXTEEN","SEVENTEEN","EIGHTEEN","NINETEEN"};String t[]={"TWENTY","THIRTY","FOURTY","FIFTY","SIXTY","SEVENTY","EIGHTY","NINETY"}; //Ten's placeif(d==3) //For three digit numbers{i=n/100;System.out.print(o[i-1]+" HUNDRED"); //Naming the number in hundred's placeint ten=n%100;if(ten/10==1){System.out.print(" AND "+h[ten%10]); /*Naming number in ten's place if it is less than 20*/

Page 82: Computer Project

P a g e | 82

}else if(ten/10>1){System.out.print(" AND "+t[(ten/10)-2]); //Numbers in tens's place >20}if(ten%10!=0) //One's place{if(ten/10==0)System.out.print(" AND "+o[(ten%10)-1]);elseSystem.out.print(" "+o[(ten%10)-1]);}}}}}Output:

1) Input a number29TWENTY NINEInput a number17001INVALID ENTRYInput a number119ONE HUNDRED AND NINETEENInput a number500FIVE HUNDRED

Question 20Encryption 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)

Page 83: Computer Project

P a g e | 83

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:EXAMPLE 1:

INPUT: n=4IT 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.

EXAMPLE 2:INPUT: n=13OUTPUT: INVALID ENTRY

ALGORITHM:1) Accept the number of sentences and create an array with ‘n’ size.2) Accept the sentences and store it in array.3) Take the sentences one by one and store each word of the sentence

into another array using split() function.4) Now take each word and take its character one by one. Nested loops

are used for this purpose.5) For sentences in odd-rows, encrypt the words by converting the

characters to ASCII code and increment it by 2.6) For sentences in even-rows, display the array in reversed form.

Source code:

Page 84: Computer Project

P a g e | 84

import java.io.*;class Encryption{public static void main(String args[])throws IOException{InputStreamReader isr=new InputStreamReader(System.in);BufferedReader br=new BufferedReader(isr);System.out.println("Enter the number of sentences");int n=Integer.parseInt(br.readLine());if(n<1||n>10) //Checking conditionSystem.out.println("INVALID ENTRY");else{int i,j,k;String A[]=new String[n]; //Array to hold the sentencesString s,word;char c;int ch;System.out.println("Enter the sentences");for(i=0;i<n;i++)A[i]=br.readLine(); //Accept sentences and store in an arrayfor(i=0;i<n;i++){s=A[i]; //Taking the sentences one by ones=s.toUpperCase();String a[]=s.split("[ .]"); //Storing the words of the sentence into another arrayif((i+1)%2!=0) //For sentences in odd rows{ for(j=0;j<a.length;j++) {word=a[j]; //Taking words one by one from second arrayfor(k=0;k<word.length();k++){c=word.charAt(k); //Taking each character of wordif(c!='Y'&&c!='Z')

Page 85: Computer Project

P a g e | 85

{ch=(int)c;ch=ch+2; //The ASCII code ofcharacter at 2 places next to cword=word.replace(c,(char)ch);}else if(c=='Y') word=word.replace(c,'A'); //Replace 'Y' by 'A'elseword=word.replace(c,'B'); }System.out.print(word+" "); //Printing the encrypted word}System.out.println(".");}else //For even rows{for(j=a.length-1;j>=0;j--){System.out.print(a[j]+" ");}System.out.println(".");}}}}}

Output:1) Enter the number of sentences

4Enter the sentencesIT IS CLOUDY.IT MAY RAIN.THE WEATHER IS FINE.IT IS COOL.KV KU ENQWFA .

Page 86: Computer Project

P a g e | 86

RAIN MAY IT .VJG YICVJIT KU HKPG .COOL IS IT .

2) Enter the number of sentences13INVALID ENTRY

Question 21Design a program which accepts your date of birth in dd mm yyyy format. Check whether the date entered is valid or not. If it is valid, display “VALID DATE”; also compute and display the day number of the year for the date of birth. If it is invalid, display ”INVALID DATE” and then terminate the program.Test your program for the given sample data and some random data.Example 1:

INPUT: Enter your date of birth in dd mm yyyy format.05012010

OUTPUT: VALID DATE5

Example 2:INPUT: Enter your date of birth in dd mm yyyy format.

03042010

OUTPUT: VALID DATE93

Example 3:INPUT: Enter your date of birth in dd mm yyyy format.

34

Page 87: Computer Project

P a g e | 87

062010

OUTPUT: INVALID DATEALGORITHM:

1) Accept the date of birth.2) Store the total number of days in each month in an array.3) If the inputted date is less than or equal to the date stored in the

array at position (inputted month-1), it is valid date.4) If it is valid date, find sum of days from array till position

(inputted month-2) and add it to the inputted date. This is the day number.

Source code:import java.util.*;class BirthDate{public static void main(String args[]){Scanner sc=new Scanner(System.in);System.out.println("Enter date of birth in dd mm yyyy format");int dd=sc.nextInt(); //Dateint mm=sc.nextInt(); //Monthint yyyy=sc.nextInt(); //Year

int m[]={31,28,31,30,31,30,31,31,30,31,30,31}; /*Array storing total number of days in each month*/

int sum=0,i;if(yyyy%4==0) //For leap year, february should have 29 daysm[1]=29;if(dd<=m[mm-1]) //Checking validity of date{System.out.println("VALID DATE");for(i=0;i<mm-1;i++){sum+=m[i]; }

Page 88: Computer Project

P a g e | 88

System.out.println(sum+dd); //Number of days from 1st january}elseSystem.out.println("INVALID DATE");}}

Output:1) Enter date of birth in dd mm yyyy format05012010VALID DATE52) Enter date of birth in dd mm yyyy format

03042010VALID DATE93

3) Enter date of birth in dd mm yyyy format34062010INVALID DATE

4) Enter date of birth in dd mm yyyy format14091997VALID DATE257

Question 22A Prime-palindrome integer is a positive integer(without leading zeroes), which is prime as well as palindrome. Given two positive integers m and n, where m<n. Write a program to determine how

Page 89: Computer Project

P a g e | 89

many prime-palindrome integers are there in the range between m and n (both inclusive) and output them.The input contains two positive integers m and n, where m<3000 and n<3000. Display the number of prime-palindrome integers in the specified range along with their values in the format specified below:Example 1: INPUT: m=100 and n=1000

OUTPUT: THE PRIME PALINDROME INTEGERS ARE:

101,131,181,191,313,353,373,383,727,757,787,797,919,929 FREQUENCY OF PRIME PALINDROME INTEGERS: 15

Example 2: INPUT: m=100 and n=5000OUTPUT: OUT OF RANGE

ALGORITHM:1) Accept the limit.2) Take each number between the given limit one by one and check

whether it is prime or not.3) Only if the number is prime, reverse the number.4) If the reversed number is equal to the original number, print the

number and increment the count variable.5) Print the number of prime palindrome integers also.

Source code:import java.util.*;class PrimePalindrome{public static void main(String args[]){Scanner sc=new Scanner(System.in);System.out.println("Enter the range");int m=sc.nextInt();int n=sc.nextInt();int i,j,f,d,rev,num,c=0;if(m<3000&&n<3000){System.out.println("THE PRIME PALINDROME INTEGERS ARE:");for(i=m;i<=n;i++){f=0;

Page 90: Computer Project

P a g e | 90

for(j=1;j<=i;j++){if(i%j==0)f++; //Finding the number of factors}if(f==2) //If it is prime number{num=i;rev=0;while(num!=0) //Reversing the digits in the number{d=num%10;rev=rev*10+d;num/=10;}if(i==rev) //Checking if reversed number is equal to original number {System.out.print(i+",");c++; //Counting the prime palindrome numbers}}}System.out.println();System.out.println("FREQUENCY OF PRIME PALINDROME INTEGERS:"+c);}elseSystem.out.println("OUT OF RANGE");}}

Output:1) Enter the range

1001000THE PRIME PALINDROME INTEGERS ARE:101,131,151,181,191,313,353,373,383,727,757,787,797,919,929,FREQUENCY OF PRIME PALINDROME INTEGERS:15

Page 91: Computer Project

P a g e | 91

2) Enter the range1005000OUT OF RANGE

Question 23Write a program to accept a sentence as input. The words in the string are to be separated by the blank. Each word must be in upper case. The sentence is terminated by either “.”, “!” or “?”. Perform the following tasks:(i) Obtain the length of the sentence (measured in words).(ii)Arrange the sentence in alphabetical order of words.Test your program with following data values and also some random data:Example 1: INPUT: NECESSITY IS THE MOTHER OF INVENTION.

OUTPUT: LENGTH 6REARRANGED SENTENCE: INVENTION IS MOTHER NECESSITY OF THE.

Example 2: INPUT: BE GOOD TO OTHERS.OUTPUT: LENGTH 4

REARRANGED SENTENCE: BE GOOD OTHERS TO.

Page 92: Computer Project

P a g e | 92

ALGORITHM:1) Accept a sentence, convert it to upper case.2) Add the words of the sentence to an array.3) Count the number of words in the sentence from the array and print

it.4) Sort the array alphabetically and print the sorted array.

Source code:import java.io.*;class AlphabeticalOrder{public static void main(String args[])throws IOException{InputStreamReader isr=new InputStreamReader(System.in);BufferedReader br=new BufferedReader(isr);System.out.println("Enter the sentence");String s=br.readLine();s=s.toUpperCase(); //Converting the sentence to upper caseString str[]=s.split("[ !?.]"); //Adding each word of the sentence into an arrayint i,j;int l=str.length; //Number of words in the sentenceString temp;System.out.println("LENGTH: "+l);for(i=0;i<l;i++){for(j=i+1;j<l;j++){if(str[i].compareTo(str[j])>0) //Comparing the words lexicographically{ //Swapping the wordstemp=str[i]; str[i]=str[j];str[j]=temp;}

Page 93: Computer Project

P a g e | 93

}}for(i=0;i<l;i++){System.out.print(str[i]+" "); //Printing the sorted array}}}

Output:1) Enter the sentence

NECESSITY IS THE MOTHER OF INVENTION.LENGTH: 6INVENTION IS MOTHER NECESSITY OF THE

2) Enter the sentenceBE GOOD TO OTHERS.LENGTH: 4BE GOOD OTHERS TO

3) Enter the sentenceWhat is your name?LENGTH: 4IS NAME WHAT YOUR

4) Enter the sentenceTHE FLOWERS ARE VERY BEAUTIFUL!LENGTH: 5ARE BEAUTIFUL FLOWERS THE VERY

Page 94: Computer Project

P a g e | 94

Question 24Write a program to declare a matrix A[ ][ ] of order (MxN) where ‘M’ is the number of rows and ‘N’ is the number of columns such that both M and N must be greater than 2 and less than 20. Allow the user to input integers into this matrix. Perform the following tasks on the matrix:a) Display the input matrix.b) Find the maximum and minimum value in the matrix and display

them along with their positions.c) Sort the elements of the matrix in ascending order using any

standard sorting technique and rearrange them in the matrix.d) Output the rearranged matrix.Test your program with following data values and also some random data:Example 1: INPUT: M=3 and N=4

8 7 9 3-2 0 4 51 3 6 -4

OUTPUT: ORIGINAL MATRIX:8 7 9 3-2 0 4 51 3 6 -4

LARGEST NUMBER 9 ROW=0COLUMN=2

SMALLEST NUMBER -4 ROW=2COLUMN=3

REARRANGED MATRIX -4 -2 0 13 3 4 56 7 8 9

Example 2: INPUT: M=3 and N=22

Page 95: Computer Project

P a g e | 95

OUTPUT: SIZE OUT OF RANGE

ALGORITHM:1) Accept the dimension of the array and check if it is in the range.2) If dimension satisfies the range, create an array of size MxN and

accept the elements.3) Add the elements of the two dimensional array to a single

dimensional array.4) Sort the elements of the 1-D array in ascending order using

bubble sort technique.5) Find the first element of the sorted array and find its position in

the 2-D array. This is our smallest element.6) Find the last element of the sorted array and find its position in

the 2-D array. This is our largest element.7) Print the sorted array in a 2x2 matrix form.

Source code:import java.util.*;class Matrix{public static void main(String args[]){Scanner sc=new Scanner(System.in);System.out.println("Enter the dimensions of the matrix");int M=sc.nextInt();int N=sc.nextInt();if(M<=2||M>=20||N<=2||M>=20) System.out.println("SIZE OUT OF RANGE");else{int A[][]=new int[M][N];System.out.println("Enter the elements of matrix");int i,j;for(i=0;i<M;i++) //Accepting the elements

Page 96: Computer Project

P a g e | 96

{for(j=0;j<N;j++){A[i][j]=sc.nextInt();}}System.out.println("ORIGINAL MATRIX:");for(i=0;i<M;i++) //Printing original matrix{for(j=0;j<N;j++){System.out.print(A[i][j]+"\t");}System.out.println();}int a[]=new int[M*N]; int k=0,tmp;for(i=0;i<M;i++) //Converting 2-D array to 1-D array{for(j=0;j<N;j++){a[k]=A[i][j];k++;}}for(i=0;i<k;i++) //Sorting the elements using bubble sort{for(j=0;j<k-1-i;j++){if(a[j]>a[j+1]){tmp=a[j];a[j]=a[j+1];a[j+1]=tmp;}}

Page 97: Computer Project

P a g e | 97

}int max=a[k-1], min=a[0];for(i=0;i<M;i++){for(j=0;j<N;j++){if(A[i][j]==max) //Finding position of largest number in matrix{System.out.println("LARGEST NUMBER\t:"+max);System.out.println("\t\tROW= "+i+"\n\t\tCOLUMN= "+j);}if(A[i][j]==min) //Finding position of smallest number in matrix {System.out.println("SMALLEST NUMBER\t:"+min);System.out.println("\t\tROW= "+i+"\n\t\tCOLUMN= "+j);}}}System.out.println("REARRANGED MATRIX");k=0;for(i=0;i<M;i++) //Displaying sorted matrix{for(j=0;j<N;j++){System.out.print(a[k]+"\t");k++;}System.out.println();}}}}

Output:1) Enter the dimensions of the matrix

3

Page 98: Computer Project

P a g e | 98

4Enter the elements of matrix8793-2045136-4ORIGINAL MATRIX:8 7 9 3-2 0 4 51 3 6 -4LARGEST NUMBER :9

ROW= 0COLUMN= 2

SMALLEST NUMBER :-4ROW= 2COLUMN= 3

REARRANGED MATRIX-4 -2 0 13 3 4 56 7 8 9

2) Enter the dimensions of the matrix222SIZE OUT OF RANGE

Page 99: Computer Project

P a g e | 99

Question 25ISBN (International Standard Book Number) is a ten digit code which uniquely identifies a book.The first nine digits represent the Group, Publisher and Title of the book and the last digit is used to check whether ISBN is correct or not.Each of the first nine digits of the code can take a value between 0 and 9. Sometimes it is necessary to make the last digit equal to ten; this is done by writing the last digit of the code as X.To verify an ISBN, calculate 10 times the first digit, plus 9 times the second digit, plus 8 times the third and so on until we add 1 time the last digit. If the final number leaves no remainder when divided by 11, the code is a valid ISBN.For example:

1) 0201103311 = 10*0 + 9*2 + 8*0 + 7*1 + 6*1 + 5*0 + 4*3 + 3*3 + 2*1 + 1*1 = 55 Since 55 leaves no remainder when divisible by 11, hence it is a valid ISBN.

2) 007462542X = 10*0 + 9*0 + 8*7 + 7*4 + 6*6 + 5*2 + 4*5 + 3*4 + 2*2 + 1*10 = 176Since 176 leaves no remainder when divisible by 11, hence it is a

valid ISBN.3) 0112112425 = 10*0 + 9*1 + 8*1 + 7*2 + 6*1 + 5*1 + 4*2 + 3*4

+ 2*2 + 1*5 = 71Since 71 leaves a remainder when divisible by 11, hence it is not

a valid ISBN.

Page 100: Computer Project

P a g e | 100

Design a program to accept a ten digit code from the user. For an invalid input, display an appropriate message. Verify the code for its validity in the format specified below:Test your program with sample data and some random data.Example 1INPUT CODE : 0201530821OUTPUT : SUM = 99

LEAVES NO REMAINDER – VALID ISBN CODEExample 2INPUT CODE : 035680324OUTPUT : INVALID INPUTExample 3INPUT CODE : 0231428031OUTPUT : SUM = 122

LEAVES REMAINDER – INVALID ISBN CODEALGORITHM:

1) Accept the code and store it in a string variable.2) Convert the string to integer type.3) Check whether e last digit of the code is ‘X’ or some integer.4) Find the sum of first nine digits.5) If last digit is ‘X’, add 10 to the sum or if it is some integer, add

that integer to the sum.6) If the sum divided by 11 leaves no remainder, it is a valid ISBN

code else it is invalid.Source code:import java.io.*;class ISBN{public static void main(String args[])throws IOException{InputStreamReader isr=new InputStreamReader(System.in);BufferedReader br=new BufferedReader(isr);System.out.println("Enter the code");String I=br.readLine();int d,sum=0,i,prod,is;

Page 101: Computer Project

P a g e | 101

char ch;if(I.length()==10) //Finding the length of ISBN{ is=Integer.parseInt(I); for(i=1;i<9;i++) //Finding sum of digits of code upto second last digit { d=is%10; prod=d*i; sum+=prod; is=is/10; } if(I.charAt(9)=='X') //If last digit is X, it is equivalent to 10 sum+=10; //Adding 10 to sum i.e., value of 'X’ else { d=I.charAt(9); sum+=d; //Adding the last number to sum } System.out.println("SUM = "+sum); if(sum%11==0) System.out.println("LEAVES NO REMAINDER-VALID ISBN CODE"); else System.out.println("LEAVES REMAINDER-INVALID ISBN CODE");}elseSystem.out.println("INVALID INPUT");}}

Output:1) Enter the code

0201530821SUM = 99LEAVES NO REMAINDER-VALID ISBN CODE

Page 102: Computer Project

P a g e | 102

2) Enter the code035680324INVALID INPUT

3) Enter the code0231428031SUM = 122LEAVES REMAINDER-INVALID ISBN CODE

Page 103: Computer Project

P a g e | 103

Question 26Write 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 20. Allow the user to input integers into this matrix. Display appropriate error message for an invalid input. Perform the following tasks:

a) Display the input matrix.b) Create a mirror image matrix.c) Display the mirror image matrix.

Test your program with the sample data and some random data.Example 1: INPUT: M=3

4 16 128 2 144 1 3

OUTPUT: ORIGINAL MATRIX:4 16 128 2 144 1 3MIRROR IMAGE MATRIX:12 16 414 2 83 1 4

Example 1: INPUT: M=22OUTPUT: SIZE OUT OF RANGE

ALGORITHM:1) Accept a size M and check if it satisfies the condition.2) If true, create a square matrix of size M and accept the elements.3) Print the original matrix.4) Print the mirror image matrix, which is actually the original matrix

with the elements of each row displayed in the opposite order.

Source code:import java.util.*;class MirrorImage{

Page 104: Computer Project

P a g e | 104

public static void main(String args[]){Scanner sc=new Scanner(System.in);System.out.println("Enter the dimension of the array");int M=sc.nextInt();if(M<=2||M>=20)System.out.println("SIZE OUT OF RANGE");else{int A[][]=new int[M][M]; //Square matrix of size Mint i,j;System.out.println("Enter the elements");for(i=0;i<M;i++) //Accepting the elements{for(j=0;j<M;j++){A[i][j]=sc.nextInt();} }System.out.println("ORIGINAL MATRIX:");for(i=0;i<M;i++) //Printing original matrix{for(j=0;j<M;j++){System.out.print(A[i][j]+"\t");}System.out.println();}System.out.println("MIRROR IMAGE MATRIX:"); //Printing mirror image matrixfor(i=0;i<M;i++) {for(j=M-1;j>=0;j--) {System.out.print(A[i][j]+"\t"); //Elements in each row in reversed order}System.out.println();

Page 105: Computer Project

P a g e | 105

} } }}Output:Enter the dimension of the array3Enter the elements416128214413ORIGINAL MATRIX:4 16 128 2 144 1 3MIRROR IMAGE MATRIX:12 16 414 2 83 1 4Enter the dimension of the array22SIZE OUT OF RANGE Enter the dimension of the array3Enter the elements100010

Page 106: Computer Project

P a g e | 106

001ORIGINAL MATRIX:1 0 00 1 00 0 1MIRROR IMAGE MATRIX:0 0 10 1 01 0 0

Question 27A palindrome is a word that may be read the same way in either direction.Accept a sentence in UPPER CASE which is terminated by either “.”, “?” or “!”.Each word of the sentence is separated by a single blank space.Perform the following tasks:

(a) Display the count of palindromic words in the sentence.(b) Display the palindromic words in the sentence.

Example of palindromic words:MADAM, ARORA, NOON

Test your program with the sample data and some random data:Example 1

INPUT : MOM AND DAD ARE COMING AT NOON.OUTPUT : MOM DAD NOON

NUMBER OF PALINDROMIC WORDS : 3Example 2

INPUT : NITIN ARORA USES LIRIL SOAP.OUTPUT : NITIN ARORA LIRIL

NUMBER OF PALINDROMIC WORDS : 3Example 3

INPUT : HOW ARE YOU?OUTPUT : NO PALINDROMIC WORDS

Page 107: Computer Project

P a g e | 107

ALGORITHM:1) Accept a sentence and split into words by using StringTokenizer

class.2) Reverse each word and compare the reversed word with the

original word.3) If the original word and the reversed word are the same, then

print the word and increment the count variable.4) Print the number of palindromic words.

Source code:import java.util.*;class Palindrome{public static void main(String args[]){Scanner sc=new Scanner(System.in);System.out.println("Enter a sentence");String s=sc.nextLine();s=s.toUpperCase();StringTokenizer st=new StringTokenizer(s);String str="",pal="";char ch;int i,c=0;System.out.println(“OUTPUT:”);while(st.hasMoreTokens()){str=st.nextToken(); //Taking word by wordfor(i=0;i<str.length();i++){ch=str.charAt(i);pal=ch+pal; //Reversed word stored in pal}if(pal.equalsIgnoreCase(str)) //Checking if pal and the original word are equal{

Page 108: Computer Project

P a g e | 108

System.out.print(str+" ");c++; //Counting the number of palindromic words}pal="";}System.out.println();if(c==0)System.out.println("NO PALINDROMIC WORDS");elseSystem.out.println("NUMBER OF PALINDROMIC WORDS:"+c);}}Output:

1) Enter a sentenceMOM AND DAD ARE COMING AT NOONOUTPUT:MOM DAD NOON NUMBER OF PALINDROMIC WORDS:3

2) Enter a sentenceNITIN ARORA USES LIRIL SOAPOUTPUT:NITIN ARORA LIRIL NUMBER OF PALINDROMIC WORDS:3

3) Enter a sentenceHOW ARE YOU?OUTPUT:NO PALINDROMIC WORDS

Page 109: Computer Project

P a g e | 109

Question 28A Composite Magic Number is a positive integer which is composite as well as magic.Composite Number:A composite number is a number that has more than two factors.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 one.Example: 28= 2 + 8= 10 = 1 + 0 = 1Accept two 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.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 INTEGRS IS: 8Example 2: INPUT: m=1200 n=1300

OUTPUT: THE COMPOSITE MAGIC INTEGERS ARE:1207, 1216, 1225, 1234, 1243, 1252, 1261,

1270, 1288FREQUENCY OF COMPOSITE MAGIC INTEGRS IS: 9

Example 3: INPUT: m=120 n=99OUTPUT: INVALID INPUT.

ALGORITHM:1) Accept a limit and check if the lower limit is less than upper limit.2) Take the numbers from m to n in a loop.3) Find the number of factors of each number. If it is two(including

the number itself), then the number is prime.4) If the number is prime, check if it is magic number or not. If the

number obtained on successive addition is 1, it is magic number.5) Display the composite magic number and find the frequency of

composite magic numbers.

Page 110: Computer Project

P a g e | 110

Source code: import java.util.*;class CompositeMagic{public static void main(String args[]){Scanner sc=new Scanner(System.in);System.out.println("Enter the range"); int m=sc.nextInt(); //Inputting rangeint n=sc.nextInt();int i,j,count,c=0,num,d,sum=0;if(m<n) //Checking whether m is less than n{System.out.println("THE COMPOSITE MAGIC INTEGERS ARE:");for(i=m;i<=n;i++){count=0;for(j=2;j<i;j++){if(i%j==0) count++; //Counting the number of factors of the number}if(count>0) //If the number has more than one factors, it is prime{num=i;sum=0;while(num!=0) //Loop for finding sum of the number{d=num%10;sum+=d;num/=10;if(num==0&&sum>9) /*If the sum has more than one digit sum is used as the number for next iteration*/{ num=sum; sum=0;

Page 111: Computer Project

P a g e | 111

}}if(sum==1) //if it is a magic number also{System.out.print(i+",");c++; //Counting number of composite magic numbers}}}System.out.println();System.out.println("FREQUENCY OF COMPOSITE MAGIC NUMBERS IS:"+c);}else //If m is greater than nSystem.out.println("INVALID INPUT");}}

Output:1) Enter the range

10100THE COMPOSITE MAGIC INTEGERS ARE:10,28,46,55,64,82,91,100,FREQUENCY OF COMPOSITE MAGIC NUMBERS IS:8

2) Enter the range12001300THE COMPOSITE MAGIC INTEGERS ARE:1207, 1216,1225,1234,1243,1252,1261,1270,1288,FREQUENCY OF COMPOSITE MAGIC NUMBERS IS:9

3) Enter the range12099

Page 112: Computer Project

P a g e | 112

INVALID INPUT

Question 29Write a program to declare a square matrix A[ ] [ ] of order MxM where ‘M’ is the number of rows and 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 of the ‘i’th row and ‘j’th column is equal to element of ‘j’th row and ‘I’th column.

c) Find the sum of the elements of left diagonal and sum of elements of right diagonal of matrix and display them.

Example 1: INPUT: M=31 2 32 4 53 5 6

OUTPUT: ORIGIANL MATRIX:1 2 32 4 5

Page 113: Computer Project

P a g e | 113

3 5 6GIVEN MATRIX IS SYMMETRIC.SUM OF LEFT DIAGONAL: 11SUM OF RIGHT DIAGONAL: 10

Example 1: INPUT: M=47 8 9 24 5 6 38 5 3 17 6 4 2

OUTPUT: ORIGIANL MATRIX:7 8 9 24 5 6 38 5 3 17 6 4 2GIVEN MATRIX IS NOT SYMMETRIC.SUM OF LEFT DIAGONAL: 17SUM OF RIGHT DIAGONAL: 20

Example 1: INPUT: M=22OUTPUT: SIZE OUT OF RANGE.

ALGORITHM:1) Accept the dimension of the matrix and check if it is greater than

2 and less than 10.2) Create a square matrix of size ‘M’ and accept the elements into

it.3) Display the original matrix.4) Check if the element at ‘i’th row and ‘j’th column and the element

at ‘j’th row and ‘i’th column are equal.5) If so, display that the matrix is symmetric. Else it is unsymmetric.6) Find the sum of left and right diagonal and display it.

Source code: import java.util.*;class SymmetricMatrix{public static void main(String args[]){

Page 114: Computer Project

P a g e | 114

Scanner sc=new Scanner(System.in);System.out.println("Enter the dimensions");int M=sc.nextInt(); //Dimension of the matrixint i,j,count=0,sum=0;if(M>2&&M<10) //Checking the condition{int A[][]=new int[M][M];System.out.println("Enter the elements");for(i=0;i<M;i++){for(j=0;j<M;j++){A[i][j]=sc.nextInt(); //Inputting the numbers in the array}}System.out.println("ORIGINAL MATRIX");for(i=0;i<M;i++){for(j=0;j<M;j++){System.out.print(A[i][j]+"\t"); //Printing the original matrix}System.out.println();}for(i=0;i<M;i++){for(j=0;j<M;j++){if(A[j][i]==A[i][j]) //Checking if matrix is symmetriccount++; //Counting the number of elements which satisfy the symmetric condition}}if(count==M*M) //If all the elements satisfy symmetric conditionSystem.out.println("GIVEN MATRIX IS SYMMETRIC");else

Page 115: Computer Project

P a g e | 115

System.out.println("GIVEN MATRIX IS NOT SYMMETRIC ");for(j=0,i=0;j<M;j++,i++){sum+=A[j][i]; //Sum of left diagonal}System.out.println("SUM OF LEFT DIAGONAL:"+sum);sum=0;for(j=0,i=M-1;j<M;j++,i--){sum+=A[j][i]; //Sum of left diagonal}System.out.println("SUM OF RIGHT DIAGONAL:"+sum);}elseSystem.out.println("SIZE OUT OF RANGE");}}

Output:1) Enter the dimensions

3Enter the elements123245356ORIGINAL MATRIX1 2 32 4 53 5 6GIVEN MATRIX IS SYMMETRIC SUM OF LEFT DIAGONAL:11

Page 116: Computer Project

P a g e | 116

SUM OF RIGHT DIAGONAL:10

2) Enter the dimensions4Enter the elements7892456385317642

ORIGINAL MATRIX7 8 9 24 5 6 38 5 3 17 6 4 2GIVEN MATRIX IS NOT SYMMETRICSUM OF LEFT DIAGONAL:17SUM OF RIGHT DIAGONAL:20

3) Enter the dimensions22SIZE OUT OF RANGE

Page 117: Computer Project

P a g e | 117

Question 30Write a program to accept a sentence which may be terminated by either ‘?’ or ’.’ 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 the sentence and reduce all the extra blank space between two 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 with the sample data and some random data:Example 1: INPUT:

Page 118: Computer Project

P a g e | 118

A MORNING WALK IS A IS BLESSING FOR THE WHOLE DAY.WORD TO BE DELETED: ISWORD POSITION IN THE SENTENCE: 6

A MORNING WALK IS A BLESSING FOR THE WHOLE DAY.Example 2: INPUT:

AS YOU SOW, SO SO YOU REAP. WORD TO BE DELETED: SOWORD POSITION IN THE SENTENCE: 4 AS YOU SOW, SO YOU REAP.

Example 3: INPUT: STUDY WELL##OUTPUT: INVALID INPUT

ALGORITHM:

1) Accept a sentence and also the word to be deleted and also its position in the sentence.

2) Add the words of the sentence to an array and replace the word at the inputted sentence by an empty literal.

3) Print the remaining words in the array with only one space.

Source code: import java.util.*;class Delete{public static void main(String args[]){Scanner sc=new Scanner(System.in);System.out.println("Enter a sentence");String s=sc.nextLine();s=s.toUpperCase(); //Converting the string to upper case

Page 119: Computer Project

P a g e | 119

System.out.print("WORD TO BE DELETED:"); String word=sc.next(); //Accepting the word to be deleted

System.out.print("WORD POSITION IN SENTENCE:");int pos=sc.nextInt(); //Accepting the position of the word to be deleted

int tok,i;String a[]=s.split("[ .]+");a[pos-1]="";//Loop to print the words with a single blank space and without the word to be deleted

for(i=0;i<a.length;i++){if(i!=(pos-1))System.out.print(a[i]+" "); //Printing the word except the word at inputted position

}}}

Output:1) Enter a sentence

A MORNING WALK IS A IS BLESSING FOR THE WHOLE DAY.WORD TO BE DELETED:ISWORD POSITION IN SENTENCE:6A MORNING WALK IS A BLESSING FOR THE WHOLE DAY

2) Enter a sentenceAS YOU SOW, SO SO YOU REAP. WORD TO BE DELETED:SO

Page 120: Computer Project

P a g e | 120

WORD POSITION IN SENTENCE:4AS YOU SOW, SO YOU REAP

Question 31Write a program in java to input 10 different words and display the longest word and the length of the word.Sample Input: Computer

SchoolDelhiEducation…………..…………..

Sample Output: The longest word: …………………. The length of the word: ………………..

ALGORITHM:1) Accept ten words and store them in an array.2) Consider the first element as the largest word and compare its

length with that of the other elements.3) If there appears any element in the array longer than the first

word, then that word is considered as the longest word and the same procedure is followed.

4) The largest word is printed along with its length.

Page 121: Computer Project

P a g e | 121

Source code:import java.util.*;class LongestWord{public static void main(String args[]){Scanner sc=new Scanner(System.in);int i,l,large,pos;String a[]=new String[10];System.out.println("Enter 10 different words");for(i=0;i<10;i++){a[i]=sc.next(); //Accepting the words into an array}large=a[0].length(); //Consider the first element as the longest wordpos=0;for(i=0;i<10;i++){l=a[i].length(); if(l>large) //Checking with other elements{large=l; //Finding the largest elementpos=i;}}System.out.println("OUTPUT:”);System.out.println("The longest word:"+a[pos]);System.out.println("The length of the word:"+large);}}Output:

1) Enter 10 different wordsComputer ScienceIs

Page 122: Computer Project

P a g e | 122

MyFavouriteSubjectBecauseItIs EasyOUTPUT:The longest word: FavouriteThe length of the word:9

2) Enter 10 different words

JavaProgrammingLanguageBlueJEnvironmentInheritenceAbstractionPolymorphismEncapsulationClassThe longest word: EncapsulationThe length of the word:13

Page 123: Computer Project

P a g e | 123

Question 32Write a program in java to input a string in mixed case and find the frequency of each vowel.Sample Input : Delhi Public SchoolSample Output: The frequency of ‘a’ or ’A’: 0

The frequency of ‘e’ or ’E’: 2 The frequency of ‘i’ or ’I’: 2 The frequency of ‘o’ or ’O’: 2 The frequency of ‘u’ or ’U’: 1

ALGORITHM:1)Accept a string and convert it into upper case.2)Create two arrays: one to store the vowels and the other to count

the number of occurrence of a vowel at the corresponding position.

3)Print the vowels and their number of occurrence from the count array.

Source code:import java.io.*;class VowelFrequency{public static void main(String args[])throws IOException{InputStreamReader isr=new InputStreamReader(System.in);BufferedReader br=new BufferedReader(isr);System.out.println("Enter a sentence");String s=br.readLine();s=s.toUpperCase();int i,j;char ch;int count[]={0,0,0,0,0}; //Array to hold the frequency of each vowelchar a[]={'A','E','I','O','U'}; //Array to hold the vowelsfor(i=0;i<s.length();i++){

Page 124: Computer Project

P a g e | 124

ch=s.charAt(i);for(j=0;j<5;j++){if(ch==a[j]) //If the character is a vowel{ count[j]++; //Increment the number at the corresponding position in the count array}}}System.out.println("The frequeny of 'a' or 'A':"+count[0]);System.out.println("The frequeny of 'e' or 'E':"+count[1]);System.out.println("The frequeny of 'i' or 'I':"+count[2]);System.out.println("The frequeny of 'o' or 'O':"+count[3]);System.out.println("The frequeny of 'u' or 'U':"+count[4]);}}

Output:Enter a sentenceThe Delhi Public School.The frequeny of 'e' or 'E':2The frequeny of 'i' or 'I':2The frequeny of 'o' or 'O':2The frequeny of 'u' or 'U':1

Page 125: Computer Project

P a g e | 125

Question 33Write a program in java to accept a word and count the number of characters, which differ to make the word palindrome.Sample Input : DILIP

Reversed word : PILIDSample Output: Number of characters which differ to make

palindrome word=2ALGORITHM:

1) Accept a word and convert it to upper case.2) Reverse the word and find the number of characters which are

different in the original word and the reversed word.3) Display the reversed word and the number of words that differ to

make it palindrome.

Source code:import java.util.*;class ReversePalin{public static void main(String args[]){Scanner sc=new Scanner(System.in);System.out.println("Enter the word");String s=sc.next();s=s.toUpperCase(); //Converting word to upper casechar ch,c;String str="";int l=s.length(),i,count=0;for(i=0;i<l;i++){ch=s.charAt(i); str=ch+str; //Reversing the word}s=s.trim();str=str.trim();

Page 126: Computer Project

P a g e | 126

System.out.println("Reversed word:"+str);for(i=0;i<l;i++){ch=s.charAt(i);c=str.charAt(i);if(c!=ch) //Counting the number of words which differ to make it a palindromecount++;}System.out.println("No.of characters which differ to make Palindrome word:"+count);}}Output:

1) Enter the wordDILIPReversed word:PILIDNo.of characters which differ to make Palindrome word:2

2) Enter the wordSUMIReversed word:IMUSNo.of characters which differ to make Palindrome word:4

Page 127: Computer Project

P a g e | 127

Question 34Write a program in java to accept a name containing three words and write the name in short form with the initial.Sample Input : MAHINDRA SINGH DHONISample Output: M. S. DHONIALGORITHM:

1) Accept the full name and split it into different parts using StringTokenizer function.

2) Display the first character of each part except the last part.3) Display the last part as it is.

Source code:import java.util.*;class Initials{public static void main(String args[]){Scanner sc=new Scanner(System.in);System.out.println("Enter the name");String name=sc.nextLine();name=name.toUpperCase(); //Converting name to upper caseint token,i;String initials="",s;char init;StringTokenizer st=new StringTokenizer(name); token=st.countTokens();for(i=1;i<=token;i++){s=st.nextToken(); //Taking each part of the name one by oneif(i!=token){init=s.charAt(0); //The first character is displayedinitials+=init+".";}if(i==token) //The last part of the name

Page 128: Computer Project

P a g e | 128

{initials+=s;}}System.out.println(initials);}}Output:

1) Enter the nameMahindra Singh Dhoni.M.S.DHONI.

2) Enter the nameSAURAV GANGULY.S.GANGULY.

Question 35Write a program in java to input two strings. Display the words, whose frequency is more than one in the String. Sample Input:MAHINDRA SINGH DHONI IS THE CAPTAIN OF INDIAN CRICKET TEAM. SAURAV GANGULY IS THE EX-CAPTAIN OF THE INDIAN CRICKET TEAM. Sample Output: THE

OFINDIAN CRICKET TEAMIS

Page 129: Computer Project

P a g e | 129

ALGORITHM:1) Accept a sentence and convert to upper case.2)Count the number of words which are in common for both the

sentences and display them.Source code:import java.util.*;class Common{public static void main(String args[]){Scanner sc=new Scanner(System.in);System.out.println("Enter two strings");//Accepting two sentences and converting them to upper caseString s1=sc.nextLine();String s2=sc.nextLine();s1=s1.toUpperCase();s2=s2.toUpperCase();String str1,str2;int count;StringTokenizer st1=new StringTokenizer(s1," .");while(st1.hasMoreTokens()){count=0;str1=st1.nextToken(); //Words of the first sentenceStringTokenizer st2=new StringTokenizer(s2," .");while(st2.hasMoreTokens()){str2=st2.nextToken(); //Words of the second sentenceif(str1.equalsIgnoreCase(str2))count++; //Counting the number of words in common}if(count>=1) //If the word occurs in both sentences, print the wordSystem.out.println(str1);}}

Page 130: Computer Project

P a g e | 130

}Output:Enter two stringsMAHINDRA SINGH DHONI IS THE CAPTAIN OF INDIAN CRICKET TEAM.SAURAV GANGULY IS THE EX-CAPTAIN OF THE INDIAN CRICKET TEAMISTHEOFINDIANCRICKETTEAM