VDTD 11 2.0
VDTD 11 2.0
This algorithm outlines the steps performed by the given Java program.
Q2)
Here's the algorithm for the given Java program:
1. Begin
2. Initialize variables:
- `M` (int) for the number of rows
- `N` (int) for the number of columns
- `scanner` (Scanner) for user input
3. Use a do-while loop to ensure `M` is between 2 and 10 (exclusive):
- Prompt the user to enter the number of rows
- Read `M` from user input
4. Use a do-while loop to ensure `N` is between 2 and 10 (exclusive):
- Prompt the user to enter the number of columns
- Read `N` from user input
5. Create a 2D array `matrix` of size `[M][N]` to store matrix elements
6. Prompt the user to enter matrix elements:
- Use nested loops to iterate over rows (`i`) and columns (`j`)
- Prompt the user to enter each element at position (`i`, `j`)
- Read and store the element in the `matrix`
7. Display the input matrix using the `displayMatrix` method
8. Create a mirror image matrix `mirrorMatrix` using the
`createMirrorImage` method:
- Create a new 2D array `mirrorMatrix` of size `[M][N]`
- Use nested loops to iterate over rows (`i`) and columns (`j`)
- Assign each element of `mirrorMatrix` as the mirror image of the
corresponding element in `matrix`
9. Display the mirror image matrix using the `displayMatrix` method
10. Calculate the sum of four corners of the original matrix using the
`calculateSumOfCorners` method:
- Check if the matrix has at least 2 rows and 2 columns
- If yes, calculate the sum of top left, top right, bottom left, and
bottom right elements
11. Display the sum of four corners
12. End
This algorithm outlines the step-by-step procedure for the given Java
program, including user input, matrix operations, and output.
Q3)
Here's the algorithm for the given Java program, which counts Kaprekar
numbers within a specified range:
1. Start the program.
2. Initialize variables:
- `scanner`: Scanner object for user input.
- `i`: Loop variable.
- `n`: Placeholder for future use.
- `x`: Number of digits in the current number.
- `cp`: Copy of the current number for manipulation.
- `p`, `q`: Variables to store the range.
- `count`: Counter for Kaprekar numbers.
- `rh`: Right half of the square of the current number.
- `lh`: Left half of the square of the current number.
- `sq`: Square of the current number.
3. Prompt the user to input the range (`p` and `q`).
4. Check if the range is valid (`p` less than or equal to `q`). If not, display an
error message and terminate the program.
5. Iterate through the range from `p` to `q`:
a. Assign the current value of the iteration (`i`) to `cp`.
b. Count the number of digits in `cp` using a loop and update the value of
`x`.
c. Calculate the square of `i` and store it in `sq`.
d. Calculate the right half (`rh`) and left half (`lh`) of the square using
modulo and division operations respectively.
e. Check if the sum of `rh` and `lh` equals `i`. If true, increment `count` and
display the current number (`i`) as a Kaprekar number.
f. Reset the value of `x` to 0 for the next iteration.
6. Output the frequency of Kaprekar numbers (`count`).
7. End the program.
10. Prompt the user to input a character to be replaced in the second string
10.1 Read the character to be replaced and store it in oldChar
10.2 Prompt the user to input a character to replace with
10.3 Read the character to replace with and store it in newChar
10.4 Replace oldChar with newChar in secondString and store the result in
replacedString