Index
Sl. Programs Teacher’s Signature
1. Prime-Adam integer.
2. Unique-Digit integer.
3. Evil number.
4. Fibonacci String Sequence
5. Finding Day for a date
6. 2D Array bubble sort.
7. Bouncy number.
8. Goldbach number.
9. ConsChange
10. Filling a 2D matrix by characters.
Find number with required sum of
11. digits
Rotating a matrix and finding sum of
12. corner
Finding number of vowels and
13. consonants in Vowels
14. Circular Prime
15. Matrix rearrangement
Finding words beginning and ending
16. with a vowel
17. Packing boxes in cartons
18. Caesar Cipher
19. Sorting rows in a matrix
20. Printing names of teams vertically.
21. Matrix Rotation
22. Symmetric Matrix
23. Mirror Matrix
24. Prime Palindrome
25. Word Sorting
Internal External
QUESTION 21
Write a program to declare a square matrix A[ ][ ] of order MxM where ‘M’ is the number of rows and the
number of columns, such that M must be greater than 2 and less than 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 90° clockwise as shown below:
Original matrix Rotated matrix
123 741
456 852
789 963
(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:
Example 1
INPUT :
M=3
123
456
789
OUTPUT :
ORIGINAL MATRIX
123
456
789
MATRIX AFTER ROTATION
741
852
963
Sum of the corner elements = 20
Example 2
INPUT :
M=3
123
456
789
OUTPUT :
ORIGINAL MATRIX
123
456
789
MATRIX AFTER ROTATION
741
852
963
Sum of the corner elements = 18
Example 3
INPUT :
M = 14
OUTPUT :
SIZE OUT OF RANGE
Example 4
INPUT :
M = 112
N = 130
OUTPUT :
INVALID INPUT
SOLUTION
ALGORITHM:-
Start
Input the size of matrix.
If size less than 3 and greater than 9 display error message.
Input the elements of matrix.
Print the original matrix.
Rotate the matrix and print the rotated matrix.
Store the sum of variables in a matrix and print it.
PROGRAM:-
import java.util.*;//
class mat
{
public static void main()
{
Scanner sc=new Scanner(System.in);
System.out.print("Enter the size of the matrix : ");
int m=sc.nextInt();
if(m<3 || m>9)
System.out.println("Size Out Of Range");
else
{
int A[][]=new int[m][m];
System.out.println("Enter the elements : ");
/* Inputting the matrix */
for(int i=0;i<m;i++)
{
for(int j=0;j<m;j++)
{
A[i][j]=sc.nextInt();
}
}
/* Printing the original matrix */
System.out.println("*********");
System.out.println(
"The Original Matrix is : ");
for(int i=0;i<m;i++)
{
for(int j=0;j<m;j++)
{
System.out.print(A[i][j]+"\t");
}
System.out.println();
}
System.out.println("*********");
int B[][]=new int[m][m];
int x;
/*Rotation of matrix begins here */
for(int i=0;i<m;i++)
{
x = m-1;
for(int j=0;j<m;j++)
{
B[i][j]=A[x][i];
x--;
}
}
/* Printing the rotated matrix */
System.out.println("Matrix After Rotation is : ");
for(int i=0;i<m;i++)
{
for(int j=0;j<m;j++)
{
System.out.print(B[i][j]+"\t");
}
System.out.println();
}
System.out.println("*********");
int sum = A[0][0]+A[0][m-1]+A[m-1][0]+A[m-1][m-1]; // Finding sum of corner elements
System.out.println("Sum of the corner elements = "+sum);
}
}
}
VARIABLE DESCRIPTION:-
VARIABLE DATA TYPE DESCRIPTION
A[] int To store the elements of matrix
m int To store the size of matrix
B[] int To store the elements of rotated matrix
i int Used as a loop variable
j int Used as a loop variable
OUTPUT:
QUESTION 22
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 (i)th row and (j)th column is equal to the element of the
(i)th row and (j)th 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:
Example 1
INPUT : M=3
1 2 3
2 4 5
3 5 6
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
Example 2
INPUT : M=4
7 8 9 2
4 5 6 3
8 5 3 1
7 6 4 2
OUTPUT :
ORIGINAL MATRIX
7 8 9 2
4 5 6 3
8 5 3 1
7 6 4 2
THE GIVEN MATRIX IS NOT SYMMETRIC
The sum of the left diagonal = 17
The sum of the right diagonal = 20
Example 3
INPUT : M = 22
OUTPUT : THE MATRIX SIZE IS OUT OF RANGE
SOLUTION
ALGORITHM
Start
Input the original matrix
Print the original matrix
Check if the elements of the matrix are identical or not
Store the sum of left and right diagonal elements in a separate variable
Display the result
End
PROGRAM
import java.util.*;
public class Symmetric
{
public static void main()
{
Scanner in=new Scanner(System.in);
int m,i,j,flag=0,ld=0,rd=0;
System.out.println("Enter size of matrix : ");
m=in.nextInt();
if((m>2)&&(m<10))
{
int a[][]=new int[m][m];
System.out.println("Enter Elements in the matrix : ");
for(i=0;i<m;i++)
{
for(j=0;j<m;j++)
{
a[i][j]=in.nextInt();
}
}
System.out.println("ORIGINAL MATRIX");
for(i=0;i<m;i++)
{
for(j=0;j<m;j++)
{
System.out.print(a[i][j]+"\t");
}
System.out.println();
}
for(i=0;i<m;i++)
{
for(j=0;j<m;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");
for(i=0;i<m;i++)
{
ld=ld+a[i][i];
rd=rd+a[i][m-1-i];
}
System.out.println("The sum of the left diagonal : "+ld);
System.out.println("The sum of the right diagonal : "+rd);
}
else
System.out.println("THE MATRIX SIZE IS OUT OF RANGE");
}
}
VARIABLE DESCRIPTION:-
VARIABLE DATA TYPE DESCRIPTION
m int To store the size of matrix
a[] int Array variable to store the original matrix
f int Used as a flag variable
ld int To store the sum of left diagonal elements
rd int To store the sum of right diagonal elements
i int Used as a loop variable
OUTPUT
QUESTION 23
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:
Example 1
INPUT : M = 3
4 16 12
8 2 14
4 1 3
OUTPUT :
ORIGINAL MATRIX
4 16 12
8 2 14
4 1 3
MIRROR IMAGE MATRIX
12 16 4
14 2 8
3 1 4
Example 2
INPUT : M = 22
OUTPUT : SIZE OUT OF RANGE
ALGORITHM:
Start
Input the size of matrix
Store the original matrix in a separate variable
Create the mirror image of the matrix by storing elemnts from the last cell of the row in a separate
variable
Display the original and mirror image matrix
End
PROGRAM
import java.util.*;
class mirror
{
public static void main()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter the size of the square matrix :");
int m=sc.nextInt();
if(m<2||m>20)
{
System.out.println("SIZE OUT OF RANGE");
}
else
{
int b[][]=new int[m][m];
int a[][]=new int[m][m];
for(int i=0;i<m;i++)
{
for(int j=0;j<m;j++)
{
a[i][j]=sc.nextInt();
}
}
for(int i=0;i<m;i++)
{ int k=2;
for(int j=0;j<m;j++)
{
b[i][j]=a[i][k];
k--;
}
}
System.out.println("ORIGINAL MATRIX");
for(int i=0;i<m;i++)
{
for(int j=0;j<m;j++)
{
System.out.print(a[i][j]+" ");
}
System.out.println();
}
System.out.println("MIRRIOR IMAGE");
for(int i=0;i<m;i++)
{
for(int j=0;j<m;j++)
{
System.out.print(b[i][j]+" ");
}
System.out.println();
}
}
}
VARIABLE DESCRIPTION:-
VARIABLE DATA TYPE DESCRIPTION
m int To store the size of matrix
a[] int Array variable to store the original matrix
b[] int Array variable to store the mirror image of the
matrix
i int Used as a loop variable
j int Used as a loop variable
OUTPUT
QUESTION 24
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>=100 and n<= 3000. Display 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:
Example 1:
INPUT:
M=100
N=1000
OUTPUT: The prime palindrome integers are:
101,131,151,181,191,313,351,373,383,727,757,787,797,919,929
Frequency of prime palindrome integers: 15
Example 2:
INPUT:
M=100
N=5000
OUTPUT: Out of Range
SOLUTION
ALGORITHM
Start
Input the limits
Check for the range
Create a boolean function checkPrimePal to reverse the number and to check whether it is a prime number
or not
If it returns true,increment the counter variable by 1
Display the frequency and the prime palindrome integers
PROGRAM
import java.util.*;
class PrimeP
{
public void showPrimePal()
{
Scanner sc=new Scanner(System.in);
int m,n, c=0;
System.out.println("Enter the lower Limit:");
m=sc.nextInt();
System.out.println("Enter the Upper Limit:");
n=sc.nextInt();
if(m>n||n>3000)
System.out.println("Out of Range.");
else
{
System.out.println("The Prime Palindrome integers are:");
while(m<=n)
{
if(checkPrimePal(m))
{
if(c==0)
System.out.print(m);
else
System.out.print(" , "+m);
c++;
}
m++;
}
System.out.println("\nFrequency of Prime Palindrome integers: "+c);
}
}
boolean checkPrimePal(int x)
{
boolean flag=false;
int c=0, temp=x;
int rev=0,rem=0;
for(int i=1;i<=x;i++)
{
if(x%i==0)
c++;
}
if(c<=2)
{
while(x!=0)
{
rem=x%10;
rev=rev*10+rem;
x/=10;
}
if(rev==temp)
flag=true;
}
return flag;
}
public static void main()
{
PrimeP ob=new PrimeP ();
ob.showPrimePal();
}
}
VARIABLE DESCRIPTION:-
VARIABLE DATA TYPE DESCRIPTION
m int To store the lower limit
n int To store the higher limit
c int Used as a counter variable
rev int To store the reversed number
OUTPUT
QUESTION 25
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:
(i) Obtain the length of the sentence. (Measured in words)
(ii) Arrange the sentence in alphabetical order of the words.
Test your program with the sample data and some random data:
Example 1:
INPUT: NECESSITY IS THE MOTHER OF INVENTION.
OUTPUT:
Length: 6
Rearranged Sentence:
INVENTION IS MOTHER NECESSITY OF THE
Example 2:
INPUT: BE GOOD TO OTHERS.
OUTPUT:
Length: 4
Rearranged Sentence: BE GOOD OTHERS TO
SOLUTION
ALGORITHM
Start
Input the sentence
Extract the words
Sort the words
Store the words in a separate variable
Display the rearranged sentence
End
PROGRAM
import java.util.*;
class WordsInSentence
{
public void take()
{
Scanner sc=new Scanner(System.in);
String str, words[], stk="";
int i,j,c=0,flag, len;
char ch;
while(true)
{
flag=0;
System.out.println("Enter the sentence:");
str=sc.nextLine();
len= str.length();
words=new String[len];
for(i=0;i<len;i++)
{
if(Character.isLowerCase(str.charAt(i)))
{
flag=1;
break;
}
}
if (flag==0)
break;
else
System.out.println("Enter the sentence again with all uppercase letters");
}
i=0;
while(i<len)
{
ch=str.charAt(i);
if(ch==' '|| ch=='.' || ch=='?' || ch=='!')
{
words[c]=stk;
c++;
i++;
stk="";
}
else
{
stk+=ch;
i++;
}
}
for(i=0;i<c;i++)
{
for(j=0;j<c-1;j++)
{
if((words[j].compareTo(words[j+1]))>0)
{
stk=words[j];
words[j]=words[j+1];
words[j+1]=stk;
}
}
}
System.out.println("Length= "+c);
System.out.println("\nRearranged Sentence:\n");
for(i=0;i<c;i++)
System.out.print(words[i]+" ");
}
public static void main()
{
WordsInSentence ob=new WordsInSentence();
ob.take();
}
}
VARIABLE DESCRIPTION:-
VARIABLE DATA TYPE DESCRIPTION
i int Used as a loop variable
n2 int To store the value of I multiplied by 2
str string To store the original sentence
c int Used as a counter variable
f Int Used as a loop variable
OUTPUT