1) Write a program to reverse a sentence and check for palindrome in each substring.
reverse()
Step 1: Declare revsent and rev as String and initialize as ""
Step 2: Create Scanner object to manipulate the sentence
Step 3: Start of while loop to reverse the sentence
Step 4: Start a for loop within the while loop to reverse each word and store the reversed
sentence in revsent.
Step 5: Using if else statement, check whether each word is a palindrome and print the
result.
Step 6: Print the reversed sentence.
main()
Step 1: Start
Step 2: Create object for calling the reverse() function.
Step 3: Create Scanner object to input sentence from user
Step 4: Store the input in String variable s
Step 5: Call reverse() function
Step 6: Stop
2) Write a program to find the longest word in an entered string.
longWd(String sen)
Step 1: Declare ‘l’ as String and initialize as "".
Step 2: Create scanner object.
Step 3: Start of while(in.hasNext())
Step 4: Store each word of the sentence in 'a'.
Step 5: Using if statement, compare the length of each word and store the longest word in 'l'.
Step 6: Print the longest word.
main()
Step 1: Start.
Step 2: Create object for calling function.
Step 3: Create scanner object for input.
Step 4: Store the entered sentence in 's'.
Step 5: Call the function longWd().
3) Write a program to accept a sentence and convert it into capitals and check the validity of
the string. If the string is valid, then display the string omitting the brackets.
valid()
Step 1: Declare 'n' as int and initialize as 0.
Step 2: Convert 's' to uppercase.
Step 3: Using if statement, check if first and last character is '(' and ')'.
Step 4: Declare 'str' as String an initialize as "".
Step 5: Start a for loop from i=0 to less than the length of 's' and increment by 1.
Step 6: Strore each character of 's' at the position 'i' in 'ch'.
Step 7: If 'ch' is '(' or ')' then n is incremented by 1.
Step 8: Store the new string in 'str'.
Step 9: If n=0 then the string is valid and print it, else print the statement "String is Not
Valid".
main()
Step 1: Start.
Step 2: Create object for calling function.
Step 3: Create scanner object for input.
Step 4: Store the entered sentence in 's'.
Step 5: Call the function valid().
4) Write a program to print the word of the text in reversed order without any punctuation
other than the blanks.
revsent()
Step 1: Declare ‘str' as String and initialize as “”.
Step 2: Start a for loop from i=0 to less than the length of ‘sent' and increment by 1.
Step 3: : Strore each character of ‘sent' at the position 'i' in 'ch’.
Step 4: Using if statement, check whether ‘ch’ is a letter or digit or if it is ‘ ‘ and store the
sentence in ‘str’.
Step 5: Use while(in.nextHas()) to reverse the the order of the string and store it in ‘fin’.
Step 6: Print the value of ‘fin’.
main()
Step 1: Start.
Step 2: Create object for calling function.
Step 3: Create scanner object for input.
Step 4: Store the entered sentence in 's'.
Step 5: Call the function revsent(s).
5) Write a program to input a number and check whether it is a Smith Number or not.
main()
Step 1: Start.
Step 2: Declare number, sum=0, sum1=0, r=0, i=2, copy, copy2, copy3 as int.
Step 3: Create scanner object for input.
Step 4: Strore the entered number in ‘num’.
Step 5: Intialize copy2 and copy3 as num.
Step 6: Start outer while(num>1).
Step 7: Use if ((number%i==0)&&(i<10)) and store num as num/i and sum as sum+i.
Step 8: Use else if((number%i==0)&&(i>10)) and store num as num/i and copy as i.
Step 9: Start inner while (copy>0) and get sum as sum of digits of copy and close the inner
while loop.
Step 10: Else increment i by 1 and close outer while loop.
Step 11: Start while (copy2>0) and get sum1 as sum of digits of copy2 and close the while
loop.
Step 12: If sum=sum1, then print “The number is a smith number” else print “The number is
not smith number”.
Step 13: Stop.
6) Write a program to accept a number (four digit) and print it in words.
main()
Step 1: Start.
Step 2: Create Scanner object.
Step 3: Ask user to input a number and store it in ‘num’.
Step 4: Declare ‘x’ and ‘p’ as int and initialize ‘x’ as ‘num’ and ‘p’ as ‘0’.
Step 5: Start while(x!=0) to check whether the user’s input is a four digit number and use ‘p’
as a counter variable to count the number of digits.
Step 6: Using if statement check if p=4. If yes then continue the following steps else print
“INVALID NUMBER”.
Step 7: Declare ‘one’ , ‘ten’ , hundred’ and ‘thousand’ as int and store the digits of ‘num’ at
ones, tens, hundreds and thousands place in the variables: ‘one’ , ‘ten’ , hundred’ and
‘thousand’ respectively.
Step 8: Declare ‘ones’, ‘tens’ ,’hund’ and ‘thou’ as String and intitialize as “”.
Step 9: If ‘ten’ is not equal to 1, continue the following steps else use switch(one) statement
to assign the value of ‘ones’ as “Eleven”, “Twelve”, “Thirteen” and so on till “Nineteen” with
default as ‘ones’=””.
Step 10: If Step 9 is true then use switch(one) statement to assign the value of ‘ones’ as
“One”, “Two”, “Three” and so on till “Nine” with default as ‘ones’=””.
Step 11: Using if statement check if ‘ten’ is not equal to 1. If yes then continue the following
steps else ‘tens’ = ””.
Step 12: If Step 11 is true use switch(ten) statement to assign the value of ‘tens’ as “Ten”,
“Twenty”, “Thirty” and so on till “Ninety” with default as ‘tens’ = “”.
Step 13: If hundred =1 , ‘hund’=”One” else if hundred=2, ‘hund’=”Two”, else if hundred=3,
‘hund’=”Three” and so on till ‘hund’=”Nine” with else statement as ‘hund’ = ””.
Step 14: If thousand =1 , ‘thou’=”One” else if thousand=2, ‘thou’=”Two”, else if thousand=3,
‘thou’=”Three” and so on till ‘thou’=”Nine” with else statement as ‘thou’ = ””.
Step 15: If ‘tens’, ‘ones’ and ‘hund’ = “”, then print "The Number Entered in Words is:
____ Thousand ". Else if ‘tens’ and ‘ones’ = “”, then print "The Number Entered in Words is:
____ Thousand ____ Hundred ". Else if ‘hund’ is not equal to ””, then print "The Number
Entered in Words is: ____ Thousand ____ Hundred and ____". Else print "The Number
Entered in Words is: ____ Thousand and ____ ".
Step 16: Stop.
7) Write a program which first inputs two integers, the first between 1-12(both included)
and the second between 0 and 59(both included), and prints the time they represent in
words.
main()
Step 1: Create Scanner object.
Step 2: Ask user to input Hours and store in ‘hr’.
Step 3: Ask user to input Minutes and store in ‘min’.
Step 4: If ‘hr’ is between 1-12 and ‘min’ is between 0-59 then continue the following steps
else print “Invalid Input”.
Step 5: If Step 4 is true, create an String array ‘wd’ and store the values: “”, “One”, “Two”,
“Three” and so on till “Twenty-Nine”.
Step 6: Declare ‘plu’ and ‘a’ as String.
Step 7: If ‘min’=1, ‘plu’ =”Minute” else ‘plu’ =”Minutes”.
Step 8: If ‘hr’ =12, ‘a’ stores the value at position 1 of ‘wd’ else, ‘a’ stores the value of
position ‘hr’+1 of ‘wd’.
Step 9: Print the time in digits.
Step 10: If ‘min’ =0, print wd[hr]+” o’clock”. Else if ‘min’ =15, print “Quarter Past “+wd[hr].
Else if ‘min’ =30, print “Half Past “+wd[hr]. Else if ‘min’ =45, print “Quarter to “+a. Else if
‘min’ <30 print: wd[min]+" "+plu+" Past "+wd[hr] .Else print: wd[60-min]+" "+plu+" to "+a.
Step 11: Stop.
8) Write a program to calculate HCF and LCM of a number.
main()
Step 1: Start.
Step 2: Declare a,b,I,hcf=0,p=0,lcm=0.
Step 3: Create Scanner object.
Step 4: Ask user to input the 1st number and store it in ‘a’.
Step 5: Ask user to input the 2nd number and store it in ‘b’.
Step 6: Start a for loop from i=1 to less than or equal to ‘a’ or ‘b’
Step 7: If ‘a’ and ‘b’ divided by ‘i’ gives a remainder 0, then ‘hcf’ =’i’. Close the for loop.
Step 8: Print the ‘hcf’.
Step 9: Store the product of ‘a’ and ‘b’ in ‘p’.
Step 10: ‘lcm’ is given by p/hcf.
Step 11: Print the ‘lcm’.
Step 12: Stop.
9) Design a program to accept the amount from the user and display the breakup in
descending order of denomination.
main()
Step 1: Start.
Step 2: Create Scanner object.
Step 3: Declare ‘amt’ as int.
Step 4: Ask user to input an amount of money and store it in ‘amt’
Step 5: Declare fifty=0, twenty=0, five=0, two=0, one=0.
Step 6: Start while loop with the condition ‘amt’ is not equal to 0.
Step 7: If ‘amt’ >=50, then amt=amt-50 and counter variable ‘fifty’ increases by one.
Step 8: Else if ‘amt’ >=20, then amt=amt-20 and counter variable ‘twenty’ increases by one.
Step 9: Else if ‘amt’ >=5, then amt=amt-5 and counter variable ‘five’ increases by one.
Step 10: Else if ‘amt’ >=2, then amt=amt-2 and counter variable ‘two’ increases by one.
Step 11: Else if ‘amt’ >=1, then amt=amt-1 and counter variable ‘one’ increases by one.
Step 12: End of while loop.
Step 13: Print the breakup in descending order of denomination.
Step 14: Stop.
10) Write a program to input a number and convert it into its binary form.
decimalToBinary(int deci)
Step 1: Create an integer array bin[] to store 100 values.
Step 2: Declare i=0 as int.
Step 3: Start while loop with the condition deci >0.
Step 4: Store the remainder of ‘deci’ divided by 2 in bin[] at the position i++.
Step 5: Change the value to: deci=deci/2 and close while loop.
Step 6: Start a for loop from x=i-1 to x>=0 and decrement by 1.
Step 7: Print the value in bin[] at the position x and close for loop.
main()
Step 1: Start.
Step 2: Create Scanner object.
Step 3: Declare dec as int.
Step 4: Ask user for input and store it in dec.
Step 5: Call the function decimalToBinary() and pass the value of dec.
Step 6: Stop.
11) Write a program to input a start limit (s>0) and a last limit (I>0). Print all prime triplets between s
and I with a suitable message. The prime triplets may be greater or lesser than I depending on the
conditions used for the generation of the prime combinations.
main()
Step 1: Start.
Step 2: Create Scanner object.
Step 3: Declare start=0, end=0 as int.
Step 4: Ask user to enter the start limit and store it in ‘start’.
Step 5: Ask user to enter the end limit and store it in ‘end’.
Step 6: Declare k=0, flag=0 as int.
Step 7: Create an integer array a[] with limit ‘end-start’.
Step 8: Start outer for loop from i=start+1 till i<(end+6) and increment i by 1.
Step 9: initialize flag as 0.
Step 10: Start inner for loop from j=2 to j<=i and increment j by 1.
Step 11: if the remainder of ‘i’ divided by ‘j’ is equal to 0 the, flag++.
Step 12: Close inner for loop.
Step 13: if flag =1, store ‘i’ in a[] at the position ‘k’ and increment k by 1.
Step 14: Close outer for loop.
Step 15: Start for loop from i=0 to i <(length of ‘a’-2) and increment i by 1.
Step 16: if a[i]+2 = a[i+1] and a[i]+6 = a[i+2] then print a[i], a[i]+6 and a[i]+6 as the prime
triplets. Else if a[i]+4 = a[i+1] and a[i]+6 = a[i+2] then print a[i] , a[i]+4 and a[i]+6 as the prime
triplets.
Step 17: Stop.
12)Write a program to determine how many unique digits are there in the range between m
and n(both included), and display them.
main()
Step 1: Create a Scanner object.
Step 2: Declare m, n, count=0, x=0 as int.
Step 3: Ask user to enter the start limit and store it in ‘m’.
Step 4: Ask user to enter the end limit and store it in ‘n’.
Step 5: Start outer for loop from i=m to i<=n and increment i by 1.
Step 6: Declare ‘a’ as String and store the string value of ‘i’.
Step 7: Start middle for loop from j=0 to j<length of ‘a’ and increment by 1.
Step 8: Start inner for loop from k=(j+1) to k<length of ‘a’ and increment by 1.
Step 9: If character of ‘a’ at the position ‘j’ = character of ‘a’ at the position ‘k’ then, x++.
Step 10: Close inner and middle for loop.
Step 11: if x=0, print ‘a’ and increment count by 1.
Step 12: Close the outer for loop.
Step 13: Print the number of unique digits.
13) Write a program to accept a sentence and a word from the user. Check that the word
can be found from the sentence.
Declare sen=””, wd=””, str=”” as String, and ‘l’, ‘c’ as int and ‘ch’ as char.
input()
Step 1: Create Scanner object.
Step 2: Ask user to input a sentence and store it in ‘sen’.
Step 3: Ask user to input the word to be searched and store it in ‘wd’.
check()
Step 1: Store the length of ‘sen’ in ‘l’.
Step 2: Declare ‘b’ as Boolean.
Step 3: Start for loop from i=0 to i<=(l-1) and increment it by 1.
Step 4: Store the character of ‘sen’ at the position ‘i’.
Step 5: If ‘ch’ is not equal to ‘ ‘ then str =str+ch.
Step 6: If ‘ch’ is equal to ‘ ‘ continue the following steps else str =””.
Step 7: If step 6 is true then if str = wd, then c++ and use break statement.
Step 8: Close for loop.
Step 9: If ‘c’ is not equal to 0 then print "The word is present in the sentence", else print
"The word cannot be found in the sentence".
main()
Step 1: Start.
Step 2: Create object to call the functions.
Step 3: Call the functions input() and check().
Step 4: Stop.
14) Write a program to input a matrix and print its transpose.
main()
Step 1: Start.
Step 2: Create Scanner object.
Step 3: Ask user to Enter the number of Rows and store it in ‘r’
Step 4: Ask user to Enter the number of Columns and store it in ‘c’
Step 5: Create array int matrix[][]=new int [r][c].
Step 6: Start for loop to enter elements of the matrix and display it.
Step 7: Start for loop to get the transpose of the matrix and display it.
Step 8: Stop.
15) Write a program to accept two matrices and find their sum.
main()
Step 1: Start.
Step 2: Create Scanner object.
Step 3: Ask user to Enter the number of Rows and store it in ‘r’
Step 4: Ask user to Enter the number of Columns and store it in ‘c’
Step 5: Create array int matrix1[][]=new int [r][c] and array int matrix2[][]=new int [r][c] .
Step 6: Start for loop to enter elements of the 1st matrix and display it.
Step 7: Start for loop to enter elements of the 2nd matrix and display it.
Step 8: Create an array int sum[][]=new int[r][c] and display it.
Step 9: Stop.
16)Write a program to print the upper and lower triangle of a matrix.
main()
Step 1: Start.
Step 2: Create Scanner object.
Step 3: Ask user to Enter the number of Rows and store it in ‘r’
Step 4: Ask user to Enter the number of Columns and store it in ‘c’
Step 5: Create array int arr[][]=new int [r][c].
Step 6: Start for loop to enter elements of the matrix and display it.
Step 7: Start outer for loop from i=0 to i<r and increment i by 1.
Step 8: Start inner for loop from j=0 to j<c and increment j by 1.
Step 9: If j>i, print 0 + ” “. Else print arr[i][j] + ” “ for Lower Triangular matrix.
Step 10: Close inner for loop print a new line and close outer for loop.
Step 11: Start outer for loop from i=0 to i<r and increment i by 1.
Step 12: Start inner for loop from j=0 to j<c and increment j by 1.
Step 13: If i>j, print 0 + ” “. Else print arr[i][j] + ” “ for Upper Triangular matrix.
Step 14: Close inner for loop print a new line and close outer for loop.
Step 15: Stop.
17)Write a program to find the row sum and column sum of a matrix.
main()
Step 1: Start.
Step 2: Create Scanner object.
Step 3: Ask user to Enter the number of Rows and store it in ‘r’
Step 4: Ask user to Enter the number of Columns and store it in ‘c’
Step 5: Create array int matrix[][]=new int [r][c].
Step 6: Start for loop to enter elements of the matrix and display it.
Step 7: Declare rsum = 0, csum = 0 as int.
Step 8: Start outer for loop from i=0 to i<r and increment by 1.
Step 9: rsum = 0.
Step 10: Start inner for loop from j=0 to j<c and increment by 1.
Step 11: rsum = rsum +matrix[i][j]. Close inner for loop.
Step 12: Print sum of each row. Close outer for loop.
Step 9: Start outer for loop from i=0 to i<c and increment by 1.
Step 10: csum = 0.
Step 11: Start inner for loop from j=0 to j<r and increment by 1.
Step 12: csum = csum +matrix[j][i]. Close inner for loop.
Step 13: Print sum of each column. Close outer for loop.
Step 14: Stop.
18) Write a program to print Julian Date.
Step 1: Create Scanner object.
Step 2: Declare JD = 0 as int.
Step 3: Ask user to enter the date and store say in d, month in m and year in y.
Step 4: Declare f=0 as int.
Step 5: If y divided by 4 gives the remainder 0, f=29. Else f=28.
Step 6: Declare jb=31+f
Step 7: If m = 1 then JD=d.
If m=2 then JD=31+d.
If m = 3 then JD=jb+d.
If m=4 then JD=jb+31+d.
If m = 5 then JD=jb+31+30+d.
If m=6 then JD=jb+31+30+31+d.
If m=7 then JD=jb+31+30+31+30+d.
Step 8: Declare tiiljul as int and initialize as jb+31+30+31+30+31.
Step 9: If m=8 then JD=tilljul+d.
If m=9 then JD=tilljul+31+d.
If m=10 then JD=tilljul+31+30+d.
If m=11 then JD=tilljul+31+30+31+d.
If m=12 then JD=tilljul+31+30+31+30+d.
Step 10: Print the julian date.
Step 11: Stop.
19) Write a program to check if an external number is prime or not using recursion.
Declare num , i=1, count =0 as int.
acceptValue()
Step 1: Create Scanner object.
Step 2: Ask user to enter a number and store it in num.
checkPrime()
Step 1: If num divided by i gives the remainder 0, count++
Step 2: If i is not equal to num then checkPrime(++i)
printValue()
Step 1: If count =1, print num is a prime number. Else, print num is not a prime number.
main()
Step 1: Start.
Step 2: Create object for calling functions.
Step 3: Call functions acceptValue(), checkPrime(), printValue().
Step 4: Stop.
20) Write a program to print the longest word using recursion.
Declare sen="", str="", longest=”” as String and a=0, l=0 as int.
acceptValue()
Step 1: Create Scanner object.
Step 2: Ask user to Enter a Sentence and store it in sen.
checkLongest()
Step 1: If sen.indexOf(' ')==-1 then stre = sen and a = 1.
Step 2: Else str=sen.substring(0,sen.indexOf(' ')) and sen=sen.substring(sen.indexOf(' ')+1)
Step 3: If str.length()>l then longest=str and l=str.length().
Step 4: If a is not equal to 1, then checkLongest().
printLongest()
Step 1: Print the longest word.
main()
Step 1: Start.
Step 2: Create object for calling functions.
Step 3: Call the functions acceptValue(), checkLongest(), printLongest().
Step 4: Stop.