Computer Science Project25-1
Computer Science Project25-1
Class: XII
Q1.
A Goldbach number is a positive even integer that can be expressed as the sum of
two odd primes.
Note: All even integer numbers greater than 4 are Goldbach numbers.
Example:
6=3+3
10 = 3 + 7
10 = 5 + 5
Hence, 6 has one odd prime pair 3 and 3. Similarly, 10 has two odd prime pairs, i.e.
3 and 7, 5 and 5.
Write a program to accept an even integer 'N' where N > 9 and N < 50. Find all the
odd prime pairs whose sum is equal to the number 'N'.
Test your program with the following data and some random data:
Example 1
INPUT:
N = 14
OUTPUT:
PRIME PAIRS ARE:
3, 11
7, 7
Example 2
INPUT:
N = 17
OUTPUT:
INVALID INPUT. NUMBER IS ODD.
Example 3
INPUT:
N = 126
OUTPUT:
INVALID INPUT. NUMBER OUT OF RANGE.
Solution:
import java.util.Scanner;
int N = sc.nextInt();
if (N % 2 != 0)
else
int complement = N - i;
// Check if i is prime
int iDivisors = 0;
{
if (i % j == 0)
iDivisors++;
int complementDivisors = 0;
if (complement % j == 0)
complementDivisors++;
}
Q2
A Prime-Adam integer is a positive integer (without leading zeros) which is a prime
as well as an Adam number.
Prime number: A number which has only two factors, i.e. 1 and the number itself.
Example: 2, 3, 5, 7 ... etc.
Adam number: The square of a number and the square of its reverse are reverse to
each other.
Example: If n = 13 and reverse of 'n' = 31, then,
(13)2 = 169
(31)2 = 961 which is reverse of 169
thus 13, is an Adam number.
Accept two positive integers m and n, where m is less than n as user input. Display
all Prime-Adam integers that are in the range between m and n (both inclusive) and
output them along with the frequency, in the format given below:
Test your program with the following data and some random data:
Example 1
INPUT:
m=5
n = 100
OUTPUT:
THE PRIME-ADAM INTEGERS ARE:
11 13 31
FREQUENCY OF PRIME-ADAM INTEGERS IS: 3
Example 2
INPUT:
m = 100
n = 200
OUTPUT:
THE PRIME-ADAM INTEGERS ARE:
101 103 113
FREQUENCY OF PRIME-ADAM INTEGERS IS: 3
Example 3
INPUT:
m = 50
n = 70
OUTPUT:
THE PRIME-ADAM INTEGERS ARE:
NIL
FREQUENCY OF PRIME-ADAM INTEGERS IS: 0
Example 4
INPUT:
m = 700
n = 450
OUTPUT:
INVALID INPUT
Solution:
import java.util.Scanner;
int m = sc.nextInt();
int n = sc.nextInt();
// Validate input
System.out.println("INVALID INPUT");
} else {
int frequency = 0;
int divisorCount = 0;
if (num % i == 0) {
divisorCount++;
}
// Proceed only if the number is prime
if (divisorCount == 2) {
int reverse = 0;
original /= 10;
int reverseOfReverseSquare = 0;
temp /= 10;
if (numSquare == reverseOfReverseSquare) {
frequency++;
}
}
if (frequency == 0) {
System.out.println("NIL");
System.out.println();
}
Q3.
Write a program to declare a matrix A[][] of order (M x N) where 'M' is the number of
rows and 'N' is the number of columns such that the value of 'M' must be greater
than 0 and less than 10 and the value of 'N' must be greater than 2 and less than 6.
Allow the user to input digits (0 - 7) only at each location, such that each row
represents an octal number.
Example:
2 3 1 (decimal equivalent of 1st row = 153 i.e. 2x82 + 3x81 + 1x80)
2. Calculate the decimal equivalent for each row and display as per the format
given below.
Test your program for the following data and some random data:
Example 1:
INPUT:
M=1
N=3
ENTER ELEMENTS FOR ROW 1: 1 4 4
OUTPUT:
1 4 4 100
Example 2:
INPUT:
M=3
N=4
ENTER ELEMENTS FOR ROW 1: 1 1 3 7
ENTER ELEMENTS FOR ROW 2: 2 1 0 6
ENTER ELEMENTS FOR ROW 3: 0 2 4 5
OUTPUT:
FILLED MATRIX DECIMAL EQUIVALENT
1 1 3 7 607
2 1 0 6 1094
0 2 4 5 165
Example 3:
INPUT:
M=3
N=3
ENTER ELEMENTS FOR ROW 1: 2 4 8
OUTPUT:
INVALID INPUT
Example 4:
INPUT:
M=4
N=6
OUTPUT:
OUT OF RANGE
Solution:
import java.util.Scanner;
int M = sc.nextInt();
int N = sc.nextInt();
// Input validation
System.out.println("OUT OF RANGE");
} else {
matrix[i][j] = sc.nextInt();
invalidInput = true;
}
if (invalidInput) {
System.out.println("INVALID INPUT");
} else {
System.out.println("FILLED MATRIX");
System.out.println();
System.out.println("DECIMAL EQUIVALENT");
int decimalValue = 0;
int power = N - 1;
power--;
System.out.println(decimalValue);
}
}
}
Q4.
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 and are in UPPER CASE.
1. Check for the validity of the accepted sentence only for the terminating
character.
2. Arrange the words in ascending order of their length. If two or more words
have the same length, then sort them alphabetically.
Test your program for the following data and some random data:
Example 1:
INPUT:
AS YOU SOW SO SHALL YOU REAP.
OUTPUT:
AS YOU SOW SO SHALL YOU REAP.
AS SO SOW YOU YOU REAP SHALL
Example 2:
INPUT:
SELF HELP IS THE BEST HELP.
OUTPUT:
SELF HELP IS THE BEST HELP.
IS THE BEST HELP HELP SELF
Example 3:
INPUT:
BE KIND TO OTHERS.
OUTPUT:
BE KIND TO OTHERS.
BE TO KIND OTHERS
Example 4:
INPUT:
NOTHING IS IMPOSSIBLE#
OUTPUT:
INVALID INPUT
Solution:
import java.util.*;
class Stringsort {
void main() {
// Check if the input ends with a valid punctuation ('.', '?', or '!')
System.out.println("INVALID INPUT");
return;
System.out.println(inp);
// Remove the terminating punctuation and split the sentence into words
// Bubble sort for arranging words by length, and alphabetically if lengths are
equal
temp = arr[j];
arr[j + 1] = temp;
swapped = true;
temp = arr[j];
arr[j + 1] = temp;
swapped = true;
if (!swapped)
break;
}
Q5.
Write a program to declare a square matrix A[][] of order (M × M) where 'M' must be
greater than 3 and less than 10. Allow the user to input positive integers into this
matrix. Perform the following tasks on the matrix:
1. Sort the non-boundary elements in ascending order using any standard sorting
technique and rearrange them in the matrix.
3. Display the original matrix, rearranged matrix and only the diagonal elements
of the rearranged matrix with their sum.
Test your program for the following data and some random data:
Example 1
INPUT:
M=4
9 2 1 5
8 13 8 4
15 6 3 11
7 12 23 8
OUTPUT:
ORIGINAL MATRIX
9 2 1 5
8 13 8 4
15 6 3 11
7 12 23 8
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
Example 2
INPUT:
M=5
7 4 1 9 5
8 2 6 10 19
13 1 3 5 1
10 0 5 12 16
1 8 17 6 8
OUTPUT:
ORIGINAL MATRIX
7 4 1 9 5
8 2 6 10 19
13 1 3 5 1
10 0 5 12 16
1 8 17 6 8
REARRANGED MATRIX
7 4 1 9 5
8 0 1 2 19
13 3 5 5 1
10 6 10 12 16
1 8 17 6 8
DIAGONAL ELEMENTS
7 5
0 2
5
6 12
1 8
SUM OF THE DIAGONAL ELEMENTS = 46
Example 3
INPUT:
M=3
OUTPUT:
THE MATRIX SIZE IS OUT OF RANGE
.Solution:
import java.util.*;
class SumDiag {
void main() {
int M = sc.nextInt();
return;
matrix[i][j] = sc.nextInt();
System.out.println("ORIGINAL MATRIX");
System.out.print(matrix[i][j] + "\t");
System.out.println();
nonBoundaryElements.add(matrix[i][j]);
Collections.sort(nonBoundaryElements);
int index = 0;
matrix[i][j] = nonBoundaryElements.get(index++);
System.out.println("REARRANGED MATRIX");
System.out.println();
int primaryDiagonalSum = 0;
int secondaryDiagonalSum = 0;
primaryDiagonalSum += matrix[i][i];
System.out.println("DIAGONAL ELEMENTS");
if (i == j || j == M - i - 1) {
System.out.print(matrix[i][j] + "\t");
} else {
System.out.print("\t");
System.out.println();
}
}
Q6.
Write a program to accept a sentence which may be terminated by either '.', '?' or '!'
only. The words may be separated by more than one blank space and are in UPPER
CASE.
2. Place the words which begin and end with a vowel at the beginning, followed
by the remaining words as they occur in the sentence.
Test your program with the sample data and some random data:
Example 1
INPUT:
ANAMIKA AND SUSAN ARE NEVER GOING TO QUARREL ANYMORE.
OUTPUT:
NUMBER OF WORDS BEGINNING AND ENDING WITH A VOWEL = 3
ANAMIKA ARE ANYMORE AND SUSAN NEVER GOING TO QUARREL
Example 2
INPUT:
YOU MUST AIM TO BE A BETTER PERSON TOMORROW THAN YOU ARE TODAY.
OUTPUT:
NUMBER OF WORDS BEGINNING AND ENDING WITH A VOWEL = 2
A ARE YOU MUST AIM TO BE BETTER PERSON TOMORROW THAN YOU TODAY
Example 3
INPUT:
LOOK BEFORE YOU LEAP.
OUTPUT:
NUMBER OF WORDS BEGINNING AND ENDING WITH A VOWEL = 0
LOOK BEFORE YOU LEAP
Example 4
INPUT:
HOW ARE YOU@
OUTPUT:
INVALID INPUT
Solution:
import java.util.*;
class StringVowel {
void main() {
inp = sc.nextLine();
System.out.println("INVALID INPUT");
return;
inp = inp.replaceAll("\\s+", " "); // Replace multiple spaces with a single space
vowelCount++;
} else {
nonVowelCount++;
System.out.println();
}
Q7.
Design a program to accept a day number (between 1 and 366), year (in 4 digits)
from the user to generate and display the corresponding date. Also, accept 'N' (1 <=
N <= 100) from the user to compute and display the future date corresponding to
'N' days after the generated date. Display an error message if the value of the day
number, year and N are not within the limit or not according to the condition
specified.
Test your program with the following data and some random data:
Example 1
INPUT:
DAY NUMBER: 255
YEAR: 2018
DATE AFTER (N DAYS): 22
OUTPUT:
DATE: 12TH SEPTEMBER, 2018
DATE AFTER 22 DAYS: 4TH OCTOBER, 2018
Example 2
INPUT:
DAY NUMBER: 360
YEAR: 2018
DATE AFTER (N DAYS): 45
OUTPUT:
DATE: 26TH DECEMBER, 2018
DATE AFTER 45 DAYS: 9TH FEBRUARY, 2019
Example 3
INPUT:
DAY NUMBER: 500
YEAR: 2018
DATE AFTER (N DAYS): 33
OUTPUT:
DAY NUMBER OUT OF RANGE
Example 4
INPUT:
DAY NUMBER: 150
YEAR: 2018
DATE AFTER (N DAYS): 330
OUTPUT:
DATE AFTER (N DAYS) OUT OF RANGE
Solution:
import java.util.*;
class DateCalculator {
void main() {
return;
System.out.println("INVALID YEAR");
return;
}
if (nDays < 1 || nDays > 100) {
return;
new int[]{31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31} :
new int[]{31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
int month = 0;
int dayOfMonth = 0;
month = i;
dayOfMonth = dayNumber;
break;
} else {
dayNumber -= monthDays[i];
}
// Display the original date
dayNumber += nDays;
return;
int futureMonth = 0;
int futureDay = 0;
futureMonth = i;
futureDay = dayNumber;
break;
} else {
dayNumber -= monthDays[i];
}
// Helper function to get the month name
return months[month];
} }
Q8.
Write a program to declare a single-dimensional array a[] and a square matrix b[][]
of size N, where N > 2 and N < 10. Allow the user to input positive integers into the
single dimensional array.
1. Sort the elements of the single-dimensional array in ascending order using any
standard sorting technique and display the sorted elements.
1258
1251
1212
1125
Test your program for the following data and some random data:
Example 1
INPUT:
N=3
ENTER ELEMENTS OF SINGLE DIMENSIONAL ARRAY: 3 1 7
OUTPUT:
SORTED ARRAY: 1 3 7
FILLED MATRIX
137
131
113
Example 2
INPUT:
N = 13
OUTPUT:
MATRIX SIZE OUT OF RANGE
Solution:
import java.util.*;
class MatrixArrayOperations {
void main() {
int N = sc.nextInt();
return;
a[i] = sc.nextInt();
Arrays.sort(a);
System.out.println();
System.out.println("FILLED MATRIX:");
System.out.print(b[i][j]);
System.out.println();
}
Q9.
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 and are in uppercase.
(b) Convert the non-palindrome words of the sentence into palindrome words by
concatenating the word by its reverse (excluding the last character).
Example:
The reverse of the word HELP would be LEH (omitting the last alphabet) and by
concatenating both, the new palindrome word is HELPLEH. Thus, the word HELP
becomes HELPLEH.
Note: The words which end with repeated alphabets, for example ABB would
become ABBA and not ABBBA and XAZZZ becomes XAZZZAX.
[Palindrome word: Spells same from either side. Example: DAD, MADAM etc.]
(c) Display the original sentence along with the converted sentence.
Test your program for the following data and some random data:
Example 1
INPUT:
THE BIRD IS FLYING.
OUTPUT:
THE BIRD IS FLYING.
THEHT BIRDRIB ISI FLYINGNIYLF
Example 2
INPUT:
IS THE WATER LEVEL RISING?
OUTPUT:
IS THE WATER LEVEL RISING?
ISI THEHT WATERETAW LEVEL RISINGNISIR
Example 3
INPUT:
THIS MOBILE APP LOOKS FINE.
OUTPUT:
THIS MOBILE APP LOOKS FINE.
THISIHT MOBILELIBOM APPA LOOKSKOOL FINENIF
Example 3
INPUT:
YOU MUST BE CRAZY#
OUTPUT:
INVALID INPUT
Solution:
import java.util.*;
class PalindromeSentence {
void main() {
System.out.println("Enter a sentence:");
if (!sentence.matches("[A-Z ]*[.!?]$")) {
System.out.println("INVALID INPUT");
return;
convertedWord = makePalindrome(word);
convertedSentence.append(convertedWord).append(" ");
System.out.println(sentence + punctuation);
System.out.println(convertedSentence.toString().trim() + punctuation);
return false;
return true;
// Method to make a word palindrome by appending its reverse (excluding the last
character)
}
Q10.
Test your program with the following data and some random data:
Example 1
INPUT:
N = 726
OUTPUT:
48 * 15 = 720
6*1=6
Remaining boxes = 0
Total number of boxes = 726
Total number of cartons = 16
Example 2
INPUT:
N = 140
OUTPUT:
48 * 2 = 96
24 * 1 = 24
12 * 1 = 12
6*1=6
Remaining boxes = 2 * 1 = 2
Total number of boxes = 140
Total number of cartons = 6
Example 3
INPUT:
N = 4296
OUTPUT:
INVALID INPUT
Solution:
import java.util.*;
class CartonPacking {
void main() {
int N = sc.nextInt();
System.out.println("INVALID INPUT");
return;
// Carton sizes
int totalBoxes = N;
int totalCartons = 0;
totalCartons += cartonCounts[i];
}
// If boxes left are less than 6, we add an extra carton of 6 capacity
if (totalBoxes > 0) {
cartonCounts[3] += 1;
totalCartons += 1;
totalBoxes = 0;
if (cartonCounts[i] > 0) {
if (totalBoxes > 0) {
}
Q11.
ROT13
A/ B/ C/ D/ E/ G/ H/ K/ M/
F/f I/i J/j L/l
a b c d e g h k m
↕ ↕ ↕ ↕ ↕ ↕ ↕ ↕ ↕ ↕ ↕ ↕ ↕
N/ O/ P/ Q/ S/ U/ V/ W/ X/ Y/
R/r T/t Z/z
n o p q s u v w x y
Write a program to accept a plain text of length L, where L must be greater than 3
and less than 100.
Test your program with the sample data and some random data.
Example 1
INPUT:
Hello! How are you?
OUTPUT:
The cipher text is:
Uryyb! Ubjnerlbh?
Example 2
INPUT:
Encryption helps to secure data.
OUTPUT:
The cipher text is:
Rapelcgvbaurycfgbfrpherqngn.
Example 3
INPUT:
You
OUTPUT:
INVALID LENGTH
Solution:
import java.util.*;
class CaesarCipher{
void main() {
System.out.println("INVALID LENGTH");
return;
inp = inp.toUpperCase();
char ch = inp.charAt(i);
// If the character is a letter, apply ROT13 encryption
if (Character.isLetter(ch)) {
ch = (char) (ch + 13); // Shift within the first half of the alphabet
ch = (char) (ch - 13); // Shift within the second half of the alphabet
encrypted += ch;
System.out.println(encrypted);
}
Q12.
Write a program to take an array of integers and arrange its elements in ascending
order using Insertion sort method.
Solution:
import java.util.*;
class InsertionSortExample {
void main() {
int n = sc.nextInt();
System.out.println("INVALID INPUT");
return;
arr[i] = sc.nextInt();
// Move elements of arr[0..i-1] that are greater than key, to one position
ahead
arr[j + 1] = arr[j];
j = j - 1;
}
Q13.
Test your program for the following data and some random data:
Example 1
INPUT:
N=3
Team 1: Emus
Team 2: Road Rols
Team 3: Coyote
OUTPUT:
E R C
m o o
u a y
s d o
t
R e
o
l
s
Example 2
INPUT:
N=4
Team 1: Royal
Team 2: Mars
Team 3: De Rose
Team 4: Kings
OUTPUT:
R M D K
o a e i
y r n
a s R g
l o s
s
e
Example 3
INPUT:
N = 10
OUTPUT:
INVALID INPUT
Solution:
import java.util.*;
class TeamBanner {
void main() {
int N = sc.nextInt();
// Validate N
if (N < 3 || N > 8) {
System.out.println("INVALID INPUT");
return;
teams[i] = sc.nextLine();
int maxLength = 0;
maxLength = team.length();
}
// Print the character at the current position of the team name, or a blank
space if it is out of bounds
if (i < teams[j].length()) {
System.out.print(teams[j].charAt(i));
} else {
System.out.print(" ");
if (j < N - 1) {
System.out.print("\t");
System.out.println();
}
Q14.
Write a Program in Java to fill a square matrix of size ‘n*n” in a circular fashion
(clockwise) with natural numbers from 1 to n*n, taking ‘n’ as input.
For example: if n = 4, then n*n = 16, hence the array will be filled as given below.
Solution:
import java.util.Scanner;
int n = sc.nextInt();
// Check if n is valid
System.out.println("INVALID INPUT");
return;
matrix[top][i] = num++;
top++;
// Fill right column
matrix[i][right] = num++;
right--;
matrix[bottom][i] = num++;
bottom--;
matrix[i][left] = num++;
left++;
System.out.println("Filled Matrix:");
System.out.print(matrix[i][j] + "\t");
}
System.out.println();
}
Q15.
A =1
B =2
C =3
.
.
.
Z = 26
The potential of a word is found by adding the encrypted value of the alphabets.
Example: KITE
Potential = 11 + 9 + 20 + 5 = 45
POTENTIAL:
THE = 33
SKY = 55
IS = 28
HE = 33
LIMIT = 63
import java.util.Scanner;
System.out.println("Enter a sentence:");
System.out.println("INVALID INPUT");
return;
// Define a maximum number of words (we assume a max of 50 words for this
example)
int wordCount = 0;
if (!word.isEmpty()) {
words[wordCount] = word;
wordCount++;
} else {
word += ch;
if (!word.isEmpty()) {
words[wordCount] = word;
wordCount++;
int potential = 0;
char ch = words[i].charAt(j);
potentials[i] = potential;
// Sort the words based on their potential using simple sorting (bubble sort)
// Swap potentials
potentials[i] = potentials[j];
potentials[j] = temp;
words[i] = words[j];
words[j] = tempWord;
System.out.println("OUTPUT: ");
System.out.println();
}}
Q16.
Let’s understand the concept of Fascinating Number through the following example:
It could be observed that ‘192384576’ consists of all digits from 1 to 9 exactly once.
Hence, it could be concluded that 192 is a Fascinating Number.
Some examples of fascinating Numbers are: 192, 219, 273, 327, 1902,
1920, 2019 etc.
Solution:
import java.util.Scanner;
class FascinatingNumber {
System.out.println("Enter a number:");
} else {
// Check if the concatenated string has all digits from 1 to 9 exactly once
if (isFascinating(concatenated)) {
} else {
if (str.length() != 9) {
return false;
if (digit == 0 || digitsPresent[digit]) {
return false;
digitsPresent[digit] = true;
return true;
}
Q17.
A class Adder has been defined to add any two accepted time.
For Example:
Member function/methods :
current object
appropriate message
Specify the class, Adder giving details of the constructor( ), void readTime( ),
void addtime( )
and void display ( ). Define the main ( ) function to create an object and call the
function
import java.util.Scanner;
class Adder {
Adder() {
ar[0] = 0; // Hours
ar[1] = 0; // Minutes
void readTime() {
// Method to add time of two objects and store the sum in the current object
this.ar[1] -= 60;
void display() {
System.out.println("Total time: " + ar[0] + " hours " + ar[1] + " minutes");
time1.readTime();
time2.readTime();
sum.display();
}}
Q18.
Register is an entity which can hold a maximum of 100 names. The register
enables the user to add and remove names from the top most end only. Define a
class Register with the following details:
Member function/methods :
possible, otherwise
display the message
“OVERFLOW”
“$$”
import java.util.Scanner;
class Register {
String[] stud;
int cap;
int top;
Register(int max) {
cap = max;
top = -1;
void push(String n) {
stud[++top] = n;
} else {
System.out.println("OVERFLOW");
String pop() {
if (top >= 0) {
return stud[top--];
} else {
return "$$";
void display() {
if (top == -1) {
} else {
System.out.println(stud[i]);
while (true) {
System.out.println("\nChoose an option:");
System.out.println("4. Exit");
switch (choice) {
case 1:
reg.push(name);
break;
case 2:
if (removed.equals("$$")) {
} else {
break;
case 3:
reg.display();
break;
case 4:
System.out.println("Exiting...");
sc.close();
return;
default:
System.out.println("Invalid option.");
}}}}
Q19.
1 to 5 2.00
6 to 10 3.00
Design a class Library and another class Compute to perform the task. The
details of the two classes are given below:
to data members
number of days,
fine and total amount to
be paid. Total
amount is calculated as:
(2% of price of
book*total number of days)+fine
Write the main ( )to create an object and call the above member functions
to enable the task.
Solution:
import java.util.Scanner;
class Library {
name = bName;
author = bAuthor;
price = bPrice;
void show() {
d = dTaken;
}
void fine() {
double fine = 0;
if (d > 7) {
int excessDays = d - 7;
fine = excessDays * 2;
fine = excessDays * 3;
fine = excessDays * 5;
show();
compute.fine();
sc.close();
}
Q20.
Given a square matrix list [ ] [ ] of order ‘n’. The maximum value possible for ‘n’ is
20. Input the value for ‘n’ and the positive integers in the matrix and perform the
following tasks:
1. Print the row and column position of the largest element of the matrix
2. Print the row and column position of the second largest element of the matrix
3. Sort the elements of the rows in the ascending order and display the new
matrix
Sample Data:
INPUT:
N=3
LIST [ ] [ ]
5 1 3
7 4 6
9 8 2
OUTPUT
5 1 3
7 4 6
9 8 2
Sorted list
1 3 5
4 6 7
2 8 9
Solution:
import java.util.Scanner;
class MatrixOperations {
int n = sc.nextInt();
matrix[i][j] = sc.nextInt();
System.out.println("\nOriginal Matrix:");
System.out.print(matrix[i][j] + "\t");
System.out.println();
}
// Find the largest and second largest elements
// Traverse the matrix to find the largest and second largest elements
secondLargest = largest;
secondLargestRow = largestRow;
secondLargestCol = largestCol;
largest = matrix[i][j];
largestRow = i;
largestCol = j;
secondLargest = matrix[i][j];
secondLargestRow = i;
secondLargestCol = j;
System.out.println("\nSorted List:");
matrix[i][j] = matrix[i][k];
matrix[i][k] = temp;
System.out.print(matrix[i][j] + "\t");
System.out.println();
sc.close();