Comp
Comp
Comp
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
1
ALGORITHM:
Step 1 : Start
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
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
7
ALGORITHM:
void perm1:
8
Main function
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++)
{
}}
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
SAMPLE INPUT
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 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 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
19
ALGORITHM:
Step 1 : Start
Step 3 : Input the two values and store it in ‘m’ and ‘n’
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
(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
25
ALGORITHM:
Step 1 : Start
Step 4 : Declare a 2-D array of size (m*m) and store the data in it.
Display the original matrix
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
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
WORD TO BE DELETED:IS
SAMPLE OUTPUT
30
ALGORITHM:
Step 1 : Start
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 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
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
SAMPLE OUTPUT
# * * #
? # # ?
? # # ?
# * * #
35
ALGORITHM:
Step 1 : Start
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
SAMPLE INPUT
m = 100
n = 1000
SAMPLE OUTPUT
40
ALGORITHM:
Step 1 : Start
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 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
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
46
ALGORITHM:
Step 1 : Start
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 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
(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 sentences:
TO BE OR NOT TO BE.
SAMPLE OUTPUT
51
ALGORITHM:
Step 1 : Start
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 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
52
Transfer the words to a new array along with their
frequencies
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();
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
First array:(A)
Suman
Anil
Second array:(B)
Usha
Sachin
John
SAMPLE OUTPUT
58
Sorted Second array:(B)
John
Sachin
Usha
59
ALGORITHM:
Step 1 : Start
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 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++];
63
OUTPUT:
64
QUESTION 12
SAMPLE INPUT
SAMPLE OUTPUT
65
Sentence Number of Vowels/Words
1 VVVVVV
WWW
2 VVVVVVVVVVVVVVV
WWWWWWWWW
3 VVVVVVVVVVVVVVVVVVVVVVVV
WWWWWWWWWWWW
4 VVVVVVVVV
WWWWWWWWW
66
ALGORITHM:
Step 1 : Start
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 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 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.
Test your program with the sample data and some random data:
SAMPLE INPUT
SAMPLE OUTPUT
NO PALINDROMIC WORDS
72
ALGORITHM:
Step 1 : Start
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 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:
Test your program with the sample data and some random data:
SAMPLE INPUT
BE GOOD TO OTHERS
SAMPLE OUTPUT:
Length: 6
Rearranged Sentence:
Length: 4
Rearranged Sentence:
BE GOOD OTHERS TO
77
ALGORITHM:
Step 1 : Start
Step 5 : Using (.hasMoreTokens), store the tokens into the array words
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
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 3 : Input the number and store it in ‘s’ . Store the value of ‘s’ in
‘temp’
Step 5 : Begin a while loop with condition (n>0) and find the number of
digits . Store it in ‘c’
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 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.
Function isprime:
Step 3 : Begin a for loop i=2 until c (the passed value) . Find the
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
SAMPLE INPUT
SAMPLE OUTPUT
91
God Is Great
92
ALGORITHM:
Step 1 : Start
Step 4 : Declare a string array of size ‘c’ and store the tokens in it
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 3 : Input the size of the array and store it in ‘m’ and ‘n’
100
else
d=d+a[j][i];
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
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 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 : 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
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 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 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
Sentence 1:
Java is a programming language.
Sentence 2:
It is a simple language.
SAMPLE OUTPUT
116
ALGORITHM:
Step 1 : Start
Step 4 : Input the sentences . The sentences are stored in the array
and then concatenated into one single sentence
Step 6 : Store the tokens in a new variable ‘s1’ in the reverse order
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];
}
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
SAMPLE OUTPUT
121
ALGORITHM:
Step 1 : Start
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 3 : Input the starting limit and ending limit and store it in ‘s’
and ‘e’
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
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
132
ALGORITHM:
Step 1 : Start
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 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();
}
137
OUTPUT:
138
QUESTION 25
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
140
ALGORITHM:
Step 1 : Start
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 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++];
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];
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
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 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 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
154
ALGORITHM:
Step 1 : Start
Step 5 : Convert the digits to string and concate it with other digits
and store it in ‘b’
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
135=11+32+53
=135
158
ALGORITHM:
Step 1 : Start
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 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
SAMPLE OUTPUT
163
ALGORITHM:
Step 1 : Start
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 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
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
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 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