You are on page 1of 60

AIM: 1. Write a java program to find the sum of both diagonal elements of a matrix.

Sample Data:Enter the matrix : 5 3 7 2 1 4 8 7 2 The sum of first diagonal is: 8 The sum of second diagonal is: 16 ALGORITHM Step 1 Declare and initialize the required variables like i,j,a[] Step 2 Get the size of the matrix. Step 3 Get the elements of the matrix Step 4 Begin loop i from 0 to n-1 Step 6 Find the sum1 using the formula sum1=sum1+a[i][i]. Step 7Begin loop j from 0 to n-1 Step 8 Find the sum2 using the formula sum2=sum2+a[(n-j-1)][(n-j-1)]; Step 9 print sum1 and sum2 Step 10 End the process PROGRAM import java.io.*; class diagonalmatrix { public static void main(String args[])throws IOException { BufferedReader bf=new BufferedReader(new InputStreamReader(System.in)); System.out.println("enter the no: of columns"); int n=Integer.parseInt(bf.readLine()); int a[][]=new int[n][n] ; int i,j; System.out.println("enter the matrix"); for( i=0;i<n;i++) { for(j=0;j<n;j++) { a[i][j]=Integer.parseInt(bf.readLine()); } } int sum1=0; for(i=0;i<n;i++) { sum1=sum1+a[i][i]; } int sum2=0;

for(j=0;j<n;j++) { sum2=sum2+a[(n-j-1)][(n-j-1)]; } System.out.println("The sum of first diagonal is :"+sum1); System.out.println("The sum of second diagonal is:"+sum2); }} VARIABLE DESCRIPTION riable Name a[][] n sum1,sum2 i, j Data Type Integer array Integer Integer Integer, Integer Purpose To store the first array for multiplication To store the size of array To store the sum of the elements of the 2 diagonals Loop variables

OUTPUT enter the matrix dsg enter the no: of columns 3 enter the matrix 1 2 3 4 5 6 7 8 9 The sum of first diagonal is :15 The sum of second diagonal is:15

AIM: 2. Write a menu driven program to implement a 4-function calculator. The user should be able to select the options corresponding to the four operations, i.e. addition, subtraction, multiplication or division, and then enter the two numbers for the operation. The result of the calculation should be displayed as the output. The four operations should be done in four different functions. The arguments of the functions will be the two numbers on which the operation has to be performed and the result will be the return value. Sample data :Menu 1. Addition 2. Subtraction 3. Multiplication 4. Division 5. Exit Enter your choice (1, 2, 3, 4 or 5) 3 Enter the first number 5 Enter the second number 3 Product = 15 ALGORITHM Step 1- create a object named obj Step 2 Enter the choice Step 3 Enter the two numbers . Step 4 call function according to the given choice Step 6 execute the function and print the result Step 7call main function again Step 8 End the process if choice is 5

PROGRAM import java.io.*; class calculator { public void addition(int a,int b) { int sum=a+b; System.out.println("sum:"+sum); } public void subtraction(int a,int b) { int difference=a-b;

System.out.println(" difference:"+difference); } public void division(int a,int b) { int quotient=a/b; int remainder=a%b; System.out.println("the answer after dividing is:"+quotient+" "+remainder+"/"+b); } public void multiplication(int a,int b) { int product=a*b; System.out.println("product:"+product); } public static void main()throws IOException { calculator obj=new calculator(); BufferedReader bf=new BufferedReader(new InputStreamReader(System.in)); System.out.println(" 1.Addition"); System.out.println(" 2.Substraction"); System.out.println(" 3.Division"); System.out.println(" 4.Multiplication"); System.out.println(" 5.Exit"); System.out.println("enter your choice(1, 2, 3, 4 or 5) "); int ch=Integer.parseInt(bf.readLine()); if(ch!=5) { System.out.println("enter the 1st number"); int a=Integer.parseInt(bf.readLine()); System.out.println("enter the 2nd number"); int b=Integer.parseInt(bf.readLine()); if(ch==1) obj.addition(a,b); else if(ch==2) obj.subtraction(a,b); else if(ch==3) obj.division(a,b); else if(ch==4) obj.multiplication(a,b); obj.main(); } } } VARIABLE DESCRIPTION riable Name ch a,b Data Type Integer Integer Purpose To store the choice of the user To store the 2 numbers

OUTPUT 1.Addition 2.Substraction 3.Division 4.Multiplication 5.Exit enter your choice(1, 2, 3, 4 or 5) 3 enter the 1st number 5 enter the 2nd number 2 the answer after dividing is:2 1/2 1.Addition 2.Substraction 3.Division 4.Multiplication 5.Exit enter your choice(1, 2, 3, 4 or 5) 5

AIM: 4.Write a program to reverse all strings in an array Sample Input :Enter the string : Happy Birthday Sample Output :The changed string is : yppaH yadhtriB ALGORITHM Step 1 Declare and initialize the required variables like m,n,beg,I,k,pop,s[] Step 2 enter the string. Step 3 Begin loop i from 0 to length of the string Step 4 when i becomes equal to the length Step 6 the last word is reversed and printed Step 7 when the character at index is a space Step 8 each word is reversed and printed Step 9 End the process

PROGRAM import java.io.*; class hello { public static void main(String args[])throws IOException { hello obj=new hello(); int m=0; int beg=0; int i; String pop; String s[]=new String[50]; BufferedReader bf=new BufferedReader(new InputStreamReader(System.in)); System.out.println("Enter the sentence"); String str=bf.readLine(); for(i=0;i<=str.length();i++) { if(i==str.length()) { s[m]=str.substring(beg,i); String reverseStr = new StringBuffer(s[m]).reverse().toString(); System.out.print(reverseStr+" "); break; } else if (str.charAt(i)==' ') {

s[m]=str.substring(beg,i); String reverseStr = new StringBuffer(s[m]).reverse().toString(); System.out.print(reverseStr+" "); m++; beg=i; } } }} VARIABLE DESCRIPTION variable Name s[] m beg i, Data Type String array Integer Integer Integer Purpose To store the word in the input string in a string array To store the index of the array To store the index of the first letter of each word Loop variable

OUTPUT Enter the sentence my name is noel ym eman si leon

AIM: 5.Program to insert elements into a sorted array at an appropriate location Sample Data :Enter the number of elements in the array : 6 Enter the elements of the array : 3 5 6 8 11 14 Enter the element to be inserted : 9 The array after insertion of the element is : 3 5 6 8 9 11 14 ALGORITHM Step 1 Declare and initialize the required variables like and j. Step 2 enter the size of the array Step 3 Begin loop i from 0 to n-1 Step 4 Get the elements of the array Step 5 Begin loop i from 0 to n-1 Step 6 enter the number to be entered. Step 7 if the entered number is lesser than the number at index i, print array till there. Step 8 print the input number Step 9- print the rest of the array Step 10-end process PROGRAM import java.io.*; class pro5 { public static void main(String args[])throws IOException { int i,j; BufferedReader bf=new BufferedReader(new InputStreamReader(System.in)); System.out.println("enter the number of elements in the array"); int n=Integer.parseInt(bf.readLine()); int a[]=new int[n]; System.out.println("enter the elements of the array"); for(i=0;i<n;i++) { a[i]=Integer.parseInt(bf.readLine()); } System.out.println("enter the element to be inserted"); int x=Integer.parseInt(bf.readLine()); for(i=0;i<n;i++) {

if(a[i]>x) { int b[]=new int[(n+1)]; for(j=0;j<=(i-1);j++) { System.out.print(a[j]); } System.out.print(x); for(j=i;j<n;j++) { System.out.print(a[j]); }break;} }}} VARIABLE DESCRIPTION Variable Name a[] n x i, j Data Type Integer array Integer Integer Integer, Integer Purpose To store the array To store the number of elements in the array To enter the number Loop variables

OUTPUT enter the number of elements in the array 5 enter the elements of the array 1 6 7 5 9 enter the element to be inserted 4 146759

AIM: 6.Write a java program to print the tribonacci series (i.e. 0, 1, 2, 3, 6, 11, 20, 27). The number of terms required is to be passed as parameter ALGORITHM Step 1 Declare and initialize the required variables like a=0,b=1,c=2,d=3 Step 2 Enter the number of terms n( parameter). Step 3 print the first three numbers Step 4 Begin loop i from 4 to n and repeat step 6, and87 Step 5 find d by adding a, b and c Step 6 change the value of a, b and c to the next adjacent value Step 7 Print d. Step 8 End the process

PROGRAM public class pro6 { void func(int n) { int a=0, b=1, c= 2, d=0, i; System.out.print( a +", "+ b + ", "+c); for(i=4;i<=n;i++) { d=a+b+c; System.out.print("," + d); a=b; b=c; c=d; } } } VARIABLE DESCRIPTION Variable Name a,b,c d Data Type Integer Integer

Purpose To store the numbers that must b added to get next To store the value of the next number in the series

i,

Integer

Loop variable

OUTPUT (When n=10) 0, 1, 2,3,6,11,20,37,68,125

AIM: 7. Write a Java program to input a number and print its primordial, i.e. n#. [Primordial (P#) is defined to be the product of prime numbers less than or equal to p] Sample Data:Enter a number: 3 3# = 2 x 3 = 6 Enter a number: 13 13# = 2 x 3 x 5 x 7 x 11 x 13 = 30030 ALGORITHM Step 1 Declare and initialize the required variables like i, m=0, pro=1. Step 2 enter the number Step 3 Begin loop i from n to 1 and repeat steps 5,6 and 7 Step 5 Begin loop j from 2 to i-1 and repeat step 6 Step 6 when i is completely divisible by j ,increase m by 1. Step 7 if m is 0 then it is prime so multiply it with product Step 8 print product Step 9-End process

PROGRAM import java.io.*; class pro7 { public static void main(String args[])throws IOException { int i; int m=0; int pro=1; BufferedReader bf=new BufferedReader(new InputStreamReader(System.in)); System.out.println("enter a number"); int n=Integer.parseInt(bf.readLine()); for( i=n;i>0;i--) { for(int j=2;j<i;j++) { if((i%j)==0) m++; } if(m==0)

{pro=pro*i;} m=0;} System.out.println(pro); }} VARIABLE DESCRIPTION Variable Name n m Data Type Integer Integer Purpose To store the input number To store the number of numbers with which the input number can be divided To store the product Loop variables

pro i, j

Integer Integer, Integer

OUTPUT enter a number 13 30030

AIM: 8. A Smith number is a composite number, the sum of whose digits is the sum of prime factors obtained as a result of prime factorization (excluding 1). Examples of Smith numbers are 4, 22, 27, 58, 85, 94, 121,. Example : (i) 666 Prime factors are 2, 3, 3 and 37 Sum of digits = (6 + 6 + 6) = 18 Sum of digits of the factors = (2 + 3 + 3 + (3 + 7)) = 18 (ii) 4937775 Prime factors are 3, 5, 5 and 65837 Sum of digits = (4 + 9 + 3 + 7 + 7 + 7 + 5) = 42 Sum 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. (ISC Practical 2008) ALGORITHM Step 1 Declare and initialize the required variables like num,i,k,sum=0,sum1=1,numb,pro,n. Step 2 Enter the value as a parameter. Step 3 Get the elements of the two matrices. Step 4 Begin loop i from 2 to n Step 5 sum is now added with the new number(if the number is grater than 10,it is first broken apart before adding) Step 6 now sum of all the numbers in the input is added 1 by 1. Step 7 check if the 2 sums are the same Step 8 if yes print a positive test result ,otherwise negative Step 9-end process

PROGRAM class pro8 { public static void main(int n) { int num,i,k; int sum=0; int sum1=0;

int numb; int pro; num=n; for(i=2;i<=n;i++) { if(n%i==0) { if(i>10) { while(n!=0) { numb=n%10; sum=sum+numb; n=n/10; } } else{ sum=sum+i; n=n/i; i=2; } }} while(num!=0) { numb=num%10; sum1=sum1+numb; num=num/10; } if(sum1==sum) { System.out.println("it is a smith's number"); } else { System.out.println("it is not a smith's number"); }}}

VARIABLE DESCRIPTION Variable Name Sum Sum1 n, num i Data Type Integer Integer Integer ,Integer Integer Purpose To store the sum of the factors To store the sum of the numbers the input number To store the number Loop variable

numb

Integer

To store the number to be added 1by 1.

OUTPUT (when n is4937775) it is a smith's number

AIM: 9. Write 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: Display the input matrix. Find the maximum and minimum value in the matrix and display them along with their position. Sort the elements of the matrix in ascending order using any standard sorting technique and rearrange them in the matrix and then output the rearranged matrix.

Sample Data:M=3 N=4 Entered values: 8,7,9,3,-2,0,4,5,1,3,6,-4 Original matrix: 8 7 9 3 -2 0 4 5 1 3 6 -4 Largest Number: 9 Row: 0 Column: 2 Smallest Number: -4 Row=2 Column=3 Rearranged matrix: -4 -2 0 1 3 3 4 5 6 7 8 9 (ISC Practical 2012) ALGORITHM Step 1-declare the variables like r,c, maxi,maxj,mini,minj,i,j,m,n,arr[][],bool.

Step 2-get the number of columns and rows of the matrix.check if the matrix has required size. Step 3-input the elements in the array. Step 4-print the array, the smallest and largest number,and its index. Step 5-sort the array. Step 6-print the new array. PROGRAM import java.io.*; class Matrix { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); int arr[][]; int r,c,max,min,maxi,maxj,mini,minj,i,j,m,n; public void take()throws Exception { boolean bool=true; while(bool) { System.out.println("\nEnter the number of rows:"); r=Integer.parseInt(br.readLine()); System.out.println("\nEnter the number of columns:"); c=Integer.parseInt(br.readLine()); if(r<2 || c<2 || r>20 || c>20) bool=true; else bool=false; } arr=new int[r][c]; for(i=0;i< r;i++) { for(j=0;j< c;j++) { System.out.println("\nEnter Value:"); arr[i][j]=Integer.parseInt(br.readLine()); } } max=arr[0][0]; min=arr[0][0]; maxi=0; mini=0; maxj=0; minj=0; for(i=0;i< r;i++) { for(j=0;j< c;j++)

{ if(arr[i][j]>max) { max=arr[i][j]; maxi=i; maxj=j; } else if(arr[i][j]< min) { mini=i; minj=j; min=arr[i][j]; } } } System.out.println("\nOriginal Array\n"); for(i=0;i< r;i++) { for(j=0;j< c;j++) { System.out.print(arr[i][j]+" "); } System.out.println(); } System.out.println("\nMaximum Value="+max); System.out.println("\nRow="+maxi); System.out.println("\nColumn="+maxj); System.out.println("\nMinimum Value="+min); System.out.println("\nRow="+mini); System.out.println("\nColumn="+minj); for(m=0;m< r;m++) { for(n=0;n< c;n++) { for(i=0;i< r;i++) { for(j=0;j< c;j++) { if(arr[m][n]< arr[i][j]) { min=arr[m][n]; arr[m][n]=arr[i][j]; arr[i][j]=min; } } } } }

System.out.println("\nSorted Array\n"); for(i=0;i< r;i++) { for(j=0;j< c;j++) { System.out.print(arr[i][j]+" "); } System.out.println(); } } public static void main(String args[]) throws Exception { Matrix ob=new Matrix(); ob.take(); } }

VARIABLE DESCRIPTION Variable Name r c arr[][] i, j Maxi,maxj Mini, minj min Data Type Integer Integer Integer array Integer, Integer Integer, Integer Integer, Integer Integer Purpose To store the number of rows To store the number of columns To store the sum of input matrix Loop variables To store the index of the maximum value To store the index of the minimum value To store temporary variable while sorting

OUTPUT

Enter the number of rows: 2 Enter the number of columns: 2 Enter Value: 2

Enter Value: 1 Enter Value: 3 Enter Value: 4 Original Array 21 34 Maximum Value=4 Row=1 Column=1 Minimum Value=1 Row=0 Column=1 Sorted Array 12 34

AIM: 10. A prime palindrome integer is a positive integer (without leading zeros) which is prime as well as a palindrome. Given two positive integers m and n, where m < n, write a program to determine how 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: Test your program with the sample data and some random data: Sample Data:INPUT: m=100 n=1000 OUTPUT: The prime palindrome integers are: 101,131,151,181,191,313,351,373,383,727,757,787,797,919,929 Frequency of prime palindrome integers: 15 INPUT: m=100 n=5000 OUTPUT: Out of Range (ISC Practical 2012) ALGORITHM Step 1-initailise variables like m,n and c. Step 2-input and check the range. Step 3-begin loop in the given range. Step 4- check if the number is a prime number. Step 5-reverse the number and check if they are palindromes. Step 6-if yes,print the numbers. Step 7-end process.

PROGRAM import java.io.*; class PalPrime { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); int m,n; int c; public void showPalPrime() throws Exception { c=0; System.out.println("Enter the Lower Limit:"); m=Integer.parseInt(br.readLine()); System.out.println("Enter the Upper Limit:"); n=Integer.parseInt(br.readLine()); if(m>=n || n>=3000) System.out.println("Out of Range."); else { System.out.println("The Prime Palindrome integers are:"); while(m<=n) { if(palPrime(m)) { if(c==0) System.out.print(m); else System.out.print(", "+m); c++; } m++; } System.out.println("\nFrequency of Prime Palindrome integers: "+c); } } private boolean palPrime(int x) { boolean bool=false; int i; int rev=0; for(i=2;i< x;i++) { if(x%i==0) break;

} if(i==x) bool=true; if(bool) { for(i=x;i>0;i=i/10) { rev=rev*10+i%10; } if(rev!=x) bool=false; } return bool; } public static void main(String args[]) throws Exception { PalPrime ob=new PalPrime (); ob.showPalPrime(); } } VARIABLE DESCRIPTION Variable Name m,n c sum[][] i, j rev m2,n2 Data Type Integer Integer Integer array Integer, Integer Integer Integer, Integer Purpose To store the upper and lower limit To store the frequency of the prime palindrome integers To store the sum of the two matrices entered Loop variables To store the reversed number To store the size of second array

OUTPUT Enter the Lower Limit: 100 Enter the Upper Limit: 1000 The 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

AIM: 12. A bank intends to design a program to display the denomination of an input amount, upto 5 digits. The available denomination with the bank are of rupees 1000,500,100,50,20,10,5,2 and 1. Design a program to accept the amount from the user and display the break-up in descending order of denominations. (i,e preference should be given to the highest denomination available) along with the total number of notes. [Note: only the denomination used should be displayed]. Also print the amount in words according to the digits. Sample Data:INPUT: 14836 OUTPUT: ONE FOUR EIGHT THREE SIX DENOMINATION: 1000 X 14 =14000 500 X 1 =500 100 X 3 =300 50 X 1 =50 5 X 1 =5 1 X 1 =1 INPUT: 235001 OUTPUT: INVALID AMOUNT (ISC Practical 2010) ALGORITHM Step1-Declare the variables like rev,amount,dummy Step2-input amount and check if it is less than100000. Step3- if yes, reverse the number. Step4-begin loop till the whole of the numbers is written in words. Step4-find the denominations. Step5-print the denominations. PROGRAM import java.io.*; class Bank { int rev=0,amount,dummy; BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); public void intake() throws IOException { System.out.println("Enter the Amount:");

amount=Integer.parseInt(br.readLine()); if(amount >99999) { System.out.println("Invalid Amount..."); return; } dummy=amount; while(dummy >0) { rev=rev*10+dummy%10; dummy=dummy/10; } System.out.print("Amount in words :"); while(rev >0) { switch(rev%10) { case 0: System.out.print(" ZERO"); break; case 1: System.out.print(" ONE"); break; case 2: System.out.print(" TWO"); break; case 3: System.out.print(" THREE"); break; case 4: System.out.print(" FOUR"); break; case 5: System.out.print(" FIVE"); break; case 6: System.out.print(" SIX"); break; case 7: System.out.print(" SEVEN"); break; case 8: System.out.print(" EIGHT"); break; case 9: System.out.print(" NINE"); break; } rev=rev/10; }

System.out.println("\nDENOMINATORS:\n"); rev=amount/1000; if(rev!=0) System.out.println("1000 X " + rev + " = " + rev*1000); amount=amount%1000; rev=amount/500; if(rev!=0) System.out.println("500 X " + rev + " = " + rev*500); amount=amount%500; rev=amount/100; if(rev!=0) System.out.println("100 X " + rev + " = " + rev*100); amount=amount%100; rev=amount/50; if(rev!=0) System.out.println("50 X " + rev + " = " + rev*50); amount=amount%50; rev=amount/20; if(rev!=0) System.out.println("20 X " + rev + " = " + rev*20); amount=amount%20; rev=amount/10; if(rev!=0) System.out.println("10 X " + rev + " = " + rev*10); amount=amount%10; rev=amount/5; if(rev!=0) System.out.println("5 X " + rev + " = " + rev*5); amount=amount%5; rev=amount/2; if(rev!=0) System.out.println("2 X " + rev + " = " + rev*2); amount=amount%2; rev=amount/1; if(rev!=0) System.out.println("1 X " + rev + " = " + rev*1); } } VARIABLE DESCRIPTION Variable Name rev Amount Dummy Data Type Integer Integer Integer array Purpose To reverse the amount, and also to find the number of notes To store the amount To store a copy of the amount

OUTPUT Enter the Amount: 66542 Amount in words : SIX SIX FIVE FOUR TWO DENOMINATORS: 1000 X 66 = 66000 500 X 1 = 500 20 X 2 = 40 2X1=2

AIM: 13. Write a program which takes a string (maximum 80 characters) terminated by a full stop. The words in this string are assumed to be separated by one or more blanks. Arrange the words of the input string in descending order of their lengths. 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. (ISC Practical 2005) ALGORITHM Step1-delcare and initialize variables like I,j,x,str,str2,sr[],flag. Step2-input the sentence.and check if it satisfies the given conditions. Step3-store the words in sr[] and sort. Step4-print the words. PROGRAM

import java.io.*; import java.util.*; class Program1 { String str,str2; StringTokenizer stk; String sr[]; int i,j,x; int flag; BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); public void takeString()throws Exception { char ch; while(true) { System.out.println("\nEnter the sentence:"); str=br.readLine().trim(); if(str.length() >80) { System.out.println("\nString exceeds 80 characters."); continue; } if(str.charAt(str.length()-1)!='.') { System.out.println("\nString must terminate with full stop."); continue; } else break; } str=str.substring(0,str.length()-1); str2=""; x=str.length(); flag=0; for(i=0;i< x;i++) { ch=str.charAt(i); if(i==0) str2=str2+(char)(ch-32); else if(ch==' ') { flag=1; str2=str2+ch; } else if(flag==1) { flag=0; str2=str2+(char)(ch-32); }

else str2=str2+ch; } str=str2; stk=new StringTokenizer(str); x=stk.countTokens(); sr=new String[x]; x=0; while(stk.hasMoreTokens()) { str2=stk.nextToken().trim(); sr[x++]=str2; } display(); } private void display() { for(i=0;i< x-1;i++) { for(j=i+1;j< x;j++) { if(sr[i].length()< sr[j].length()) { str2=sr[i]; sr[i]=sr[j]; sr[j]=str2; } } } for(i=0;i< x;i++) { if(i!=x-1) System.out.print(sr[i]+" "); else System.out.print(sr[i]); } System.out.print("."); } public static void main(String args[])throws Exception { Program1 ob=new Program1(); ob.takeString(); } } VARIABLE DESCRIPTION Variable Name Int i,j Data Type Integer Purpose Loop variables

str str2 Sr[] x

Integer Integer array String array Integer

To store the string To store the sorted array To store the words To store the length and number of tokens

OUTPUT Enter the sentence: my name is noel. Name Noel Is My.

AIM: 14. Write 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. Sample Data:Input Date : 29/5/2011 Day on 1st January of that year : SATURDAY Output Day on 29/5/2011 : SUNDAY (ISC Practical 2007) ALGORITHM Step1-intialize the array arr[] and declare the other variables like day,day1,x,I,mon,yr,leap1. Step2-Enter the date Step3-store the numerical day month and year in the variables. Step4-if month si greater than 2, see if it is leap year. Step5- enter the first day of year. Step5-find the day of the input date and print it.

Step6-end process. PROGRAM import java.io.*; class Date1 { int arr[]={31,28,31,30,31,30,31,31,30,31,30,31}; BufferedReader br; String str1,day,day1; int x,i,dayno,mon,yr,leap1; public static void main(String args[])throws IOException { Date1 ob=new Date1 (); ob.take(); ob.show(); } Date1 () { br=new BufferedReader(new InputStreamReader(System.in)); } public void take()throws IOException { System.out.println("Enter the date ( in dd/mm/yyyy) format:"); day=br.readLine().trim(); day1=day; i=day.indexOf("/"); dayno=Integer.parseInt(day.substring(0,i)); day=day.substring(i+1); i=day.indexOf("/"); mon=Integer.parseInt(day.substring(0,i)); day=day.substring(i+1); yr=Integer.parseInt(day); leap1=0; if(mon>2) leap1=leap(yr); System.out.println("Enter the Day on 1st January in this year:"); str1=br.readLine().trim(); } int leap(int p) { if(p%100==0 && p%400==0) return 1; else if(p%100!=0 && p%4==0) return 1; else return 0; } void show () { if (str1.equalsIgnoreCase("Sunday"))

x=1; else if (str1.equalsIgnoreCase("Monday")) x=2; else if (str1.equalsIgnoreCase("Tuesday")) x=3; else if (str1.equalsIgnoreCase("Wednesday")) x=4; else if (str1.equalsIgnoreCase("Thursday")) x=5; else if (str1.equalsIgnoreCase("Friday")) x=6; else if (str1.equalsIgnoreCase("Saturday")) x=7; for(i=0;i< mon-1;i++) dayno=dayno+arr[i]; dayno=dayno+leap1; for(i=0;i< dayno-1;i++) { x++; if(x==8) x=1; } System.out.println(day1+ ":"); switch(x) { case 1: System.out.println("Sunday"); break; case 2: System.out.println("Monday"); break; case 3: System.out.println("Tuesday"); break; case 4: System.out.println("Wednesday"); break; case 5: System.out.println("Thursday"); break; case 6: System.out.println("Friday"); break; case 7: System.out.println("Saturday"); break; } } }

VARIABLE DESCRIPTION Variable Name arr[] day Str1 Day1 x dayno i mon yr Leap1 OUTPUT Enter the date ( in dd/mm/yyyy) format: 11/05/1995 Enter the Day on 1st January in this year: sunday 11/05/1995: Thursday Data Type Integer String Integer array String array Integer integer integer integer integer integer Purpose To store the number of days of all months. To store the duplicate of day1. To store first day of year. To store the words To store the length and number of tokens To store the numerical day Temporary variable To store the numerical month To store the numerical year To alter value if it is leap year

AIM: 15. Write a program to generate all possible anagrams of a word. An 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<= 7 Test your program for the given data and some random data. Sample Data:Input: TO

Output: TO OT Total number of anagrams = 2 Input: TOP Output: TOP TPO OPT OTP PTO POT Total number of anagrams = 6 (ISC Practical 2005) ALGORITHM Step1-declare and initialize variables like str, counter Step2-find all the anagrams 1 by 1 ,print them ,and increment the value of counter by 1 each time. Step3-print the number of anagrams. Step4-end process.

PROGRAM import java.io.*; public class Anagrams { String str; BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); int counter=0; public void take()throws Exception { System.out.println("\nEnter the word:"); str=br.readLine(); show("", str); System.out.println("Total number of anagrams ="+counter); } public void show(String s, String str) { if(str.length()<=1) { counter++;

System.out.print(s+str+" , "); } else { for(int i = 0; i< str.length(); i++) { String str1 = str.substring(i, i + 1); String str2 = str.substring(0, i); String str3 = str.substring(i + 1); show(s + str1, str2 + str3); } } } public static void main(String args[])throws Exception { Anagrams ob=new Anagrams(); ob.take(); } }

str counter Str1,str2,str3 i OUTPUT

String Integer string integer

To store the input word To store the number of anagrams Temporary storage Loop variable

Enter the word: nseag nseag ,nsega ,nsaeg ,nsage ,nsgea ,nsgae ,nesag ,nesga ,neasg ,neags ,negsa ,negas ,naseg ,nasge ,naesg ,naegs ,nagse ,nages ,ngsea ,ngsae ,ngesa ,ngeas ,ngase ,ngaes ,sneag ,snega ,snaeg ,snage ,sngea ,sngae ,senag ,senga ,seang ,seagn ,segna ,segan ,saneg ,sange ,saeng ,saegn ,sagne ,sagen ,sgnea ,sgnae ,sgena ,sgean ,sgane ,sgaen ,ensag ,ensga ,enasg ,enags ,engsa ,engas ,esnag ,esnga ,esang ,esagn ,esgna ,esgan ,eansg ,eangs ,easng ,easgn ,eagns ,eagsn ,egnsa ,egnas ,egsna ,egsan ,egans ,egasn ,anseg ,ansge ,anesg ,anegs ,angse ,anges ,asneg ,asnge ,aseng ,asegn ,asgne ,asgen ,aensg ,aengs ,aesng ,aesgn ,aegns ,aegsn ,agnse ,agnes ,agsne ,agsen ,agens ,agesn ,gnsea ,gnsae ,gnesa ,gneas ,gnase ,gnaes ,gsnea ,gsnae ,gsena ,gsean ,gsane ,gsaen ,gensa ,genas ,gesna ,gesan ,geans ,geasn ,ganse ,ganes ,gasne ,gasen ,gaens ,gaesn ,Total number of anagrams =120

AIM: 16. 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.

A positive natural number, (for e.g 27) can be represented as follows2+3+4+5+6+7 8+9+10 13+14 Every row represents a combination of consecutive natural numbers, which add up to 27. Sample Data:Input N=9 Output 45 234 Input N = 15 Output 78 12345 456 (ISC Practical 2006) ALGORITHM Step1-declare variables like sum, no, k, j, i. Step2-Enter the number. Step3-Find the numbers to be added. Step4-pritn the numbers using loop with integer k. Step5-end process. PROGRAM import java.io.*; class NumberCombonation { int no,sum=0,k,j,i; BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); public void takeNumber()throws Exception { System.out.println("Enter the number:"); no=Integer.parseInt(br.readLine()); for(i=1;i<=no/2+1;i++) { sum=0; for(j=i;j<=no/2+1;j++) { sum=sum+j;

if (sum==no) break; } if(j<=no/2+1) { for(k=i;k<=j;k++) System.out.print(" "+k); System.out.println(); } } } public static void main(String args[])throws Exception { NumberCombonation obj=new NumberCombonation (); obj.takeNumber(); } } no sum k i,j OUTPUT Enter the number: 152 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 integer Integer integer integer To store the input number. To store the sum of numbers To store the value each number to be added Loop variables

AIM: 17. Write a program that inputs the names of people in to different arrays, A and B. Array A has N number of names while array B has M number of names, with no duplicates in either of them. Merge arrays A and B in to a single array C, such that the resulting array is stored alphabetically. Display all the three arrays, A, B and C, stored alphabetically. Test your program for the given data and some random data. Sample Data:Input: Enter number of names in Array A, N = 2 Enter number of names in Array A, B = 2 First array: (A) Suman Anil Second array: (B) Usha Sachin John Output: Stored Merged array: (C) Anil John Sachin Suman Usha Stored First array: (A) Anil Suman Stored second array: (B) John Sachin Usha (ISC Practical 2006) ALGORITHM Step1-declare and initialize variables like str1[],str2[],str3[],n,m. Step2-Enter the number of names of the 2 arrays. Step3-enter the names for first and second array. Step3-Sort the first and second array Step4-merge the 2 arrays by taking the smallest(Alphebetically) and placing first.

Step5-print the merged array,sorted first array and second array. Step5-end process. PROGRAM import java.io.*; class SortedNames { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); String str1[],str2[],str3[]; int n,m; String s; int i,j; public void take ()throws Exception { System.out.println("Number of Names for first array:"); n=Integer.parseInt(br.readLine()); System.out.println("Number of Names for second array:"); m=Integer.parseInt(br.readLine()); str1=new String[n]; str2=new String[m]; str3=new String[n+m]; System.out.println("Names for 1st array:"); for(i=0;i< n;i++) { System.out.println("Name:"); str1[i]=br.readLine().trim(); } System.out.println("Names for 2nd array:"); for(i=0;i< m;i++) { System.out.println("Name:"); str2[i]=br.readLine().trim(); } sort(); merge(); display(); } private void sort() { int flag; for(i=0;i< n;i++) { flag=0; for(j=0;j< n-i-1;j++) { if(str1[j].compareTo(str1[j+1]) >0) { s=str1[j]; str1[j]=str1[j+1];

str1[j+1]=s; flag=1; } } if(flag==0) break; } for(i=0;i< m;i++) { flag=0; for(j=0;j< m-i-1;j++) { if(str2[j].compareTo(str2[j+1]) >0) { s=str2[j]; str2[j]=str2[j+1]; str2[j+1]=s; flag=1; } } if(flag==0) break; } } private void merge() { int x=0; i=0; j=0; while(i!=n && j!=m) { if(str1[i].compareTo(str2[j])<=0) str3[x++]=str1[i++]; else str3[x++]=str2[j++]; } if(i==n) { for(;j< m;j++) str3[x++]=str2[j]; } else { for(;i< n;i++) str3[x++]=str1[i]; } } private void display() { System.out.println("\nSorted Merged Names\n");

for(i=0;i< m+n;i++) System.out.println(str3[i]); System.out.println(" "); System.out.println("\nFirst array names\n"); for(i=0;i< n;i++) System.out.println(str1[i]); System.out.println(""); System.out.println("\nSecond array names\n"); for(i=0;i< m;i++) System.out.println(str2[i]); } public static void main(String args[])throws Exception { SortedNames obj=new SortedNames(); obj.take(); } }

Str1[],str2[],str3 n,m flag i,j,x OUTPUT

String array Integer integer integer

To store the three group of names. To store the number of digits of the 3 arrays. To check if the array is sorted. Loop variables

Enter the number: Number of Names for first array: 3 Number of Names for second array: 2 Names for 1st array: Name: leon Name: nibuz Name: rotciv Names for 2nd array: Name: senga

Name: esor Sorted Merged Names esor leon nibuz rotciv senga

First array names leon nibuz rotciv

Second array names esor senga

AIM: 18. A new advanced Operating System, incorporating the latest hi-tech features has been designed by Opera Computer System. The task of generating copy protection codes to prevent software piracy has been entrusted to the Security Department. The security department has decided to have codes containing a jumbled combination of alternate uppercase letters of the alphabet starting from A upto K (namely among A,C,E,G,I,K). The codes may or may not be in the consecutive series of alphabets. Develop 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=4 ABCE Output: Invalid! Only alternate letters permitted! Input: N=4 AcIK Output: Invalid! Only upper case letters permitted!

Input: N=4 AAKE Output: Invalid! Repetition of characters not permitted! Input: N=7 Output: Error! Length of string should not exceed 6 characters! Input: N=4 AEGIK Output: Invalid! String length not the same as specified! Input: N=3 ACE Output: Valid! Input: N=5 GEAIK Output: Valid! (ISC Practical 2006) ALGORITHM Step1-declare the variables like sentence,n. Step2-enter the length. Step3-enter the code Step4-check if the input length is equal to the length of the string,or if it exceeds 6 letters. Step5-check if the code has only Upper case letters. Step6-check if it has alternate letters. Step7-check if any letter is repeated. Step8-if it passes all given conditions,print valid. Step9-end process. PROGRAM import java.io.*; class Sentence { String s1="ACEGIK"; boolean bool; BufferedReader br=new BufferedReader(new InputStreamReader(System.in));

public void disp()throws Exception { int n; String sentence; System.out.println("Enter the length:"); n=Integer.parseInt(br.readLine()); System.out.println("Enter the code:"); sentence=br.readLine(); bool=checkLength(sentence,n); if(!bool) return; bool=checkChar(sentence); if(!bool) return; bool=checkSequence(sentence); if(!bool) return; bool=checkRepeat(sentence); if(!bool) return; System.out.println("Valid Input..."); } private boolean checkLength(String str, int n) { boolean b=true; if(n!=str.length()) { b=false; System.out.println("Invalid! String length not the same as specified"); } else if(n>6) { b=false; System.out.println("Error! Length of string should not exceed 6 characters!"); } return b; } private boolean checkChar(String str) { int i,len; len=str.length(); for (i=0;i< len;i++) { if(str.charAt(i)< 65 || str.charAt(i) >90) break; } if(i==len) return true; else {

System.out.println("Invalid! Only upper case characters are permitted."); return false; } } private boolean checkSequence(String str) { int i,len; char ch; len=str.length(); for (i=0;i< len;i++) { ch=str.charAt(i); if(s1.indexOf(ch)< 0) break; } if(i==len) return true; else { System.out.println("Invalid! Only alternate letters are permitted."); return false; } } private boolean checkRepeat(String str) { int i,j,len; char ch; len=str.length(); for (i=0;i< len-1;i++) { ch=str.charAt(i); for(j=i+1;j< len;j++) { if(ch==str.charAt(j)) break; } if(j!=len) break; } if(i==len-1) return true; else { System.out.println("Invalid! Repetition of letters is not permitted."); return false; } }

public static void main(String args[])throws Exception { Sentence ob=new Sentence(); ob.disp(); } }

sentence n bool len i,j

String Integer boolean integer integer

To store the input letters. To store the size of input letters To check the given conditions. To store the length of the string. Loop variables

OUTPUT Enter the code: BFJB Invalid! Only alternate letters are permitted. Enter the length: 3 Enter the code: ACE Valid Input...

AIM: 19. A unique-digit integer is a positive integer (without leading zeros) with no duplicates digits. For example 7, 135, 214 are all unique-digit integers whereas 33, 3121, 300 are not. Given two positive integers m and n, where m < n, write a program to determine how many unique-digit integers are there in the range between m and n (both inclusive) and output them. The input contains two positive integers m and n. Assume m < 30000 and n <30000. You are to output the number of unique-digit integers in the

specified range along with their values in the format specified below: Sample Data:Input: m = 100 n = 120 Output: THE UNIQUE-DIGIT INTEGERS ARE: 102, 103, 104, 105, 106, 107, 108, 109, 120. FREQUENCY OF UNIQUE-DIGIT INTEGERS IS : 9 Input: m = 2500 n = 2550 Output: THE UNIQUE-DIGIT INTEGERS ARE: 2501, 2503, 2504, 2506, 2507, 2508, 2509, 2510, 2513, 2514, 2516, 2517, 2518, 2517, 2530, 2519, 2530, 2531, 2534, 2536, 2537, 2538, 2539, 2540, 2541, 2543, 2546, 2547, 2548, 2549. FREQUENCY OF UNIQUE-DIGIT INTEGERS IS: 28. (ISC Practical 2007) ALGORITHM Step1-declare variables like arr[],I,j,k,c,m,n. Step2-enter the lower and upper range. Step3-Find the unique digits. Step4-print the digits. Step5-end process. PROGRAM import java.io.*; class Digit { int k,x,i,j,m,n,c,arr[],index; BufferedReader br; public static void main(String args[])throws IOException { Digit ob=new Digit(); ob.take(); ob.show(); } Digit() { br=new BufferedReader(new InputStreamReader(System.in)); arr=new int[1000]; c=0; index=0; } public void take()throws IOException {

while(true) { System.out.println("Enter the Lower range:"); m=Integer.parseInt(br.readLine().trim()); System.out.println("Enter the Upper range:"); n=Integer.parseInt(br.readLine().trim()); if(m< n) break; } } void show () { System.out.println("The unique digit integers are:"); for(i=m;i<=n;i++) { index=0; for(j=i;j> 0;j=j/10) { x=j%10; for(k=0;k< index;k++) { if (arr[k]==x) break; } if(k< index) break; else arr[index++]=x; } if (j==0) { c++; System.out.print(" "+i); } } System.out.println("\nFequency of unique digit integers is :"+ c); } }

arr[] m,n c i,j,k

Integer array Integer integer Integer,integer,integer

To store the unique numbers. To store the range. To store the number of unique digits Loop variables

OUTPUT

Enter the Lower range: 100 Enter the Upper range: 500 The unique digit integers are: 102 103 104 105 106 107 108 109 120 123 124 125 126 127 128 129 130 132 134 135 136 137 138 139 140 142 143 145 146 147 148 149 150 152 153 154 156 157 158 159 160 162 163 164 165 167 168 169 170 172 173 174 175 176 178 179 180 182 183 184 185 186 187 189 190 192 193 194 195 196 197 198 201 203 204 205 206 207 208 209 210 213 214 215 216 217 218 219 230 231 234 235 236 237 238 239 240 241 243 245 246 247 248 249 250 251 253 254 256 257 258 259 260 261 263 264 265 267 268 269 270 271 273 274 275 276 278 279 280 281 283 284 285 286 287 289 290 291 293 294 295 296 297 298 301 302 304 305 306 307 308 309 310 312 314 315 316 317 318 319 320 321 324 325 326 327 328 329 340 341 342 345 346 347 348 349 350 351 352 354 356 357 358 359 360 361 362 364 365 367 368 369 370 371 372 374 375 376 378 379 380 381 382 384 385 386 387 389 390 391 392 394 395 396 397 398 401 402 403 405 406 407 408 409 410 412 413 415 416 417 418 419 420 421 423 425 426 427 428 429 430 431 432 435 436 437 438 439 450 451 452 453 456 457 458 459 460 461 462 463 465 467 468 469 470 471 472 473 475 476 478 479 480 481 482 483 485 486 487 489 490 491 492 493 495 496 497 498 Fequency of unique digit integers is :288

AIM: 20. A sentence is terminated by either . , ! or ? followed by a space. Input a piece of text consisting of sentences. Assume that there will be a maximum of 10 sentences in a block. Write a program to: (i)Obtain the length of the sentence (measured in words) and the frequency of vowels in each sentence. (ii)Generate the output as shown below using the given data Sample Data:Input: HELLO! HOW ARE YOU? HOPE EVERYTHING IS FINE. BEST OF LUCK. Output: Sentence No. of Vowels No. of words ---------------------------------------------------------1 2 1 2 5 3 3 8 4 4 3 3 (ISC Practical 2008)

ALGORITHM Step1-initialize and declare variables like word,sen,i,vow. Step2-enter the sentences. Step3-begin loop of I from0 to s.length-1 Step4-increment values of word and vowel if their respective character give the test Step5-print the number of sentence,words andvowels. Step6-Repeat 4 and 5 for the next sentence. Step6-End process PROGRAM import java.io.*; class pro20 { public static void main(String args[])throws IOException { int i; int word=1; int sen=1; int vow=0;

BufferedReader bf=new BufferedReader(new InputStreamReader(System.in)); System.out.println("Enter the sentences"); String s=bf.readLine(); int len=s.length(); System.out.println("sentence no:vowel no:words"); for(i=0;i<len;i++) { if(s.charAt(i)==32) word++; else if((s.charAt(i)=='A')||(s.charAt(i)=='a')||(s.charAt(i)=='E')||(s.charAt(i)=='e')||(s.charAt(i)=='I')| |(s.charAt(i)=='i')||(s.charAt(i)=='O')||(s.charAt(i)=='o')||(s.charAt(i)=='U')||(s.charAt(i)=='u')) vow++; else if((s.charAt(i)=='.')||(s.charAt(i)==',')||(s.charAt(i)=='!')||(s.charAt(i)=='?')) { System.out.println(sen+"\t"+"\t"+vow+"\t"+"\t"+word); sen++; word=1; vow=0; } }}} VARIABLE DESCRIPTION Variable Name i word sen vow s Data Type integer Integer array Integer array Integer, Integer string Purpose Loop variable To store the number of words. To store the number of sentences. To store the number of vowels. To store the input string

OUTPUT Enter the sentences to be sure of hitting the target,shoot first.and whatever you hit,call it the target. sentence no:vowel no:words 1 10 7 2 3 2 3 7 4 4 5 4

AIM: 3. Write a Java program to find the product of two matrices. ALGORITHM Step1-declare variables like a,b,m,n,p,q,i,j,k. Step 2-Enter the size of the two arrays Step 3-check if the matrices are compatible for multiplication Step 4 Enter the elements of the 2 arrays Step 5-find the product by finding the sum of the products of the corresponding elements in the row of the first array and the column of the second. Step 6-print the resultant array. PROGRAM import java.io.*; class multiple { public static void main(String args[])throws IOException { int a[][],b[][],m,n,p,q,i,j,k; BufferedReader bf=new BufferedReader(new InputStreamReader(System.in)); System.out.println("Enter the number of rows and columns for first and second matrices"); m=Integer.parseInt(bf.readLine()); n=Integer.parseInt(bf.readLine()); p=Integer.parseInt(bf.readLine()); q=Integer.parseInt(bf.readLine()); if(n==p) { if(m>20||n>20||p>20||q>20) { System.out.println("Invalid input"); System.exit(0); } a=new int[m][n]; b=new int[p][q]; int c[][]=new int[m][q]; System.out.println("Enter the elements for first matrix"); for(i=0;i<m;i++) { for(j=0;j<n;j++) {

a[i][j]=Integer.parseInt(bf.readLine()); } } System.out.println("Enter the elements for second matrix"); for(i=0;i<p;i++) { for(j=0;j<q;j++) { b[i][j]=Integer.parseInt(bf.readLine()); } } for(i=0;i<m;i++) { for(j=0;j<q;j++) { c[i][j]=0; for(k=0;k<n;k++) { c[i][j]+=a[i][k]*b[k][j]; } } } System.out.println("Result is"); for(i=0;i<m;i++) { for(j=0;j<q;j++) { System.out.print(c[i][j]+" "); } System.out.println(); } } } }VARIABLE DESCRIPTION variable Name m,n,p,q a[][],b[][] c[] i,j,k, Data Type Integer,integer,integer,integer Integer array,integer array Integer arrya Integer,integer,integer Purpose To size of the 2arrays. To store the two input arrays To store product Loop variables

OUTPUT

Enter the number of rows and columns for first and second matrices 2 2 2 2 Enter the elements for first matrix 1 2 3 4 Enter the elements for second matrix 4 0 1 0 Result is 60 16 0

AIM: 11. Write a program to accept a sentence as input. The words in the string are to be separated by a blank. Each word must be in upper case. The sentence is terminated by either .,! or ?. Perform the following tasks: Obtain the length of the sentence (measured in words). Arrange the sentence in alphabetical order of the words.

Test your program with the sample data and some random data: Sample Data:INPUT: NECESSITY IS THE MOTHER OF INVENTION. OUTPUT: Length: 6 Rearranged Sentence: INVENTION IS MOTHER NECESSITY OF THE INPUT: BE GOOD TO OTHERS. OUTPUT: Length: 4 Rearranged Sentence: BE GOOD OTHERS TO (ISC Practical 2012) ALGORITHM Step1-declare the variables likei,j,x,c,words[], Step2-Enter the sentence. Step3-check if it is in uppercase. Step4-store the words in a string array Step5-sort the array in alphabetic order. Step6-print the words from the string array Step7-end process. PROGRAM import java.io.*; import java.util.*; class Matrixmult { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); String str;

String words[]; StringTokenizer stk; int i,j,c,x; public void take()throws Exception { int flag; while(true) { flag=0; System.out.println("Enter the sentence:"); str=br.readLine(); for(i=0;i< str.length();i++) { if(Character.isLowerCase(str.charAt(i))) { flag=1; break; } } if (flag==0) break; } stk=new StringTokenizer(str," .,?!"); c=stk.countTokens(); x=0; words=new String[c]; while(stk.hasMoreElements()) { words[x++]=stk.nextToken(); } System.out.println("Length="+c); for(i=0;i< x-1;i++) { for(j=i+1;j< x;j++) { if(words[i].compareTo(words[j])>0) { str=words[i]; words[i]=words[j]; words[j]=str; } } } System.out.println("\nRearranged Sentence:\n");

for(i=0;i< c;i++) System.out.print(words[i]+" "); } public static void main(String args[]) throws Exception { Matrixmult ob=new Matrixmult(); ob.take(); } }

VARIABLE DESCRIPTION variable Name str Words[] i,j,x c Data Type string String array Integer,integer,integer. integer Purpose To store the input sentence. To store the words. Loop variables To count the number of tokens.

OUTPUT Enter the sentence: AGGIE IS THE BESTEST CREATION OF GOD Length=7 Rearranged Sentence: AGGIE BESTEST CREATION GOD IS OF THE

You might also like