Вы находитесь на странице: 1из 173

QUESTION 1

Write a program to declare a square matrix A[ ] [ ] of order (M x M) 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:

SAMPLE INPUT

M=3
4 16 12
8 2 14
4 1 3

SAMPLE OUTPUT

ORIGINAL MATRIX
4 16 12
8 2 14
4 1 3

MIRROR IMAGE MATRIX


12 16 4
14 2 8
3 1 6

1
ALGORITHM:

Step 1 : Start

Step 2 : Declare integer variables M,i,j

Step 3 : Input the size of the array and store it in ‘M’ . If M is


between 2 and 10, proceed to the next step or print the
message,”Out of range”

Step 4 : Declare a 2-D array of size (M*M) and store the data in it.
Display the original matrix

Step 5 : Store the mirror image of the original matrix in a new matrix B of
the same size using the condition (B[i][j]=A[i][M-1-j])

Step 6: End

2
PROGRAM:

import java.util.*;
class q1
{
public static void main()
{ Scanner sc=new Scanner(System.in);
int M,i,j;
System.out.print("INPUT: M = ");
M=sc.nextInt();
if(M>2&&M<20)
{
int A[][]=new int[M][M];
System.out.println("Enter the data for the array");
for(i=0;i<M;i++)
{
for(j=0;j<M;j++)
{
A[i][j]=sc.nextInt();
}}
int B[][]=new int[M][M];
System.out.println("\nOUTPUT:");
System.out.println("ORGINAL MATRIX");
for(i=0;i<M;i++)
{
for(j=0;j<M;j++)
{
System.out.print(A[i][j]+"\t ");
B[i][j]=A[i][M-1-j];

3
}System.out.println();
}
System.out.println("MIRROR IMAGE MATRIX");
for(i=0;i<M;i++)
{
for(j=0;j<M;j++)
{
System.out.print(B[i][j]+"\t ");
}System.out.println();
}}
else
System.out.println("Size out of range");
}}

4
OUTPUT:

5
QUESTION 2

We would like to generate all possible anagrams of a word.For example


“TOP” has 6 possible anagrams:
TOP
TPO
OPT
OTP
PTO
POT
Anagrams must be printed only once.You may output the anagram in any
order.Also,output the total number of anagrams.You may also assume that the
number of letters,N, in the word will be seven, that is N<=7.
Test your program for the given data and some random data.

SAMPLE INPUT

LEAN

SAMPLE OUTPUT

LEAN
LENA
LAEN
LANE
LNEA
LNAE
EANL

6
EALN
ELAN
ENLA
ELNA
ENAL
ALNE
ALEN
ANLE
ANEL
AENL
AELN
NLEA
NLAE
NELA
NEAL
NALE
NAEL

Total number of anagrams=24

7
ALGORITHM:

void perm1:

Step 1 : A static variable count = 0 is declared outside this function


to find the number of anagrams

Step 2 : Two string parameters prefix and s is present in the function to


obtain the string from the main function

Step 3 : The length of the string is found and stored in ‘n’

Step 4 : A if statement is started with the condition (n==0).


Until the condition gets true the else part executes

Step 5 : In the else part , Begin a for loop i=0 until n .


Inside the for loop call the same function again(recursive
function) .This is how it looks
for (i=0;i<n;i++)
{
perm1(prefix + s.charAt(i), s.substring(0, i) +
s.substring(i+1, n));
}
Step 6 : The recursive function continues until the length of the string
becomes zero.

8
Main function

Step 1 : Declare string variable w

Step 2 : Input the word and store it in ‘w’

Step 3 : Pass the string to void perm1

9
PROGRAM:

import java.util.*;
class Q2
{
static int count=0;
public static void perm1(String prefix, String s)
{

int i,n;
n = s.length();
if (n == 0)
{
prefix=prefix.toUpperCase();
count++;
System.out.println(prefix);
}
else
{
for (i=0;i<n;i++)
{

perm1(prefix + s.charAt(i), s.substring(0, i) + s.substring(i+1, n));

}}

10
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
String w;
System.out.println("Enter the word");
w=sc.next();
perm1("",w);
System.out.println("No of anagrams="+count);
}
}

11
OUTPUT:

12
QUESTION 3

The potential of a word is found by adding the ASCII value of the alphabets.

Example : BALL
Potential = 66+65+76+76 = 283

Write a program to accept a sentence which may be terminated


by either “.”,”?’ or “!” only. The words of sentence are seperated by single
blank space and are in UPPER CASE . Decode the words according to their
potential and arrange them in ascending order of their potential strength.
Test your program with the following data and some random data:

SAMPLE INPUT

HOW DO YOU DO?

LOOK BEFORE YOU LEAP.

SAMPLE OUTPUT

HOW = 238
DO = 147
YOU = 253
DO = 147

13
LOOK = 309
BEFORE = 435
YOU = 253
LOOP = 290

14
ALGORITHM :

Step 1 : Start

Step 2 : Declare integer variables l , k , le , I , j , h , sum=0 , temp ;


string variable w , t ; char variables

Step 3 : Input the sentence and store in ‘w’ and check whether it ends
with . Or ! Or ? . If it is true , proceed to the next step or display
the message “Invalid input”

Step 4 : Create a stringtokenizer object ‘sb’ to extract tokens from the


sentence

Step 5 : Count the number of tokens and store it in ‘k’. Create a string
array M of size k for storing the tokens . Also create a integer
array of the same size for storing the potential of each word

Step 6 : Begin a for loop i=0 until k. Begin another for loop j=0 until the
length of each token i.e. m[i].length(). Then extract each
characters one by one and convert them to their ASCII equivalent.
Then add the values of all the characters in the word to find the
potenial .

Step 7 : Likewise find the potential of each word and store it in an integer
array. Sort the words in the ascending order according to their
potential .

Step 8 : End

15
PROGRAM :

import java.util.*;
class Q4
{
public static void main()
{
Scanner sc=new Scanner(System.in);
String w,t;
int l,k,le,i,j,h,sum=0,temp;
char ch,o;
System.out.println("INPUT : ");
w=sc.nextLine();
l=w.length();
ch=w.charAt(l-1);
if(ch=='.'||ch=='?'||ch=='!')
{
w=w.substring(0,l-1);
StringTokenizer sb=new StringTokenizer(w);
k=sb.countTokens();
int p[]=new int[k];
String m[]=new String[k];
System.out.println("\nOUTPUT : ");
for(i=0;i<k;i++)
{
m[i]=sb.nextToken();
}
for(i=0;i<k;i++)
{

16
for(j=0;j<m[i].length();j++)
{
h=(int)m[i].charAt(j);
sum=sum+h;
}
p[i]=sum;
sum=0;
}

for(i=0;i<k;i++)
{
System.out.println(m[i]+ "" +"="+""+p[i] );
}
for(i=0;i<k;i++)
{
for(j=0;j<k-i-1;j++)
{
if(p[j]>p[j+1])
{
temp=p[j];
p[j]=p[j+1];
p[j+1]=temp;
t=m[j];
m[j]=m[j+1];
m[j+1]=t;
}}}
for(i=0;i<k;i++)
System.out.print(m[i]+"");
}}}

17
OUTPUT:

18
QUESTION 4

Given two positive numbers M and N, such that M is between 100 and 10000
and N is less than 100. Find the smallest integer that is greater than M and
whose digits add up to N. For example, if M=100 and N=11, then the
smallest integer greater than 100 whose digits add up to 11 is 119.
Write a program to accept the numbers M and N from the user and print the
smallest required number whose sum of all its digits is equal to N. Also, print
the total number of digits present in the required number. The program
should check for the validity of the inputs and display an appropriate message
for an invalid input.
Test your program with the sample data and some random data:

SAMPLE INPUT

M = 100
N = 11

M = 1500
N = 25

SAMPLE OUTPUT

The required number = 119


Total number of digits = 3

The required number = 1699


Total number of digits = 4

19
ALGORITHM:

Step 1 : Start

Step 2 : Declare integer variables n,m,s,no,temp,t=0,r

Step 3 : Input the two values and store it in ‘m’ and ‘n’

Step 4 : If m is between 100 and 10000 , and n is less than 100


proceed to the next step or display the message,”size out of range”

Step 5 : The value of m is stored in another variable. Begin a do-while


loop with the test condition while(s!=n),(s is the sum of the digits
of ‘m’ )

Step 6 : With an another while loop inside the first loop, we extract
the digits of ‘m’ and find it’s sum using the condition :
r=t%10;
s=s+r;
t = t/10

Step 7 : The iterations continue till the value of n is equal to the sum.

Step 8 : End

20
PROGRAM:

import java.util.*;
class q4
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
int n,m,s,no,temp,t=0,r;
System.out.println("INPUT :");
System.out.print(" M = ");
m=sc.nextInt();
System.out.print(" N = ");
n= sc.nextInt();
if(m>=100&&m<=10000&&n<100){
temp=m;
do{
t=temp++;
s = 0;
no = 0;
while(t > 0){
r=t%10;
s=s+r;
t=t/10;
no++;
}
}while(s != n);
System.out.println(“\nOutput :”);
System.out.println("The required number = "+(temp-1));

21
System.out.println("Total number of digits = "+(no));
}
else
System.out.println("INVALID INPUT");
}}

22
OUTPUT:

23
QUESTION 5

Write a program to declare a square matrix a[][] of order M × M where ‘M’ is


the number of rows and the number of 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) Rotate the matrix 900 clockwise as shown below:

Original matrix Rotated matrix


1 2 3 7 4 1
4 5 6 8 5 2
7 8 9 9 6 3

(c) Find the sum of the elements of the four corners of the matrix.
Test your program for the following data and some random data:

SAMPLE INPUT

M=3

3 4 9
2 5 8
1 6 7

24
SAMPLE OUTPUT

ORIGINAL MATRIX
3 4 9
2 5 8
1 6 7

MATRIX AFTER ROTATION


1 2 3
6 5 4
7 8 9

Sum of the corner elements = 20

25
ALGORITHM:

Step 1 : Start

Step 2 : Declare integer variables m,i,j,sum=0

Step 3 : Input the size of matrix and store it in ‘m’.If it is between 2


and 10 , proceed to the next step or display the message ,
“Size out of range”.

Step 4 : Declare a 2-D array of size (m*m) and store the data in it.
Display the original matrix

Step 5 : By using two loops i and j , we get to rotate the matrix.


In j loop we give j the initial value of m-1 and set the test
condition as j>=0 while decreasing the value of j in each
iteration

Step 6 : For finding the sum of boundary elements we use the


condition if((i==0&&j==0)||(i==0&&j==m-1)||(i==m-
1&&j==0)||(i==m-1&&j==m-1))

Step 7 : End

26
PROGRAM:

import java.util.*;
class nq5
{
public static void main()
{
Scanner sc=new Scanner(System.in);
int m,i,j,sum=0;
System.out.print("INPUT : M = ");
m=sc.nextInt();
if(m>2&&m<10)
{
int matrix[][]=new int[m][m];
System.out.println("ENTER THE MATRIX ELEMENTS");
for(i=0;i<m;i++)
{
for(j=0;j<m;j++)
matrix[i][j] = sc.nextInt();
}
System.out.println("\nOUTPUT:");
System.out.println("\nTHE ORIGINAL MATRIX");
for(i=0;i<m;i++)
{
for(j=0;j<m;j++) {
System.out.print(matrix[i][j] + "\t ");
}
System.out.println();
}

27
System.out.println("\nMATRIX AFTER ROTATION");
for(i=0;i<m;i++)
{
for(j=m-1;j>=0;j--)
{
System.out.print(matrix[j][i]+ "\t ");
}
System.out.println();
}
for(i=0;i<m;i++)
{
for(j=0;j<m;j++)
{
if((i==0&&j==0)||(i==0&&j==m-1)||(i==m-1&&j==0)||(i==m-1&&j==m-1))
sum=sum+matrix[i][j];
}}
System.out.println("\nSUM OF CORNER ELEMENTS="+sum);
}
else
System.out.println("SIZE OUT OF RANGE");
}}

28
OUTPUT:

29
QUESTION 6

Write a program to accept a sentence which may be terminated by either


‘.’,’?’ or ’!’ only. Any other characters may be ignored.

The words may be seperated by more than one blank space and are in
uppercase.
Perform the following task:

 Accept the sentence and reduce all the blank spaces between two words
to a single blank space.

 Accept a word from the user which is part of the sentece along with its
position number and delete the word and display the sentence.

Test your program for the given data and some random data.

SAMPLE INPUT

SENTENCE: A MORNING WALK IS A IS BLESSING FOR THE WHOLE


WORLD.

WORD TO BE DELETED:IS

WORD POSITION IN THE SENTENCE:6

SAMPLE OUTPUT

A MORNING WALK IS A BLESSING FOR THE WHOLE WORLD.

30
ALGORITHM:

Step 1 : Start

Step 2 : Declare integer variables l , p , c , j , count=0 ; string


variables s , w , s1 , ns =” “and char variable ch

Step 3 : Input the sentence and store it in ‘s’

Step 4 : Input the word to be deleted and store it in ‘w’

Step 5 : Input the position of the word to be deleted and store it in ‘p’

Step 6 : Find the character at the end of the sentence . If it is ‘.’ or ‘?’ or ‘!’
, proceed to the next step. Otherwise print the message “ Invalid
input”

Step 7 : Create a stringtokenizer object sb

Step 8 : Using a while loop , while (sb.hasMoreTokens()) . Check whether


the word is not equal to ‘w’.If it is not equal to , store that token in
a new variable

Step 9 : Along with the above conditions , check whether the count is
equal to the position of the word

Step 10 : After storing all the words using the above condition, display
the new string

Step 11 : End of algorithm

31
PROGRAM:

import java.util.*;
class q6_
{
public static void main()
{
Scanner sc=new Scanner(System.in);
String s,w,s1,ns="";
int l,p,c,j,count=0;
char ch;
System.out.println("INPUT :");
System.out.print("\nSENTENCE: ");
s=sc.nextLine();
System.out.print("WORD TO BE DELETED= ");
w=sc.next();
System.out.print("WORD POSITION IN SENTENCE= ");
p=sc.nextInt();
l=s.length();
ch=s.charAt(l-1);
if(ch=='.'||ch=='?'||ch=='!')
{
StringTokenizer sb=new StringTokenizer(s);
while(sb.hasMoreTokens())
{
s1=sb.nextToken();
count++;
if(s1.equals(w)==false||count!=p)
ns=ns+""+s1;

32
}
System.out.println("\nOUTPUT:");
System.out.println(ns);
}
else
System.out.println("INVALID INPUT");
}}

33
OUTPUT:

34
QUESTION 7

Given a square matrix M [ ] [ ] of order ‘n’. The maximum value possible for
‘n’ is 10. Accept three different characters from the keyboard and fill the
array according to the instruction given below.

Fill the upper and lower elements formed by the intersection of the diagonals
by character 1.
Fill the left and right elements formed by the intersection of the diagonals by
character 2.
Fill both the diagonals by character 3.
Output the result in format given below:

SAMPLE INPUT

ENTER SIZE : 4

FIRST CHARACTER : ‘*’

SECOND CHARACTER : ‘?’

THIRD CHARACTER : ‘#’

SAMPLE OUTPUT

# * * #
? # # ?
? # # ?
# * * #

35
ALGORITHM:

Step 1 : Start

Step 2 : Declare integer variables n , i , j ; char variables c , c1 , c2

Step 3 : Input the size of the array and store it in ‘n’

Step 4 : Declare 2-D char array of size (n*n)

Step 5 : Input the characters and store it in c , c1 , c2

Step 6 : The diagonals are filled by character 3 using the condition


if((i==j)||(i+j==(n-1)).The upper and lower elements formed by
the intersection of the diagnols is filled by character 1 using the
condition if(i>j)&&(i+j>=(n-1)||(j>i)&&(i+j)<(n-1)

Step 7 : The 3rd character is filled simply using an else condition at


the last

Step 8 : End

36
PROGRAM:

import java.util.*;
class q7
{
public static void main()
{
Scanner sc=new Scanner(System.in);
int n,i,j;
char c,c1,c2;
System.out.println("INPUT : ");
System.out.print("ENTER SIZE = ");
n=sc.nextInt();
char M[][]=new char[n][n];
System.out.print("\nFIRST CHARACTER : ");
c = sc.next().charAt(0);
System.out.print("SECOND CHARACTER : ");
c1 = sc.next().charAt(0);
System.out.print("THIRD CHARACTER : ");
c2 = sc.next().charAt(0);
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
if((i==j)||(i+j==(n-1)))
M[i][j]=c2;
else if(((i>j)&&(i+j>=(n-1)))||((j>i)&&(i+j)<(n-1)))
M[i][j]=c;
else

37
M[i][j]=c1;
}}
System.out.println("\nOUTPUT : ");
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
System.out.print(M[i][j]+"\t");
}
System.out.println();
}
}}

38
OUTPUT:

39
QUESTION 8

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 INPUT

m = 100
n = 1000

SAMPLE OUTPUT

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

40
ALGORITHM:

Step 1 : Start

Step 2 : Declare integer variables m , n , i , j , k , no=0 , rev =0 , d ,


o=0

Step 3 : Input the two numbers and store it in ‘m’ and ‘n’. If m and n are
greater than 3000, proceed to the next step or print the
message,”Out of range”

Step 4 : Begin a for loop with initial value ‘ m’ and test expression
i<=n . If the value of ‘i’ is prime , we reverse the digits of
the number

Step 5 : After reversing , check whether the reversed number is equal to


the original number . If yes, print the number. Likewise all the
numbers in the limit are checked and printed.

Step 6 : End

41
PROGRAM:

import java.util.*;
class q8
{
public static void main()
{
Scanner sc=new Scanner(System.in);
int m,n,i,j,k,no=0,rev=0,d,O=0;
System.out.println("INPUT:");
System.out.print("\nm = ");
m=sc.nextInt();
System.out.print("n = ");
n=sc.nextInt();
System.out.println("\nOUTPUT: ");
System.out.println("THE PRIME PALINDROME INTEGERS ARE");
if(m<3000&&n<3000)
{
for(i=m;i<=n;i++)
{
no=0;
k=i;
for(j=1;j<=k;j++)
{
if(i%j==0)
no++;
}
if(no==2)
{

42
rev=0;
while(k!=0)
{
d=k%10;
rev=rev*10+d;
k=k/10;
}
if(rev==i)
{
System.out.print(i+",");
O++;
}
}
}
System.out.println("FREQUENCY OF PRIME PALINDROME INTEGERS
: "+O);
}
else
System.out.println("OUT OF RANGE");
}}

43
OUTPUT:

44
QUESTION 9

Write a program to declare a square matrix A[ ] [ ] of order (M x M) where


‘M’ is the number of rows and the number of 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 ith row and jth
column is equal to the element of the jth row and ith column.
(c) Find the sum of the elements of left diagonal and the sum of the elements
of right diagonal of the matrix and display them.
Test your program with the sample data and some random data:

SAMPLE INPUT
 M=3

1 2 3
2 4 5
3 5 6

 M = 22

45
SAMPLE OUTPUT

ORIGINAL MATRIX

1 2 3
2 4 5
3 5 6

THE GIVEN MATRIX IS SYMMETRIC

The sum of the left diagonal = 11


The sum of the right diagonal = 10

THE MATRIX SIZE IS OUT OF RANGE

46
ALGORITHM:

Step 1 : Start

Step 2 : Declare integer variables M , i , j , k , ld=0 , rd=0 , flag=0

Step 3 : Input the array size and store it in ‘M’ . If M is between 2 and 10,
proceed to the next step or print the message,”Out of range”

Step 4 : Declare a 2-D array of size (M*M) and store the data in it.
Display the original matrix

Step 5 : Using the condition if(A[i][j]!=A[j][i])


flag=1;
break;
we can check whether the matrix is symmetric

Step 6 : Using the condition if(i+j==M-1),we can find the sum of the
elements in the right diagonal
Using the condition if(i==j), we can find the sum of the
elements in the left diagonal

Step 7 : End

47
PROGRAM:

import java.util.*;
class q9
{
public static void main()
{
Scanner sc=new Scanner(System.in);
int M,i,j,ld=0,rd=0,flag=0;;
System.out.print("INPUT: M = ");
M=sc.nextInt();
System.out.println("ENTER THE DATA ");
if(M>2&&M<10)
{
int A[][]=new int[M][M];
for(i=0;i<M;i++)
{
for(j=0;j<M;j++)
{
A[i][j]=sc.nextInt();
}}
System.out.println("\nOUTPUT: ");
System.out.println("ORGINAL ARRAY");
for(i=0;i<M;i++)
{
for(j=0;j<M;j++)
{
System.out.print(A[i][j]+"\t");
}

48
System.out.println();}

for(i=0;i<M;i++)
{
for(j=0;j<M;j++)
{
if(i==j)
ld=ld+A[i][j];
if(i+j==M-1)
rd=rd+A[i][j];
if(A[i][j]!=A[j][i])
{ flag=1;
break;

}
}}
if(flag==0)
System.out.println("THE GIVEN MATRIX IS SYMMETRIC");
else
System.out.println("THE GIVEN MATRIX IS NOT SYMMETRIC");
System.out.println("The sum of the left diagnol="+ld);
System.out.println("The sum of the right diagnol="+rd);
}
else
System.out.println("MATRIX SIZE IS OUT OF RANGE");
}}

49
OUTPUT:

50
QUESTION 10

Input a paragraph containing 'n' number of sentences where (1<=n<=4). The


words are to be separated with single blank space and are in upper case. A
sentence may be terminated either with a full stop (.) or question mark (?).
Perform the followings:

(i) Enter the number of sentences, if it exceeds the limit show a message.
(ii) Find the number of words in the paragraph
(iii) Display the words in ascending order with frequency.

SAMPLE INPUT

Enter number of sentences:


1

Enter sentences:
TO BE OR NOT TO BE.

SAMPLE OUTPUT

Total number of words: 6


WORD FREQUENCY
OR 1
NOT 1
TO 2
BE 2

51
ALGORITHM:

Step 1 : Start

Step 2 : Declare integer variables l,c=0,j,i,no=0,t=0,k=0; string


variables ns="",temp="",s=""; char variable ch

Step 3 : Input the number of sentences and store it in ‘n’ . Check whether n
is between 1 and 4 . If yes , go to step 4 else print the message,
“Invalid Input”

Step 4 : Input the sentence and store it in ‘s’. Create a


stringtokenizer object st and extract the tokens

Step 5 : Declare two integer arrays “freqA”,”freqB”. Declare two


String arrays ‘A’ and ‘B’ . All the arrays are of size “no”

Step 6 : Begin a for loop i=0 until no. Initialize the frequency array
freqA. Begin another for j=0.Check whether the words in
different positions are equal. If they are equal update the
frequency value

Step 7 : Begin two for loops and check whether the words at
different positions are equal .If they are equal , make the
frequency at that position equal to 0

Step 8 : Using the condition :


if(freqA[i]!=0)

52
Transfer the words to a new array along with their
frequencies

Step 9 : Sort the words in the array by comparing their frequencies


Use any standard sorting technique

Step 10 : Print the words along with their frequencies

Step 11: End

53
PROGRAM:

import java.util.*;
import java.io.*;
class Question10
{
public static void main(String args[]) throws IOException
{
BufferedReader br=new BufferedReader(new
InputStreamReader(System.in));
String ns="",temp="",s="";
char ch;
int l,c=0,j,i,no=0,t=0,k=0;
System.out.println("INPUT :");
System.out.print("Enter number of sentences: ");
int n=Integer.parseInt(br.readLine());
if(n<1||n>4)
{
System.out.println("Invalid input");
}
else
{
System.out.print("Enter sentences: ");
s=br.readLine();

StringTokenizer st =new StringTokenizer(s, " .?,");


no=st.countTokens();
System.out.println("\nOUTPUT : ");
System.out.println("Total number of words: "+no);

54
String A[]=new String[no];
int freqA[]=new int[no];
String B[]=new String[no];
int freqB[]=new int[no];
for(i=0;i<no;i++)
{
A[i]=st.nextToken();
}

for(i=0;i<no;i++)
{
freqA[i]=0;
for(j=0;j<no;j++)
{
if(A[i].equals(A[j]))
freqA[i]++;
}}
for(i=0;i<no-1;i++)
{
for(j=i+1;j<no;j++)
{
if(A[i].equals(A[j]))
freqA[j]=0;
}
}
k=no;
for(i=0;i<k;i++)
{
if(freqA[i]!=0)

55
{
B[c]=A[i];
freqB[c]=freqA[i];
c++;
}
}
for(i=0;i<c;i++)
{
for(j=0;j<c-i-1;j++)
{
if(freqB[j]>freqB[j+1])
{
t=freqB[j+1];
freqB[j+1]=freqB[j];
freqB[j]=t;
temp=B[j+1];
B[j+1]=B[j];
B[j]=temp;
}
}
}
System.out.println("WORD\tFREQUENCY");
for(i=0;i<c;i++)
{
System.out.println(B[i]+"\t"+freqB[i]);
}5

56
OUTPUT:

57
QUESTION 11

Write a program that inputs the names of people into two 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 array A and B into a
single array C, such that the resulting array is sorted alphabetically.
Display all the three arrays, A, B and C, sorted alphabetically.

Test your program for the given data and some random data.

SAMPLE INPUT

Enter number of names in Array A, N=2


Enter number of names in Array B, M=3

First array:(A)
Suman
Anil

Second array:(B)
Usha
Sachin
John

SAMPLE OUTPUT

Sorted first array:(A)


Anil
Suman

58
Sorted Second array:(B)
John
Sachin
Usha

Sorted Merged array:(C)


Anil
John
Sachin
Suman
Usha

59
ALGORITHM:

Step 1 : Start

Step 2 : Declare integer variables M , N , i ,j

Step 3 : Input the value of N and M and store it in ‘N’ and ‘M’.Declare
three string arrays A[],B[],C[] of size N , M and (N+M)
respectively

Step 4 : Enter the names into both the arrays.

Step 5 : Sort both the arrays using bubble sort respectively

Step 6 : Merge the sorted arrays into a single array

Step 7 : Display the sorted arrays and merged array

Step 8 : End

60
PROGRAM:

import java.util.*;
class q11
{
public static void display()
{
Scanner sc=new Scanner(System.in);
int M,N,i,j;
String temp="";
System.out.println("INPUT :");
System.out.print("\nEnter number of names in Array A,N = ");
N=sc.nextInt();
System.out.print("Enter number of names in Array B,M = ");
M=sc.nextInt();
String A[]=new String[N];
String B[]=new String[M];
String C[]=new String[M+N];
System.out.println("\nFirst array:(A)");
for(i=0;i<N;i++)

A[i]=sc.next();

System.out.println("Second array:(B)");
for(i=0;i<M;i++)

B[i]=sc.next();

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

61
for(i=0;i<N;i++)
{

for(j=i+1;j<N;j++)
{
if(A[i].compareTo(A[j])>0)
{
temp=A[i];
A[i]=A[j];
A[j]=temp;
}

}}
for(i=0;i<M;i++)
{

for(j=i+1;j<M;j++)
{
if(B[i].compareTo(B[j])>0)
{
temp=B[i];
B[i]=B[j];
B[j]=temp;
}
}}
int x=0;
i=0;
j=0;
while(i!=N && j!=M)

62
{
if(A[i].compareTo(B[j])<=0)
C[x++]=A[i++];
else
C[x++]=B[j++];
}
while(i<N)

C[x++]=A[i++];

while(j<M)

C[x++]=B[j++];

System.out.println("\nSorted first array : (A)");


for(i=0;i< N;i++)
System.out.println(A[i]);
System.out.println("\nSorted second array : (B)");
for(i=0;i< M;i++)
System.out.println(B[i]);
System.out.println("\nSorted Merged Array : (C)");
for(i=0;i<M+N;i++)
System.out.println(C[i]);
}}

63
OUTPUT:

64
QUESTION 12

A sentence is terminated either by “.”, “!” 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 letters.
Write a program to:
1. Obtain the length of the sentence (measured in words) and the frequency of
vowels in each sentence.
2. Generate the output as shown below using the given data.

SAMPLE INPUT

Hello! How are you? Hope everything is fine. Best of luck.

SAMPLE OUTPUT

Sentence Number of Vowels Number of Words


1 2 1
2 5 3
3 8 4
4 3 3

65
Sentence Number of Vowels/Words

1 VVVVVV

WWW

2 VVVVVVVVVVVVVVV

WWWWWWWWW

3 VVVVVVVVVVVVVVVVVVVVVVVV

WWWWWWWWWWWW

4 VVVVVVVVV

WWWWWWWWW

66
ALGORITHM:

Step 1 : Start

Step 2 : Declare integer variables l,i,j=0,k=0; string variables s,


ns ="" ; char variable ch

Step 3 : Declare a 2-d string array of size 10

Step 4 : Input the sentence and store it in ‘s’. Then using charAt
function extract the letters and concat to form words. Then
store all the words int the array

Step 5 : Declare two integer arrays of size k to store the number of


words and number of vowels (k is the number of words)

Step 6 : Begin two for loops i and j . The first loop has a limit till ‘k’ ,
while the second loop has a limit till ‘l’ (l being length of
each word)

Step 7 : Find the number of vowels using the condition if(ch=='A'||


ch=='E'||ch=='I'||ch=='O'||ch=='U')

Step 8 : Find the number of words using the condition if(ch=='')

Step 9 : Display the output using for loops for both vowels and words
vowels :for(j=0;j<vowels[i];j++)
System.out.print("VVV");

67
words : for(j = 0; j < word[i]; j++)
System.out.print("WWW");

Step 10 : End

68
PROGRAM:

import java.util.*;
class q12
{
public static void main()
{
Scanner sc=new Scanner(System.in);
String s,ns="";
int l,i,j=0,k=0;
String words[]=new String[10];
char ch;
System.out.println("INPUT:");
s=sc.nextLine();
l=s.length();
for(i=0;i<l;i++)
{
ch=s.charAt(i);
ns=ns+ch;
if(ch=='!'||ch=='?'||ch=='.')
{
words[k]=ns;
ns="";
k++;
}
}
int vowels[]=new int[k];
int word[]=new int[k];
for(i=0;i<k;i++)

69
{l=words[i].length();
for(j=0;j<l;j++)
{
ch=words[i].charAt(j);
if(ch=='A'||ch=='E'||ch=='I'||ch=='O'||ch=='U'||ch=='a'||ch=='e'||ch=='i'||ch=='o'|
|ch=='u')
vowels[i]++;
else if(ch=='')
word[i]++;
}}
System.out.println("\nOUTPUT:");
System.out.println("\nSentence\tNo. of Vowels\tNo. of Words");
for(i=0;i<k;i++)
{
System.out.print((i+1)+"\t\t"+vowels[i]+"\t\t"+"");
System.out.print("\t"+word[i]+"\t\t");
System.out.println();
}
System.out.println("\nSentence\tNo. of Vowels/Words");
for(i=0;i<k;i++){
System.out.print((i+1)+"\t\t");
for(j=0;j<vowels[i];j++)
System.out.print("VVV");
System.out.println();
System.out.print("\t\t");
for(j = 0; j < word[i]; j++)
System.out.print("WWW");
System.out.println();
}}}

70
OUTPUT:

71
QUESTION 13

A palindrome is a word that may be read the same way in either direction.
Accept a sentence in uppercase 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.

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

SAMPLE INPUT

 MOM AND DAD ARE COMING AT NOON.

 HOW ARE YOU?

SAMPLE OUTPUT

 MOM DAD NOON

NUMBER OF PALINDROMIC WORDS: 3

 NO PALINDROMIC WORDS

72
ALGORITHM:

Step 1 : Start

Step 2 : Declare integer variables c=0, i , j , n; string variables s ,


rev=” “

Step 3 : Input the sentence and store it in ‘s’

Step 4 : Create a StringTokenizer object ‘st’ to extract tokens (words) from


the sentence using space and punctuation spaces “ . , ? ! “

Step 5 : Declare a string array of size n (number of tokens)

Step 6 : Begin a for loop i=0 until ‘n’.Create a stringbuffer object


with the token. Use reverse function and reverse the string

Step 7 : Check whether the reversed string is equal to the original token
and then print it . If it is not equal , then continue step 6 , till any
palindromic words are found

Step 8 : Print the palindromic words and the number of palindromic


words

Step 9 : End

73
PROGRAM:

import java.util.*;
class Q13
{
public static void main(String args[])
{
Scanner sc= new Scanner(System.in);
System.out.print("INPUT: ");
String s, rev="";
int c=0, i,j,n;
s=sc.nextLine();
s=s.toUpperCase();
StringTokenizer st =new StringTokenizer(s," .,?!");
n=st.countTokens();
String a[]=new String[n];
System.out.print("OUTPUT: ");
for(i=0;i<n;i++)
{
a[i]=st.nextToken();
StringBuffer sb=new StringBuffer(a[i]);
sb.reverse();
rev=sb.toString();
if(a[i].equals(rev))
{
System.out.print(a[i]+"");
c++;
}
}

74
if(c==0)
System.out.print("NO PALINDROMIC WORDS\n");
else
System.out.println("\nNUMBER OF PALINDROMIC WORDS: "+ c);
}}

75
OUTPUT:

76
QUESTION 14

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:

1. Obtain the length of the sentence (measured in words)


2. Arrange the sentence in alphabetical order of the words.

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

SAMPLE INPUT

 NECESSITY IS THE MOTHER OF INVENTION.

 BE GOOD TO OTHERS

SAMPLE OUTPUT:

 Length: 6

Rearranged Sentence:

INVENTION IS MOTHER NECESSITY OF THE

 Length: 4

Rearranged Sentence:

BE GOOD OTHERS TO

77
ALGORITHM:

Step 1 : Start

Step 2 : Declare integer variables i , j , n , k=0 ; string variables s ;


character variable ch

Step 3 : Input the sentence and store it in ‘s’.Create a StringTokenizer


object ‘sb’ to extract tokens (words) from the sentence using space

Step 4 : Count the number of tokens (words) and store it in an


integer variable ‘c’.Also create String array words[ ] of size‘c’

Step 5 : Using (.hasMoreTokens), store the tokens into the array words

Step 6 : Compare the length of the strings and sort them in


descending order using bubble sort

Step 7 : Display the new array

Step 8 : End

78
PROGRAM:

import java.util.*;
class nq14
{
public static void main(String args[])
{
int i,j,n,k=0;
String s;
char ch;
Scanner sc = new Scanner(System.in);
System.out.print("INPUT: ");
s=sc.nextLine();
s=s.replace(".","");
s=s.trim();
StringTokenizer sb=new StringTokenizer(s);
int c=sb.countTokens();
String words[]=new String[c];
while(sb.hasMoreTokens())
{
words[k]=sb.nextToken();
k++;
}
System.out.println("\nOUTPUT: ");
System.out.println("LENGTH="+c);
for (i = 0; i < words.length- 1; i++)
{
for (j = 0; j < words.length - i - 1; j++)
{

79
if (words[j].compareTo(words[j + 1]) > 0)
{
String temp = words[j];
words[j] = words[j + 1];
words[j + 1] = temp;
}
}
}
System.out.print("Rearranged Sentence: ");
for (i = 0; i < c; i++)
{
System.out.print(words[i] + "");
}
}}

80
OUTPUT:

81
QUESTION 15

Write a Program in Java to input a number and check whether it is a Kaprekar


number or not.
Note: A 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
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. Hence, 9 is a Kaprekar number.
Example 2: 45
452= 2025, right-hand piece of 2025 = 25 and left hand piece of 2025 = 20
Sum = 25 + 20 = 45, i.e. equal to the number. Hence, 45 is a Kaprekar
number.
Example 3:297
2972= 88209, right-hand piece of 88209 = 209 and left hand piece of 88209
= 88
Sum = 209 + 88 = 297, i.e. equal to the number. Hence, 297 is a Kaprekar
number.

82
ALGORITHM:

Step 1 : Start

Step 2 : Declare integer variables n , sq , c=0 , temp , s

Step 3 : Input the number and store it in ‘s’ . Store the value of ‘s’ in
‘temp’

Step 4 : Find the square of the number and store in ‘sq’

Step 5 : Begin a while loop with condition (n>0) and find the number of
digits . Store it in ‘c’

Step 6 : Using math.pow function , find the power of 10 with which


we can obtain the left hand piece and right hand piece

Step 7 : Store the right hand piece in ‘ex’ (ex=sq % p)


Store the left hand piece in ‘el’ (el = sq/p)=

Step 8 : Add es and el and check whether it is equal to ‘temp’ . If it is


equal , display the message ,”The number is karprekar” or if it is
not equal display the message, “The number is not karprekar”.

Step 9 : End

83
PROGRAM:

import java.util.*;
class q15
{
public static void main()
{
Scanner sc = new Scanner(System.in);
int n,sq,c=0,temp,s;
System.out.print("Enter the number : ");
n = sc.nextInt();
temp = n;
sq = n*n;
while(n>0)
{
n = n/10;
c++;
}
int p=(int)Math.pow(10, c);
int ex=sq%p;
int el=sq/p;
s=ex+el;
if(s==temp)
System.out.println("The number is a Kaprekar number");
else
System.out.println("The number is not a Kaprekar number");
}
}

84
OUTPUT:

85
QUESTION 16

Write a Program in Java to fill a 2-D array with the first ‘m*n’ prime
numbers, where ‘m’ is the number of rows and ‘n’ is the number of columns.
For example: If rows = 4 and columns = 5, then the result should be:

2 3 5 7 11
13 17 19 23 29
31 37 41 43 47
53 58 61 67 71

86
ALGORITHM:

Main function:

Step 1 : Create the object of the class

Step 2 : Declare the integer variables m,n,i,j=0,s,c=1,k;


Input the array size and store it in ‘m’ and ‘n’

Step 3 : Declare an integer array of size(m*n)

Step 4 : Begin a for loop i=2 until m*n with the limit (j<s). Pass a
value to the function “isprime” . If the value is prime , true
value is returned and the value gets stored int the array.

Step 5 : A counter variable j =0 is declared . The for loop continues


until j<m*n

Function isprime:

Step 1 : The function has return type – boolean

Step 2 : A flag variable p = 0 is set inorder to find the number of


factors for the passed value

Step 3 : Begin a for loop i=2 until c (the passed value) . Find the

number of factors . If p=1 , return true ,else return false

87
PROGRAM:

import java.util.*;
class q16
{
public static void main(String args[])
{
q16 obj =new q16();
Scanner sc=new Scanner(System.in);
int m,n,i,j=0,s,c=1,k;
System.out.println("INPUT :");
System.out.print("m: ");
m=sc.nextInt();
System.out.print("\nn: ");
n=sc.nextInt();
s=m*n;
int b[]=new int[s];
for(i=2;j<s;i++)
{
if(obj.isprime(i))
{
b[j]=i;
j++;
}
}
System.out.println("\nOUTPUT:");
System.out.println();

88
k=0;
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
System.out.print(b[k]+"\t");
k++;
}
System.out.println();
}
}
public boolean isprime(int c)
{
int i,p=0;
for(i=2;i<=c;i++)
{
if(c%i==0)
p++;
}
if(p==1)
return true;
else
return false;
}
}

89
OUTPUT:

90
QUESTION 17

Write a program to accept a sentence which may be terminated by either ‘.’ or


‘?’ only. The words are to be separated by a single blank space. Print an error
message if the input does not terminate with ‘.’ or ‘?’. You can assume that no
word in the sentence exceeds 15 character, so that you get a proper formatted
output. Perform the following tasks:
(i) Convert the first letter of each word to uppercase.
(ii) Find the number of vowels and consonants in each word and display them
with proper headings along with the words.

Test your program with the following inputs.

SAMPLE INPUT

 Intelligence plus character is education.


 God is great.

SAMPLE OUTPUT

 Intelligence Plus Character Is Education

Word Vowels Consonants


Intelligence 5 7
Plus 1 3
Character 3 6
Is 1 1
Education 5 4

91
 God Is Great

Word Vowels Consonants


God 1 2
Is 1 1
Great 2 3

92
ALGORITHM:

Step 1 : Start

Step 2 : Declare integer variables l , p=0 , I , l1 , j; char variable c5;


string variable s

Step 3 : Input the sentence and store it in ‘s’ . Create a stringtokenizer


object sb and extract the tokens

Step 4 : Declare a string array of size ‘c’ and store the tokens in it

Step 5 : Declare three integer arrays vowels [] , consonants [] and


space[] of size ‘c’ for storing the vowel frequency ,
consonant frequency and number of spaces respectively

Step 6 : Begin a for loop i=0 until c (number of tokens) . Find the
length of the token and store it in l1

Step 7 : Begin another for loop j=0 until l1 and extract the characters
of the token . Check whether the character is equal to any
vowel and update it’s value .Check whether the character is a
consonant and update it’s value(Update the value according to
their respective arrays)

Step 8 : Display the vowel frequency and consonants frequency along with
their words

Step 9 : End

93
PROGRAM:

import java.util.*;
import java.io.*;
class q17
{
public static void main(String[] args)throws IOException
{
Scanner sc=new Scanner(System.in);
String s;
int l,p=0,i,l1,j,max=0,dif;
char c5;
System.out.print("INPUT : ");
s=sc.nextLine();
l=s.length();
c5=s.charAt(l-1);

if(c5=='.'||c5=='?')
{
StringTokenizer sb=new StringTokenizer(s," .? ");
int c=sb.countTokens();
String words[]=new String[c];
int space[]=new int[c];
while(sb.hasMoreTokens())
{
words[p]=sb.nextToken();
words[p]=words[p].substring(0,1).toUpperCase()+words[p].substring(1);
if(max<words[p].length())

94
max=words[p].length();
p++;
}
int vowels[]=new int[c];
int consonants[]=new int[c];
for(i=0;i<c;i++)
{
String S=words[i];
l1=S.length();
vowels[i]=0;
consonants[i]=0;
space[i]=max-l1;
for(j=0;j<l1;j++)
{

char ch=S.charAt(j);
if(ch=='A'||ch=='E'||ch=='I'||ch=='O'||ch=='U'||ch=='a'||ch=='e'||ch=='i'||ch=='o'|
|ch=='u')
vowels[i]++;
else if(ch!='.')
consonants[i]++;
}
}
System.out.println("\nOUTPUT : ");

for(i=0;i<c;i++)
{

System.out.print(words[i]+"");

95
for(j=words[i].length();j<space[i];j++)
words[i]=words[i]+ "";
}
System.out.println();
System.out.println("\nWord\t\tVowels\t\tConsonants");

for(i=0;i<c;i++)
{
System.out.print(words[i]);
System.out.println("\t "+vowels[i]+"\t "+consonants[i]);
}
}
else
System.out.println("INVALID INPUT");
}}

96
OUTPUT:

97
QUESTION 18

Write a program to declare an array A[][] of order MxN and must be greater
than 2 and less than 10. M indicates the number of rows and N indicates the
number of coloumns.Allow the uset to input positive integers to this
matrix.Perform the following task on the matrix.

• Calculate the sum of each row and store in its respective rows.
• Calculate the sum of each coloumn and store in it’s respective coloumn.
• Calculate the sum of only the double digit integers of the matrix.
• Display the orginal matrix , new matrix containing sum of each row and
sum of each coloumn in their respective rows and coloumn.

SAMPLE INPUT

 M=3
N=3

3 5 9
4 8 7
1 3 2

SAMPLE OUTPUT

3 5 9 17
4 8 2 19
1 3 2 6
8 16 18

98
Sum of double digits=0

 INVALID INPUT

99
ALGORITHM:

Step 1 : Start

Step 2 : Declare integer variables i , j, c=0 , d=0 , s=0 , m , n

Step 3 : Input the size of the array and store it in ‘m’ and ‘n’

Step 4 : Declare an integer array of size(m+1)*(n+1) and store the


values in it. Print the original array

Step 5 : Store the sum of the rows using the condition:


for(i=0;i<=m;i++)
{
for(j=0;j<=n;j++)
{
if(j==n)
a[i][j]=c;
else
c=c+a[i][j];

Step 6 : Store the sum of the columns using the condition:


for(i=0;i<=n;i++)
{
for(j=0;j<=m;j++)
{
if(j==n)
a[j][i]=d;

100
else
d=d+a[j][i];

Step 7 : Store the sum of double digits using the condition:


if(a[i][j]>9)

Step 8 : End

101
PROGRAM:

import java.util.*;
class q18
{
public static void main()
{
Scanner sc=new Scanner(System.in);
System.out.println("INPUT : ");
System.out.print("M = ");
int m=sc.nextInt();
System.out.print("\nN = ");
int n=sc.nextInt();
int a[][]=new int[m+1][n+1];
int i,j,c=0,d=0,s=0;
if((m>2)&&(m<10)&&(n>2)&&(n<10))
{
System.out.println("Enter the elements");
{
for(i=0;i<m;i++)
{
for(j=0;j<m;j++)
{
a[i][j]=sc.nextInt();
}}}
System.out.println("\nOUTPUT :");
System.out.println("ORGINAL ARRAY :");
for(i=0;i<m;i++)

102
{
for(j=0;j<n;j++)
{
System.out.print(a[i][j]+"\t");
}
System.out.println();
}
for(i=0;i<=m;i++)
{
for(j=0;j<=n;j++)
{
if(j==n)
a[i][j]=c;

else
c=c+a[i][j];
}
c=0;
}
for(i=0;i<=n;i++)
{
for(j=0;j<=m;j++)
{
if(j==n)
a[j][i]=d;
else
d=d+a[j][i];
}
d=0;

103
}
System.out.println("Resultant matrix");
for(i=0;i<=m;i++)
{
for(j=0;j<=n;j++)
{
if((i==m)&&(j==n))
System.out.print("");
else
System.out.print(a[i][j]+"\t");
}
System.out.println();
}
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
if(a[i][j]>9)
s=s+a[i][j];
}
}
System.out.println("Sum of double digits="+s);
}
else
System.out.println("INVALID ");
}}

104
OUTPUT:

105
QUESTION 19

Write a program to declare an array of order MxN.Then find the maximum


and minimum element of the array.

SAMPLE INPUT

M=3
N=3

2 4 9
0 1 7
0 3 2

SAMPLE OUTPUT

Maximum element=9
Minimum element=0

106
ALGORITHM:

Step 1 : Start

Step 2 : Declare integer variables m , n , i , j,max,min

Step 3 : Input the size of the array and store it in ‘m’ and ‘n’

Step 4 : Declare an integer array of size (m*n) and store the values
in it. Print the original array.
Step 5 : Initialize min=a[0][0]

Step 6 : Find the largest element using the condition :


if(a[i][j]>max)
max=a[i][j];

Step 7 : Find the smallest element using the condition :


if(a[i][j]<min)
min=a[i][j]

Step 6 : End

107
PROGRAM:

import java.util.*;
class q19
{
public static void main()
{
Scanner sc=new Scanner(System.in);
int m,n,i,j;
System.out.println("INPUT : ");
System.out.print("M = ");
m=sc.nextInt();
System.out.print("N = ");
n=sc.nextInt();
int a[][]=new int[m][n];
System.out.println("Enter values of array");
for(i=0;i<m;i++)
{
for(j=0;j<m;j++)
{
a[i][j]=sc.nextInt();
}}
System.out.println("OUTPUT : ");
System.out.println("ORGINAL ARRAY");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{

108
System.out.print(a[i][j]+"\t");
}
System.out.println();
}
int max=0;
int min=a[0][0];;

for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
if(a[i][j]>max)
max=a[i][j];
if(a[i][j]<min)
min=a[i][j];
}}
System.out.println("MAXIMUM ELEMENT="+max);
System.out.println("MINIMUM ELEMENT="+min);
}}

109
OUTPUT:

110
QUESTION 20

Write a program to declare an array of order MxN.Then find the largest


element and the second largest element in that array.

SAMPLE INPUT

M=3
N=3

3 0 1
4 8 5
9 6 3

SAMPLE OUTPUT

Largest element = 9
Second largest element = 8

111
ALGORITHM:

Step 1 : Start

Step 2 : Declare integer variables m , n , I , j , max=0 , sec=0 , b=0 , c= 0

Step 3 : Input the row size and column size and store it in m and n

Step 4 : Create a 2-D array and store the values in it . Print the
original array

Step 5 : Find the largest element using the condition if(a[i][j]>max).


During each iteration , the value of ‘max’ changes to the larger
element

Step 6 : After the largest element is found , make that element zero
in the array and then find the second largest element using
the above technique

Step 7 : End

112
PROGRAM:

import java.util.*;
class q20
{
public static void main()
{
Scanner sc=new Scanner(System.in);
int m,n,i,j,max=0,sec=0;
System.out.println("INPUT : ");
System.out.print("M = ");
m=sc.nextInt();
System.out.print("N = ");
n=sc.nextInt();
int a[][]=new int[m][n];
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
a[i][j]=sc.nextInt();
}}
System.out.println("OUTPUT : ");
System.out.println("Orginal array");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
System.out.print(a[i][j]+"\t");
}

113
System.out.println();
}
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
if(a[i][j]>max)
{
max=a[i][j];

}}}
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
if(a[i][j]==max)
{
a[i][j]=0;
}}}
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
if(a[i][j]>sec)
sec=a[i][j];
}}
System.out.println("LARGEST NUMBER="+max);
System.out.println("SECOND LARGEST NUMBER="+sec);
}}

114
OUTPUT:

115
QUESTION 21

The input of the program will consist of a number of lines of English text
containing letters of the English alphabet , punctuation , apostrophe , full
stop , comma , semicolon , colon and white space and characters. Your task is
to print the words of the text in reverse order without any punctuation other
than space.

SAMPLE INPUT

Enter the number of sentences :


2

Enter the sentences:

Sentence 1:
Java is a programming language.

Sentence 2:
It is a simple language.

SAMPLE OUTPUT

language simple a is It language programming a is Java

116
ALGORITHM:

Step 1 : Start

Step 2 : Declare the integer variables I , n .

Step 3 : Input the number of sentences and store it in ‘n’. Create a


string array of size n.

Step 4 : Input the sentences . The sentences are stored in the array
and then concatenated into one single sentence

Step 5 : Create a stringtokenizer object str for extracting the tokens


from the sentence using space and punctuation marks such
as ( . , ; : ! ?)

Step 6 : Store the tokens in a new variable ‘s1’ in the reverse order

Step 7 : Display the reversed sentences

Step 8 : End

117
PROGRAM:

import java.util.*;
import java.io.*;
class q21
{
public static void main(String args[])throws IOException
{
InputStreamReader isr=new InputStreamReader(System.in);
BufferedReader br=new BufferedReader(isr);
int i,n;
System.out.println("INPUT : ");
System.out.println("Enter the number of sentences");
n=Integer.parseInt(br.readLine());
String s[]=new String[n];
String s1="",s2="",ns="";
System.out.println("Enter the sentences");
for(i=0;i<n;i++)
{
System.out.println("Sentence"+""+(i+1) +":");
s[i]=br.readLine();
ns=ns+""+s[i];
}

StringTokenizer str=new StringTokenizer(ns," .,;:!? ");


while(str.hasMoreElements())
{
String n1=str.nextToken();
s1=n1+""+s1;

118
}
System.out.println("OUTPUT : ");
System.out.println(s1);
}}

119
OUTPUT:

120
QUESTION 22

Write a program which takes a string as an input.The words in this string are
assumed to be seperated by one or more blanks.

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.

SAMPLE INPUT

This Is Human Resource Department

SAMPLE OUTPUT

Department Resource Human This Is

121
ALGORITHM:

Step 1 : Start

Step 2 : Declare integer variables I , j , c , l , ls; string variables


st,temp="",news=""

Step 3 : Input the sentence and store it in ‘st’.Convert the sentence


into lowercase

Step 4 : Using string tokenizer, break the sentence into tokens and
store each of them in an array

Step 5 : Using two loops i and j , sort the tokens by comparing their
lengths in descending order. If the lengths of the tokens are equal ,
sort them in the alphabetical order.

Step 6 : End

122
PROGRAM:

import java.util.*;
class Question22
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
String st,temp="",news="";
int i,j,c,l,ls;
System.out.println("INPUT : ");
st=sc.nextLine();
st=st.toLowerCase();
StringTokenizer sb=new StringTokenizer(st);
c=sb.countTokens();
String s[]=new String[c];
while(sb.hasMoreTokens())
{
for(i=0;i<c;i++)
{
s[i]=sb.nextToken();
}}
for(i=0;i<c;i++)
{
for(j=0;j<c-1;j++)
{
l=s[j].length();
ls=s[j+1].length();
if(l<ls)

123
{
temp=s[j];
s[j]=s[j+1];
s[j+1]=temp;
}
else if(l==ls)
{
if((s[j].compareTo(s[j+1]))>0)
{
temp=s[j];
s[j]=s[j+1];
s[j+1]=temp;
}}}}

System.out.println("\nOUTPUT : ");
for(i=0;i<c;i++)
{
s[i]=s[i].substring(0,1).toUpperCase()+s[i].substring(1);

news=news+""+s[i];
}
news=news.replace(".","");
System.out.println(news+'.');
}}

124
OUTPUT:

125
QUESTION 23

Write a program in java to print all twin primes within given range.

Twin prime numbers are pair of numbers which are both prime and difference
is 2.Twin prime in the range 1-100 are:

(3,5),(5,7),(11,13),(17,19),(29,31),(41,43),(59,61),(71,73)

126
ALGORITHM:

Step 1 : Start

Step 2 : Declare integer variables int s , e, i , j , count=0 , k=0 , l ,


c=0

Step 3 : Input the starting limit and ending limit and store it in ‘s’
and ‘e’

Step 4 : Check if the number in the range is a prime number . Set


the counter variable as “count”

Step 5 : If the count is equal to 2 , increase the value of the number


by 2 .

Step 6 : Check whether the increased number is also a prime


number . If yes go to step 7 , else go to step 4

Step 7 : Repeat step 4 , step 5 and step 6 until the condition is false

Step 8 : If the increased number , and the original number are prime , then
display both of them

Step 9 : End

127
PROGRAM:

import java.util.*;
class twinprime
{
public static void main()
{
Scanner sc=new Scanner(System.in);
int s , e , i , j, count = 0,k=0,l,c=0;
System.out.println("Enter starting limit");
s=sc.nextInt();
System.out.println("Enter ending limit");
e=sc.nextInt();
System.out.println("Twin primes");
for(i=s;i<e;i++)
{
for(j=1;j<=i;j++)
{
if(i%j==0)
count++;
}
if(count==2)
{
k=i+2;
for(l=1;l<=k;l++)
{
if(k%l==0)
c++;
}}

128
if(c==2)
{
System.out.println(i+","+k);
}
count=0;
c=0;
k=0;
}}}

129
OUTPUT:

130
QUESTION 24

Write a program to declare a square matrix A of order MxM where M must


be greater than 3 and less than 10.Allow the user to input positive integers
into the matrix .Perform the following task on the matrix.

• Sort the non-boundary element in ascending order using any standard


sorting technique and rearrange them in the matrix.
• Calculate the sum of both the diagonals.
• Display the orginal matrix , rearranged matrix and the sum of the
diagonal elments.

SAMPLE INPUT

 M=4

9 2 1 5
8 13 8 4
15 6 3 11
7 12 23 8

SAMPLE OUTPUT:

ORIGINAL MATRIX

9 2 1 5
8 13 8 4
15 6 3 11
7 12 23 8

131
REARRANGED MATRIX

9 2 1 5
8 3 6 4
15 8 13 11
7 12 23 8

DIAGONAL ELEMENTS

9 5
3 6
8 13
7 8

SUM OF THE DIAGONAL ELEMENTS = 59

132
ALGORITHM:

Step 1 : Start

Step 2 : Declare integer variables m , n , I , j , t=0 , l=0 , sum=0

Step 3 : Input the array size and store it in ‘m’.Check whether m is


between 3 and 10.If yes proceed to the next step or
print,”Invalid input”

Step 4 : Declare a 2-D array and input the values into it and print the original
array

Step 5 : Create a 1-D array with size (m-2)*(m-2) for storing the non
boundary elements

Step 6 : Transfer the non boundary elements from 2-D array to 1-D
array using the condition if(i>0&&i<m-1&&j>0&&j<m-1)
,i and j are loop variables

Step 7 : Sort the elements in the 1-D array using bubble sort
technique and again transfer it back to the 2-D array using
the condition used earlier

Step 8 : Find the sum of the diagnols using the condition


if(i+j==(m-1)||(i==j)

Step 9 : End

133
PROGRAM:

import java.util.Scanner;
public class Question24
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
int m,n,i,j,t=0,l=0,sum=0;
System.out.println("INPUT : ");
System.out.print("M = ");
m=sc.nextInt();

if(m>3&&m<10)
{
int A[][]=new int[m][m];
System.out.println("Enter the elements of array ");
for(i=0;i<m;i++)
{
for(j=0;j<m;j++)
{
A[i][j]=sc.nextInt();
}}
System.out.println("\nOUTPUT : ");
System.out.println("ORIGINAL MATRIX");
for(i=0;i<m;i++)
{
for(j=0;j<m;j++)

134
{
System.out.print(A[i][j]+"\t");
}
System.out.println();
}
n=((m-2)*(m-2));
int B[]=new int[n];
for(i=0;i<m;i++)
{
for(j=0;j<m;j++)
{
if(i>0&&i<m-1&&j>0&&j<m-1)
{
B[l]=A[i][j];
l++;
}}}
l=0;
t=0;
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
if(B[i]>B[j])
{
t=B[j];
B[j]=B[i];
B[i]=t;
}}
t=0;

135
}
for(i=0;i<m-1;i++)
{
for(j=0;j<m-1;j++)
if(i>0&&i<m-1&&j>0&&j<m-1)
{
A[i][j]=B[l];
l++;
}
}
System.out.println("REARRANGED MATRIX");
for(i=0;i<m;i++)
{
for(j=0;j<m;j++)
{
System.out.print(A[i][j]+"\t");
}
System.out.println();
}
System.out.println("DIAGONAL ELEMENTS");
for(i=0;i<m;i++)
{
for(j=0;j<m;j++)
{
if(i+j==(m-1)||i==j)
{
sum=sum+A[i][j];
System.out.print(A[i][j]+"\t");
}

136
else
{
System.out.print("");
}
}
System.out.println();
}

System.out.println("SUM OF THE DIAGNOL ELEMENTS");


System.out.println(sum);
}
else
System.out.println("INVALID INPUT");
}}

137
OUTPUT:

138
QUESTION 25

Write a program to declare a square matrix A[][] of order MxM where M


must be greater than 2 and less than 20.Allow the user to input positive
integers into the matrix .Perform the following task on the matrix.

Allow the user to input integers into their matrix. Perform the following task
on the matrix

• Sort the elements of the outer row and column elements in ascending
order using any standard sorting technique and rearrange them in the matrix.
• Calculate the sum of the outer row and column elements.
• 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 other random data

SAMPLE INPUT

M=4

9 2 1 5
8 13 8 4
15 6 3 11
7 12 23 8

139
SAMPLE OUTPUT

ORIGINAL MATRIX
9 2 1 5
8 13 8 4
15 6 3 11
7 12 23 8

REARRANGED MATRIX

1 2 4 5
23 13 8 7
15 6 3 8
12 11 9 8

The sum of boundary elements is = 105

140
ALGORITHM:

Step 1 : Start

Step 2 : Declare integer variables r,j,i,temp,sum=0

Step 3 : Input the array size and store it in ‘r’ .Check whether r is
between 2 and 20 . If yes proceed to the next step or print,”Invalid
input”

Step 4 : Declare a 2-D array of size (r*r) and store the data in it .Print the
original array

Step 5 : Create a 1-D array of size (2*r+2*(r-2)) for storing the


boundary elements

Step 6 : Transfer the boundary elements from 2-D array to 1-D


array using the condition if(i==0||j==0||i==(m-1)||j==(n1))
, i and j are loop variables.

Step 7 : Sort the elements in the 1-D array using bubble sort
technique and again transfer it back to the 2-D array using
the following conditions:

Step 8 : The boundary elements at the top are filled using the
condition: for(i=0;i<r;i++)
A[a][i]=B[k++];

141
The right most boundary elements are filled using the
condition: for(i=1;i<r;i++)
A[i][d]=B[k++];

The bottom elements are filled using the condition:


for(i=d-1;i>=0;i--)
A[b][i]=B[k++];

The left most elements are filled using the condition:

for(i=b-1;i>=1;i--)
A[i][a]=B[k++];

Step 8 : Find the sum of the boundary elements using the condition
if(i==0||j==0||i==(m-1)||j==(n1))

Step 9 : End

142
PROGRAM:

import java.util.*;
class Question25
{
public static void main()
{
Scanner sc=new Scanner(System.in);
int r,j,i,temp,sum=0;
System.out.println("INPUT : ");
System.out.print("M = ");
r=sc.nextInt();
if(r>2&&r<20)
{
int v=0;
int A[][]=new int[r][r];

int B[]=new int[2*r+2*(r-2)];


System.out.println("Enter the data");
for(i=0;i<r;i++)
{
for(j=0;j<r;j++)
{
A[i][j]=sc.nextInt();
}}
System.out.println("\nOUTPUT : ");
System.out.println("ORIGINAL MATRIX");
for(i=0;i<r;i++)
{

143
for(j=0;j<r;j++)
{
System.out.print(A[i][j]+"\t");
}
System.out.println();
}
for(i=0;i<r;i++)
{

for(j=0;j<r;j++)
{
if(i==0||j==0||j==(r-1)||i==(r-1))
{B[v]=A[i][j];
sum=sum+A[i][j];
v++;
}}}
for(i=0;i<v;i++)
{
for(j=0;j<v-1-i;j++)
{
if(B[j]>B[j+1])
{
temp=B[j];
B[j]=B[j + 1];
B[j+1]=temp;
}
}
}
System.out.println("REARRANGED MATRIX");

144
int a=0,b=r-1,d=r-1,k=0;
for(i=0;i<r;i++)

A[a][i]=B[k++];

for(i=1;i<r;i++)
A[i][d]=B[k++];

for(i=d-1;i>=0;i--)

A[b][i]=B[k++];

for(i=b-1;i>=1;i--)
A[i][a]=B[k++];
for(i=0;i<r;i++){
for(j=0;j<r;j++){
System.out.print(A[i][j] + "\t");
}
System.out.println();
}
System.out.println("The Sum of boundary elements: " + sum);
}}}

145
OUTPUT:

146
QUESTION 26

Write a program to declare a single dimensional array A[] and a square


matrix B[][] of size n where ‘n’ is greater than 2 and less than 10.Allow the
user to input positive integers into the single dimensional array.

Perform the following task on the matrix.Sort the elements of Single


dimensional array in ascending order using any standard sorting technique
and display the sorted elements.Fill the square matrix B in the following
format.

SAMPLE INPUT

 n=4

ARRAY A:

5
2
8
1

SAMPLE OUTPUT
SORTED ARRAY:

1
2
5
8

147
ARRAY B:

1 2 5 8
1 2 5 1
1 2 1 2
1 1 2 5

148
ALGORITHM:

Step 1 : Start

Step 2 : Declare integer variables n , I , j , t=0 , l = 0

Step 3 : Input the number and store it in ‘n’

Step 4 : Check whether n is between 3 and 10. If it, is proceed to the


next step or print , ”Invalid input”.

Step 5 : Create an 1-D array of size n and a 2-D array of size (n*n).
Input the elements into 1-D array and display the original
matrix

Step 6 : Sort the elements of 1-D array using bubble sort technique.
Fill the 2-D array with the elements of the sorted array.

Step 7 : The elements of the right diagonal and above it are stored
using the condition if{i+j<(n-1)||(i+j++(n-1))

Step 8 : The other elements are stored using the condition


if(B[i][j]==0)

Step 9 : End

149
PROGRAM:

import java.util.*;
class Question26
{
public static void main()
{
Scanner sc=new Scanner(System.in);
int n,i,j,t=0,l=0;
System.out.println("INPUT : ");
System.out.print("n = ");
n=sc.nextInt();
if(n>2&&n<10)
{
int A[]=new int[n];
int B[][]=new int[n][n];
System.out.println("Enter the data");
for(i=0;i<n;i++)
{
A[i]=sc.nextInt();
}
System.out.println("ARRAY A ");
for(i=0;i<n;i++)
System.out.println(A[i]);
System.out.println("\nOUTPUT : ");
System.out.println("SORTED ARRAY : ");
for(i=0;i<n;i++)
{
for(j=0;j<n-i-1;j++)

150
{
if(A[j]>A[j+1])
{
t=A[j];
A[j]=A[j+1];
A[j+1]=t;
}}}
for(i=0;i<n;i++)
System.out.println(A[i]);
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
if((i+j)<(n-1)||(i+j)==(n-1))
{
B[i][j]=A[l];
l++;
}}
l=0;
}
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
if(B[i][j]==0)
{
B[i][j]=A[l];
l++;
}}

151
l=0;
}
System.out.println("ARRAY B :");
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
System.out.print(B[i][j]+"\t");
}
System.out.println();
}
}
else
System.out.println("INVALID INPUT");
}}

152
OUTPUT:

153
QUESTION 27

An evil number is a positive whole number which has even number of i’s in
it’s binary equivalent . For example, binary equivalent of 9 is 1001 which
contains even numbers of 1.

Design a program to accept a positive whole number and find the binary
equivalent of the number and count the numbers of 1’s in it and display
whether it is an evil number or not with an appropriate message.

SAMPLE INPUT

 n=15

 n=26

SAMPLE OUTPUT

 Binary equivalent=1111

number of 1’s =4

15 is an evil number.

 Binary equivalent=11010

number of 1’s =3

26 is not an evil number.

154
ALGORITHM:

Step 1 : Start

Step 2 : Input the number and store it in integer variable n

Step 3 : Declare integer variables l , r and string variable b and


initialize them.

Step 4 : Find the remainder of the number after dividing it with 2.


If it is equal to 1 , increase the value of the flag variable l

Step 5 : Convert the digits to string and concate it with other digits
and store it in ‘b’

Step 6 : if the flag variable is divisible by 2, it is an evil


number . Otherwise print , “Wrong input”

Step 7 : End

155
PROGRAM:

import java.util.*;
class Question27
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
System.out.println("INPUT : ");
System.out.print("n = ");
int n=sc.nextInt();
int l=0,r=0,p;
p=n;
String b="";
while(n>0)
{
r=n%2;
b=Integer.toString(r)+b;
if(r==1)
l++;
n=n/2;
}
System.out.println("\nOUTPUT: ");
System.out.println("Binary equivalent="+b);
System.out.println("Number of ones="+l);
if(l%2==0)
System.out.println(p+" is an evil number");
else
System.out.println(p+" is not an evil number");
}}

156
OUTPUT:

157
QUESTION 28

A dissarium number is a number in which sum of digits to the power of


their respective position is equal to the number itself.

135=11+32+53
=135

Hence 135 is a dissarium number . Design a class to check whether a given


number is dissarium number or not.

158
ALGORITHM:

Step 1 : Start

Step 2 : Input the number and store it in integer variable n

Step 3 : Declare integer variables p , r = 0, d , t , b and double


variable a and initialize it.

Step 4 : Extract the digits of the number using the condition t=p%10
and find the number of digits using a flag variable r.

Step 5 : Find the sum of the digits raised to the power of their
position using the condition a+ Math.pow(t,r)) (a is
initialized above)

Step 6 : Then decrease the count of the number of the digits.

Step 7 : Check whether the sum is equal to the number . If yes ,


print “It is a dissarium number”.Otherwise print “ Invalid
input”.

Step 8 : End

159
PROGRAM:

import java.util.*;
class Question28
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter the number");
int n=sc.nextInt();
int p,r=0,d,t,b;
double a=0.0;
p=n;
b=n;
while(n>0)
{
d=n%10;
r++;
n=n/10;
}
while(p>0)
{
t=p%10;
a=a+Math.pow(t,r);
r--;
p=p/10;
}
if(a==b)
System.out.println("It is a dissarium number");

160
else
System.out.println("It is not a dissarium number");
}}

161
OUTPUT:

162
QUESTION 29

Read a single sentence with a full stop.The words are to be seperated with
asingle 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 occuring first in the input sentence
should come first.Both input and output must begin with uppercase.Test your
program for the data and some random data.

SAMPLE INPUT

 The lines are printed in reverse order.

 Print the sentence in ascending order

SAMPLE OUTPUT

 In the are lines order printed reverse.

 In the print order sentence ascending

163
ALGORITHM:

Step 1 : Start

Step 2 : Declare integer variables i,j=0 , l , p=0 , v=0 , t ; char


variables a , b , c='' , d='' ; string variable s ,s1 , s2 ,temp

Step 3 : Input the sentence and store it in ‘s’. Convert the string to
lowercase using toLowerCase()

Step 4 : Begin for loop i=0 until l (length of string) . Using charAt
function , check if the characters are equal to blankspace or dot
and find the number of words.

Step 5 : Declare a string array of size p (p = number of words).Split


the sentences into words and store them in the array
using substring function

Step 6 : Sort the words contained in the sentence according to it’s


length in ascending order using bubble sort

Step 7 : If two words are of same length then the word occuring first in the
input sentence should come first . Check the position of the letter
in the original sentence and then store it

Step 8 : End

164
PROGRAM:

import java.util.*;
class Question29
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
System.out.print("INPUT : ");
String s=sc.nextLine();
s=s.toLowerCase();
int i,j=0,l,p=0,v=0,t;
char a,b,c='',d='';
String s1="",s2="",tmp="" ;
s=s+"";
l=s.length();
for(i=0;i<l;i++)
{
a=s.charAt(i);
if(a==''||a=='.')

p++;

}
String str[]=new String[p];
for(i=0;i<l;i++)
{
a=s.charAt(i);

165
if(a==''||a=='.')
{
str[j]=s.substring(v,i);
j++;
v=i+1;
}
}
for(i=0;i<p;i++)
{
for(j=0;j<p-1;j++)
{

if(str[j].length()==str[j+1].length())
{
c=str[j].charAt(0);
d=str[j+1].charAt(0);
}
if(s.indexOf(c)>s.indexOf(d))
{
tmp=str[j];
str[j]=str[j+1];
str[j+1]=tmp;
}
else if(str[j].length()>str[j+1].length())
{
tmp=str[j];
str[j]=str[j+1];
str[j+1]=tmp;
}

166
}}
for(i=0;i<p;i++)
{
s2=s2+""+str[i];
}
s2=s2.trim();
s2=s2.substring(0,1).toUpperCase()+s2.substring(1);
System.out.print("\nOUTPUT : "+ s2+ ".");
}}

167
OUTPUT:

168
QUESTION 30

An ISBN is a 10 digit code which uniquely identifies a book.The first 9 digits


represents 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 9 digits of the code can take a value between 0 and 9 ,
sometimes it is necessary to make the last digit =10, this is done by writing
the last digit as X.

To verify an ISBN , calculate 10 times the 10 times the first digit, 9 times the
second digit 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.

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 divided 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 = 176

Since 176 leaves no remainder when divided by 11, hence it is a valid ISBN.

169
ALGORITHM:

Step 1 : Start

Step 2 : Input the ISBN number and store it in string variable ‘p’

Step 3 : Find the length of the string and check whether it is


10 and then proceed

Step 4 : Declare and initialize variables a = 0 , sum = 0 , and


k=10.

Step 5 : Start a for loop i= 0 until k

Step 6 : Using the statement, Character.toString(p.charAt(i) we


get the characters of the string. Here ‘i’ is the loop
variable

Step 7 : We find the sum by multiplying each of the digits with


k (k=10) and adding them ,while decreasing the value of
k in each iteration

Step 8 : Check if the sum is divisible by 11. If yes print the


statement “ It is a valid ISBN” .

Step 9 : Otherwise print “Invalid ISBN”.

Step 10 : End

170
PROGRAM:

import java.util.*;
class ISBN
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter the ISBN no");
String p=sc.nextLine();
int l=p.length();
if(l==10)
{
String ch;
int a,sum=0,k=10,i;
for(i=0;i<l;i++)
{
ch=Character.toString(p.charAt(i));
if((ch.equalsIgnoreCase("x")))
a=10;
else
a=Integer.parseInt(ch);
sum=sum+a*k;
k--;
}
System.out.println("sum="+sum);
if(sum%11==0)
System.out.println("Valid ISBN");
else

171
System.out.println("Not valid");
}
else
System.out.println("Invalid input");
}}

172
OUTPUT:

173