Algorithm
STEP 1 ===.
• START
STEP 2 ===.
• Define the class Square_SubMatrix.
STEP 3 ===.
• Declare static integer variable N for matrix size and a static 2D integer array matrix.
STEP 4 ===.
• Define the method createMatrix() to generate a matrix: Create a 2D integer array matrix of size n x n. Use nested loops
to fill the matrix with consecutive integers starting from 1.
STEP 5 ===.
• Define the method displayMatrix(): Use nested loops to print the elements of a m x m matrix in tabular format.
STEP 6 ===.
• Define the method extractSubMatrix(): Initialize variables startRow and startCol to −1 to store the position of value
v in the matrix. Use nested loops to locate the position of v in the matrix: If found, set startRow and startCol to the
respective indices. If v is not found, return null.
STEP 7 ===.
• If startRow and startCol are valid, extract a sub-matrix of size m x m: Create a new 2D array subMatrix. Use nested
loops to copy elements from the original matrix to the subMatrix, ensuring bounds are not exceeded.
STEP 8 ===.
• Return the extracted sub-matrix.
STEP 9 ===.
• Define the main() method.
STEP 10 ===.
• Prompt the user to input the size of the matrix (N). Validate the input: If N is not between 1 and 11, print an error
message and terminate.
STEP 11 ===.
• Call createMatrix(N) to generate the matrix and store it in the variable matrix.
STEP 12 ===.
• Display the generated matrix using displayMatrix().
STEP 13 ===.
• Prompt the user to input the starting value (V) and the size of the sub-matrix (M).
STEP 14 ===.
• Call extractSubMatrix(matrix, V, M) to get the sub-matrix: If the sub-matrix is null, print an error message indicating
that the value V was not found.
STEP 15 ===.
• If a valid sub-matrix is returned, display it using displayMatrix().
STEP 16 ===.
STOP
1
Enter the size of the matrix (N): 5
Generated Matrix:
1 2 3 4 5
6 7 8 9 10
11 12 13 14 15
16 17 18 19 20
21 22 23 24 25
Enter the starting value (V): 18
Enter the size of the sub-matrix (M): 2
Extracted Sub-Matrix:
18 19
23 24
Enter the size of the matrix (N): 14
Invalid size. N should be between 1 and 11.
Enter the size of the matrix (N): 5
Generated Matrix:
1 2 3 4 5
6 7 8 9 10
11 12 13 14 15
16 17 18 19 20
21 22 23 24 25
Enter the starting value (V): 8
Enter the size of the sub-matrix (M): 4
Extracted Sub-Matrix:
8 9 10 6
13 14 15 11
18 19 20 16
23 24 25 21
Algorithm
STEP 1 ===.
• START
STEP 2 ===.
• Define the class Square_SubMatrix.
STEP 3 ===.
• Declare static integer variable N for matrix size, and a static 2D integer array matrix.
STEP 4 ===.
• Define the main() method to execute the program: Create a Scanner object sc to read user input.
STEP 5 ===.
• Prompt the user to input the size of the matrix (N). Read the value into variable N. If N is less than 1 or greater than 11,
print an error message and stop the program.
STEP 6 ===.
• Call the createMatrix() method to generate a matrix of size N x N. Store the resulting matrix in matrix.
STEP 7 ===.
• Display the generated matrix using the displayMatrix() method.
STEP 8 ===.
• Prompt the user to input a starting value V to locate in the matrix. Read the value into variable V.
STEP 9 ===.
• Prompt the user to input the size of the sub-matrix M. Read the value into variable M.
STEP 10 ===.
• Call the extractSubMatrix() method to extract a sub-matrix of size M x M starting from the position of value V in
the matrix.
STEP 11 ===.
• If extractSubMatrix() returns null, print an error message indicating that the starting value V was not found.
STEP 12 ===.
• If a valid sub-matrix is returned, display it using the displayMatrix() method.
STEP 13 ===.
• Define the createMatrix() method: Initialize a 2D array matrix of size n x n. Fill the matrix with consecutive integers
starting from 1.
STEP 14 ===.
• Define the extractSubMatrix() method: Find the position of value V in the matrix. If V is not found, return null.
STEP 15 ===.
• Create and return a sub-matrix of size M x M starting from the position of V.
STEP 16 ===.
• Define the displayMatrix() method to display the matrix in a readable format.
STEP 17 ===.
• STOP
1
Enter the size of the matrix (N): 3
Enter the elements of the matrix:
0.5 0.25 0.25 0.25 0.75 0.0 0.25 0.0 0.75
Formed Matrix:
0.5 0.25 0.25
0.25 0.75 0.0
0.25 0.0 0.75
The Matrix is a Doubly Markov Matrix
Enter the size of the matrix (N): 3
Enter the elements of the matrix:
1.5 3 0.15 0.25 4 1.0 0.25 1.0 3
Formed Matrix:
1.5 3.0 0.15
0.25 4.0 1.0
0.25 1.0 3.0
The Matrix is not a Doubly Markov Matrix
Enter the size of the matrix (N): 3
Enter the elements of the matrix:
0.5 0.25 0.25 0.25 0.75 0.0 0.25 0.0 -1.75
Negative number entered. Invalid entry.
₹
Enter employee name: Billu Bhai
Enter employee code: 698969
Enter base salary: 2000000
Enter overtime hours: 60
Employee Name: Billu Bhai
Employee Code: 698969
Base Salary: ₹2000000.0
Overtime Hours: 60
Total Salary: ₹2005000.0
Enter employee name: Billa Ran
Enter employee code: 1001001
Enter base salary: 1000000
Enter overtime hours: 40
Employee Name: Billa Ran
Employee Code: 1001001
Base Salary: ₹1000000.0
Overtime Hours: 40
Total Salary: ₹1003000.0
Enter employee name: Doggo Bhai
Enter employee code: 1119988
Enter base salary: 1500000
Enter overtime hours: 30
Employee Name: Doggo Bhai
Employee Code: 1119988
Base Salary: ₹1500000.0
Overtime Hours: 30
Total Salary: ₹1503000.0
Algorithm
STEP 1 ===.
• START
STEP 2 ===.
• Define the class Matrix_Boundary.
STEP 3 ===.
• Declare a static integer array bound to store boundary elements.
STEP 4 ===.
• Define the method createMatrix(int m): Initialize a 2D integer array arr of size m x m. Initialize an integer variable t to
index the bound array. Accept elements for the matrix from the user using nested loops. Identify and store boundary
elements in the bound array. Return the created matrix.
STEP 5 ===.
• Define the method displayMatrix(int[][] arr, int m): Use nested loops to iterate through the matrix and print its elements
in a readable format.
STEP 6 ===.
• Define the method arrange():
Sort the bound array in ascending order using Bubble Sort.
STEP 7 ===.
• Define the method non_boundary(int[][] arr, int n): Initialize a variable p to 1 for storing the product of non-boundary
elements. Use nested loops to extract non-boundary elements from the matrix. Display the non-boundary elements and
calculate their product.
STEP 8 ===.
• Define the main() method:
STEP 9 ===.
• Create a Scanner object to read user input.
STEP 10 ===.
• Prompt the user to enter the size of the matrix (N). If N < 3 or N > 10, print an error message and stop the program.
STEP 11 ===.
• Call the createMatrix(N) method to create the matrix and extract boundary elements. Store the resulting matrix in
matrix.
STEP 12 ===.
• Display the original matrix using the displayMatrix(matrix, N) method.
STEP 13 ===.
• Call the arrange() method to sort the boundary elements in ascending order.
STEP 14 ===.
• Create a new 2D array arr2 to store the rearranged matrix. Rearrange the sorted boundary elements in a clockwise
manner, leaving non-boundary elements unchanged.
STEP 15 ===.
• Display the rearranged matrix using the displayMatrix(arr2, N) method.
STEP 16 ===.
• Call the non_boundary(matrix, N) method to:Display the non-boundary elements. Calculate and display their product.
STEP 17 ===.
• STOP
1
Enter the size of the matrix (N): 4
Enter the array elements:
11 2 5 7 8 10 9 4 15 5 3 11 1 17 14 8
Original Matrix:
11 2 5 7
8 10 9 4
15 5 3 11
1 17 14 8
Rearranged Matrix:
1 2 4 5
17 10 9 7
15 5 3 8
14 11 11 8
Non Boundary Elements:
10 9
5 3
Product of the Non-Boundary Elements: 1350
Enter the size of the matrix (N): 2
Invalid size. N should be between 3 and 10.
Enter the size of the matrix (N): 13
Invalid size. N should be between 3 and 10.
Algorithm
STEP 1 ===.
• START
STEP 2 ===.
• Define the class Vampire_Num.
STEP 3 ===.
• Define the method isVampire(int num):
‣ Convert num to a string numStr.
STEP 4 ===.
• If the length of numStr is odd, return false (Vampire numbers must have an even number of digits).
STEP 5 ===.
• Calculate half as half the length of numStr.
STEP 6 ===.
• Loop through factors i from 10^(half-1) to 10^half.
‣ For each i, check if num % i == 0.
STEP 7 ===.
• If i is a factor, calculate j = num / i.
STEP 8 ===.
• Ensure i and j have the same number of digits as half.
STEP 9 ===.
• Check if i and j form a valid pair using isValidPair(i, j, numStr).
STEP 10 ===.
• If valid, return true.
STEP 11 ===.
• Define the method isValidPair(int x, int y, String numStr):
‣ Check if x or y ends with ‘0’. If both do, return false.
STEP 12 ===.
• Concatenate x and y into combined.
STEP 13 ===.
• Check if digits of combined match digits of numStr using areDigitsEqual(numStr, combined).
STEP 14 ===.
• Define the method areDigitsEqual(String numStr, String combined):
‣ Sort both strings and compare their characters.
STEP 15 ===.
• Define the main() method to execute the program:
‣ Prompt the user to input m and n and validate the range [1000, 9999].
STEP 16 ===.
• Loop through numbers from m to n, check if each is a Vampire number, and print results.
STEP 17 ===.
• STOP
1
Enter the lower limit (m): 1050
Enter the upper limit (n): 1640
THE VAMPIRE NUMBERS ARE:
1260 1395 1435 1530
FREQUENCY OF VAMPIRE NUMBERS IS: 4
Enter the lower limit (m): 1890
Enter the upper limit (n): 7000
THE VAMPIRE NUMBERS ARE:
2187 6880
FREQUENCY OF VAMPIRE NUMBERS IS: 2
Enter the lower limit (m): 999
Enter the upper limit (n): 10000
Invalid input! m and n must be in the range [1000, 9999] and m < n.
Algorithm
STEP 1 ===.
• START
STEP 2 ===.
• Define the class Date.
STEP 3 ===.
• Define the method isLeapYear(int year) to check if a year is a leap year:
‣ Return false if the year is not divisible by 4.
‣ Return false if the year is divisible by 100 but not by 400.
‣ Otherwise, return true.
STEP 4 ===.
• Define the method getFormattedDate(int dayNumber, int year) to convert a day number to a formatted date:
‣ Define an array daysInMonth with the number of days in each month, adjusting for leap years.
‣ Define an array monthNames with month names.
‣ Initialize the month variable to 0.
STEP 5 ===.
• While the dayNumber is greater than the days in the current month, subtract the days in the current month from
dayNumber and increment month.
‣ If month exceeds 11 (December), reset it to 0 and increment the year. Adjust the days in February based on whether
the new year is a leap year.
STEP 6 ===.
• Return the formatted date as “Month day, year”.
STEP 7 ===.
• Define the main() method to execute the program:
‣ Create a Scanner object sc to read user input.
STEP 8 ===.
• Prompt the user to input the day number (1-366), year (YYYY), and number of days to add (N).
STEP 9 ===.
• Validate the day number:
‣ If the day number is less than 1 or greater than the number of days in the year (based on leap year), print an error
message and stop the program.
STEP 10 ===.
• Validate the value of N:
‣ If N is less than 1 or greater than 100, print an error message and stop the program.
STEP 11 ===.
• Call getFormattedDate() with the entered dayNumber and year to calculate the entered date.
STEP 12 ===.
• Call getFormattedDate() with the day number incremented by N to calculate the future date.
STEP 13 ===.
• Print the entered date and the future date.
STEP 14 ===.
• Close the Scanner object.
STEP 15 ===.
• STOP
1
Enter the day number (1-366): 500
Enter the year (YYYY): 2023
Enter the number of days to add (N): 199
Invalid day number for the given year.
Enter the day number (1-366): 300
Enter the year (YYYY): 2020
Enter the number of days to add (N): 89
OUTPUT: ENTERED DATE: OCTOBER 26, 2020
89 DAYS LATER: JANUARY 23, 2021
Enter the day number (1-366): 269
Enter the year (YYYY): 2069
Enter the number of days to add (N): 69
OUTPUT: ENTERED DATE: SEPTEMBER 26, 2069
69 DAYS LATER: DECEMBER 4, 2069
Enter the day number (1-366): 2000
Enter the year (YYYY): 199
Enter the number of days to add (N): 2019
Invalid day number for the given year.
Enter the day number (1-366): 169
Enter the year (YYYY): 2000
Enter the number of days to add (N): 169
Invalid value for N. Must be between 1 and 100.
Algorithm
STEP 1 ===.
• START
STEP 2 ===.
• Define the class Snowball_String.
STEP 3 ===.
• Define the method isValidSentence(String sentence) to validate the sentence:
‣ Trim any leading or trailing spaces from the sentence.
‣ If the sentence is empty, return false.
‣ Check if the last character is a period (.) or a question mark (?). Return true if valid, else return false.
STEP 4 ===.
• Define the method checkSnowballString(String sentence) to check if the sentence is a Snowball String:
‣ Remove the terminating character (period or question mark) from the sentence and trim any extra spaces.
‣ Tokenize the sentence using StringTokenizer to separate words.
STEP 5 ===.
• Initialize previousLength to 0 to track the length of the previous word.
STEP 6 ===.
• Iterate over each word in the tokenized sentence:
‣ For each word, get its length currentLength.
‣ If the previousLength is not 0 and currentLength is not equal to previousLength + 1, return false.
STEP 7 ===.
• If the loop completes without returning false, return true (indicating the sentence is a Snowball String).
STEP 8 ===.
• Define the main() method to execute the program:
‣ Create a Scanner object sc to read user input.
STEP 9 ===.
• Prompt the user to input a sentence that ends with either . or ?.
STEP 10 ===.
• Call the isValidSentence() method to check if the input sentence is valid.
STEP 11 ===.
• If the sentence is invalid, print “Invalid sentence. It must end with ‘.’ or ‘?’ only.” and stop the program.
STEP 12 ===.
• Call the checkSnowballString() method to determine if the sentence is a Snowball String.
STEP 13 ===.
• If the sentence is a Snowball String, print “IT IS A SNOWBALL STRING”.
STEP 14 ===.
• If the sentence is not a Snowball String, print “IT IS NOT A SNOWBALL STRING”.
STEP 15 ===.
• Close the Scanner object.
STEP 16 ===.
• STOP
1
Enter a sentence (ending with '.' or '?'): I am the Lord
Invalid sentence. It must end with '.' or '?' only.
Enter a sentence (ending with '.' or '?'): I am the Lord.
IT IS A SNOWBALL STRING
Enter a sentence (ending with '.' or '?'): He may give donation.
IT IS NOT A SNOWBALL STRING
Algorithm
STEP 1 ===.
• START
STEP 2 ===.
• Define the class Matrix_Pattern.
STEP 3 ===.
• Define the method generateMatrix(int N, char char1, char char2, char char3) to generate the matrix:
‣ Initialize a 2D char array matrix of size N x N.
STEP 4 ===.
• Loop through each row index i from 0 to N-1.
STEP 5 ===.
• Inside the row loop, loop through each column index j from 0 to N-1.
STEP 6 ===.
• For each element at position (i, j), check the following conditions:
‣ If the element is in any of the four corners, assign it char1.
‣ If the element is on the boundary (first or last row/column) but not a corner, assign it char2.
‣ If the element is not on the boundary, assign it char3.
STEP 7 ===.
• After populating the matrix, return the matrix.
STEP 8 ===.
• Define the method displayMatrix(char[][] matrix) to display the matrix:
‣ Loop through each row of the matrix.
‣ For each row, loop through each element and print it followed by a space.
‣ After each row, print a newline.
STEP 9 ===.
• Define the main() method to execute the program:
‣ Create a Scanner object sc to read user input.
STEP 10 ===.
• Prompt the user to input the size of the matrix N.
STEP 11 ===.
• Validate the size of the matrix:
‣ If N is less than or equal to 3 or greater than or equal to 10, print an error message and stop the program.
STEP 12 ===.
• Prompt the user to input the first character char1.
STEP 13 ===.
• Prompt the user to input the second character char2.
STEP 14 ===.
• Prompt the user to input the third character char3.
STEP 15 ===.
• Call the generateMatrix() method to generate the matrix using the user inputs N, char1, char2, and char3.
STEP 16 ===.
• Print the message “Formed Matrix:”.
STEP 17 ===.
• Call the displayMatrix() method to display the matrix.
STEP 18 ===.
• STOP
1
Enter the size of the matrix (N): 4
Enter the first character: @
Enter the second character: #
Enter the third character: $
Formed Matrix:
@##@
#$$#
#$$#
@##@
Enter the size of the matrix (N): 5
Enter the first character: #
Enter the second character: %
Enter the third character: &
Formed Matrix:
#%%%#
%&&&%
%&&&%
%&&&%
#%%%#
Enter the size of the matrix (N): 6
Enter the first character: @
Enter the second character: *
Enter the third character: #
Formed Matrix:
@****@
*####*
*####*
*####*
*####*
@****@
Algorithm
STEP 1 ===.
• START
STEP 2 ===.
• Define the class Word_Insertion.
STEP 3 ===.
• Define the method isValid(String s) to validate the sentence:
‣ Trim leading and trailing spaces from the sentence. Check if the sentence is empty. If it is, return false.
‣ Check if the last character of the sentence is either ‘.’, ‘?’, or ‘!’. Return true if valid, else return false.
STEP 4 ===.
• Define the method removeSpaces(String s) to remove extra spaces:
‣ Trim leading and trailing spaces. Replace any sequence of multiple spaces within the sentence with a single space.
STEP 5 ===.
• Define the method insertWord(String s, String w, int pos) to insert a word at a specific position:
‣ Exclude punctuation from the sentence by using substring(0, s.length() - 1) and extract the last punctuation
character. Tokenize the sentence using StringTokenizer (excluding punctuation). Count the number of tokens in
the sentence.
STEP 6 ===.
• Validate the position pos:
‣ If pos is less than 1 or greater than the number of tokens + 1, return the original sentence as no insertion is possible.
STEP 7 ===.
• Initialize an empty string result to build the modified sentence.
STEP 8 ===.
• Iterate over the tokens in the sentence:
‣ For each token, check if the current index matches the desired position pos.
‣ If it matches, insert the word w at that position in the sentence. Append the current token to the result.
STEP 9 ===.
• After iterating through the tokens, if the position is at the end of the sentence, append the word w at the end of
the sentence.
STEP 10 ===.
• Return the modified sentence by trimming extra spaces and adding the punctuation back.
STEP 11 ===.
• Define the main() method to execute the program: Create a Scanner object sc to read user input.
STEP 12 ===.
• Prompt the user to input a sentence that ends with ‘.’, ‘?’, or ‘!’.
STEP 13 ===.
• Call the isValid() method to check if the sentence is valid.
STEP 14 ===.
• If the sentence is invalid, print “Invalid sentence. It must end with ‘.’, ‘?’, or ‘!’ only.” and stop the program.
STEP 15 ===.
• Call the removeSpaces() method to remove extra spaces from the sentence.
STEP 16 ===.
• Prompt the user to input the word to insert and the position to insert it.
STEP 17 ===.
• Call the insertWord() method to insert the word at the specified position and display the modified sentence.
STEP 18 ===.
• STOP
1
Enter a sentence (ending with '.', '?', or '!'): MORNING WALK IS A BLESSING FOR THE
WHOLE DAY.
Enter the word to be inserted: TOTAL
Enter the position to insert the word: 5
Modified Sentence: MORNING WALK IS A TOTAL BLESSING FOR THE WHOLE DAY.
Enter a sentence (ending with '.', '?', or '!'): He may give bonus.
Enter the word to be inserted: not
Enter the position to insert the word: 3
Modified Sentence: He may not give bonus.
Enter a sentence (ending with '.', '?', or '!'): He may give bonus
Invalid sentence. It must end with '.', '?', or '!' only.
Algorithm
STEP 1 ===.
• START
STEP 2 ===.
• Define the class Password_Strength.
STEP 3 ===.
• Define the method requiredChars(String pw) to check and calculate the required characters:
‣ Initialize boolean flags (hasLower, hasUpper, hasDigit, hasSpecial) to track the presence of lowercase letters,
uppercase letters, digits, and special characters.
STEP 4 ===.
• Define a string special that contains the allowed special characters: “!@$%^&*()-+”.
STEP 5 ===.
• Loop through each character in the password pw:
‣ Check if the character is a lowercase letter, an uppercase letter, a digit, or a special character. Update the corresponding
boolean flag if a match is found.
STEP 6 ===.
• Initialize an integer missing to count the missing criteria for password strength.
STEP 7 ===.
• If the password length n is less than 6, increment missing and print a message: “The password needs a minimum of
6 characters.”
STEP 8 ===.
• If the password does not contain a lowercase letter (hasLower is false), increment missing and print: “The password
needs a Lower Case character.”
STEP 9 ===.
• If the password does not contain an uppercase letter (hasUpper is false), increment missing and print: “The password
needs an Upper Case character.”
STEP 10 ===.
• If the password does not contain a digit (hasDigit is false), increment missing and print: “The password needs a
digit character.”
STEP 11 ===.
• If the password does not contain a special character (hasSpecial is false), increment missing and print: “The password
needs a special character.”
STEP 12 ===.
• Ensure the minimum password length is 6 by returning the maximum of missing and 6 - n (where n is the password
length). This ensures that the password is at least 6 characters long.
STEP 13 ===.
• Define the main() method to execute the program: Create a Scanner object sc to read user input.
STEP 14 ===.
• Prompt the user to input a password and store the input in pw.
STEP 15 ===.
• Call the requiredChars(pw) method to check the password strength and determine how many characters need to be
added to meet the requirements.
STEP 16 ===.
• Print the result showing how many characters need to be added to make the password strong.
STEP 17 ===.
• STOP
1
Enter the password: 2bbaAa
The password is 6 character(s) long.
The password needs a special character.
The minimum characters to add to make the password strong: 1
Enter the password: 2bbbb
The password is 5 character(s) long.
The password needs minimum of 6 characters.
The password needs a Upper Case character. The password needs a special character.
The minimum characters to add to make the password strong: 3
Enter the password: 2bb@b
The password is 5 character(s) long.
The password needs minimum of 6 characters.
The password needs a Upper Case character.
The minimum characters to add to make the password strong: 2
Algorithm
STEP 1 ===.
• START
STEP 2 ===.
• Define the class Caesar_Cipher.
STEP 3 ===.
• Define the method encrypt(String text, int k) to encrypt the input text using the Caesar Cipher algorithm.
STEP 4 ===.
• Normalize the shift value k by taking k % 26 to avoid unnecessary cycles (since there are 26 letters in the alphabet).
STEP 5 ===.
• Create a char[] result array of the same length as text to store the encrypted characters.
STEP 6 ===.
• Loop through each character c in the input text:
STEP 7 ===.
• If c is a lowercase letter:
‣ Shift it by k positions within the lowercase alphabet and store it in the result array.
STEP 8 ===.
• If c is an uppercase letter:
‣ Shift it by k positions within the uppercase alphabet and store it in the result array.
STEP 9 ===.
• If c is not an alphabetic character (e.g., a space, punctuation):
‣ Leave the character unchanged in the result array.
STEP 10 ===.
• After looping through all characters, return the new encrypted text by converting the result array to a string.
STEP 11 ===.
• Define the main() method to execute the program:
‣ Create a Scanner object sc to read user input.
STEP 12 ===.
• Prompt the user to input the shift value k and store it in k.
STEP 13 ===.
• Consume the newline character after reading the integer value for k using sc.nextLine().
STEP 14 ===.
• Prompt the user to input the text to be encrypted and store it in text.
STEP 15 ===.
• Call the encrypt(text, k) method to encrypt the input text using the provided shift value.
STEP 16 ===.
• Print the encrypted text as the result.
STEP 17 ===.
• Close the Scanner object.
STEP 18 ===.
• STOP
1
Enter the shift value: 2
Enter the text: middle-Outz
Encrypted Text: okffng-Qwvb
Enter the shift value: 3
Enter the text: my-name-is-julius
Encrypted Text: pb-qdph-lv-mxolxv
Enter the shift value: 4
Enter the text: Internal Civil War
Encrypted Text: Mrxivrep Gmzmp Aev
Algorithm
STEP 1 ===.
• START
STEP 2 ===.
• Define the class Diagonal_Sum.
STEP 3 ===.
• Define the displayMatrix(int[][] arr, int m) method to display the matrix in a formatted manner:
‣ Iterate through each row and column of the matrix and print the elements.
STEP 4 ===.
• Define the main() method to execute the program:
‣ Create a Scanner object sc to read user input.
STEP 5 ===.
• Prompt the user to input the size of the square matrix n and store it in n.
STEP 6 ===.
• Create a 2D integer array matrix of size n x n.
STEP 7 ===.
• Prompt the user to input the elements of the matrix.
STEP 8 ===.
• Loop through the rows and columns of the matrix:
‣ For each element, read and store the integer value from user input into the matrix array.
STEP 9 ===.
• Print the message “The matrix:” to indicate the display of the matrix.
STEP 10 ===.
• Call the displayMatrix(matrix, n) method to display the entered matrix.
STEP 11 ===.
• Initialize two variables, primarySum and secondarySum, to store the sums of the primary and secondary diagonals
respectively.
STEP 12 ===.
• Loop through the matrix from i = 0 to n-1:
‣ Add the element at the primary diagonal (where i == j) to primarySum.
‣ Add the element at the secondary diagonal (where i == n - i - 1) to secondarySum.
STEP 13 ===.
• After the loop, print the sum of the primary diagonal with the message “The sum of right diagonal = “ and the value
of primarySum.
STEP 14 ===.
• Print the sum of the secondary diagonal with the message “The sum of left diagonal = “ and the value of secondarySum.
STEP 15 ===.
• Calculate the absolute difference between the two diagonal sums using Math.abs(primarySum - secondarySum) and
store it in difference.
STEP 16 ===.
• Print the absolute difference between the diagonals with the message “Absolute difference between the diagonals: “ and
the value of difference.
STEP 17 ===.
• Close the Scanner object sc.
STEP 18 ===.
• STOP
1
Enter the size of the square matrix (N): 3
Enter the elements of the matrix:
123456989
The matrix:
1 2 3
4 5 6
9 8 9
The sum of right diagonal = 15
The sum of left diagonal = 17
Absolute difference between the diagonals: 2
Enter the size of the square matrix (N): 3
Enter the elements of the matrix:
234568245
The matrix:
2 3 4
5 6 8
2 4 5
The sum of right diagonal = 13
The sum of left diagonal = 12
Absolute difference between the diagonals: 1
Enter the size of the square matrix (N): 3
Enter the elements of the matrix:
245913257
The matrix:
2 4 5
9 1 3
2 5 7
The sum of right diagonal = 10
The sum of left diagonal = 8
Absolute difference between the diagonals: 2
Algorithm
STEP 1 ===.
• START
STEP 2 ===.
• Define the class MinMax.
STEP 3 ===.
• Define the arrange(int[] arr, int n) method to sort the array in ascending order.
Step 4 ===.
• Loop through the array arr to perform the sorting.
Step 5 ===.
• Use the bubble sort algorithm to compare adjacent elements and swap them if they are in the wrong order.
STEP 6 ===.
• Define the main() method to execute the program:
‣ Create a Scanner object sc to read user input.
STEP 7 ===.
• Print the message “Enter five positive integers:” to prompt the user to input five integers.
STEP 8 ===.
• Declare an integer array nums of size 5 to store the input integers.
STEP 9 ===.
• Use a for loop to input five integers from the user and store them in the nums array.
STEP 10 ===.
• Call the arrange(nums, 5) method to sort the nums array in ascending order.
STEP 11 ===.
• Initialize two integer variables minSum and maxSum to store the sum of the smallest 4 numbers and the largest 4 numbers,
respectively.
STEP 12 ===.
• Use a for loop to calculate the sum of the smallest 4 numbers:
‣ Add the first 4 elements of the sorted array to minSum.
STEP 13 ===.
• Use another for loop to calculate the sum of the largest 4 numbers:
‣ Add the last 4 elements of the sorted array to maxSum.
STEP 14 ===.
• After the loop, print the result with the message “Minimum and Maximum: “ followed by the values of minSum and
maxSum.
STEP 15 ===.
• Close the Scanner object sc to free the resources.
STEP 16 ===.
• STOP
1
Enter five positive integers:
12345
Minimum and Maximum: 10 14
Enter five positive integers:
34 87 12 45 79
Minimum and Maximum: 170 245
Enter five positive integers:
12 67 31 11 18
Minimum and Maximum: 72 128