[go: up one dir, main page]

0% found this document useful (0 votes)
23 views16 pages

VDTD 11 2.0

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views16 pages

VDTD 11 2.0

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 16

Q1)

Here's the algorithm for the given Java program:


1. Start the program.
2. Import the necessary Java utilities.
3. Define a class named `degree90`.
4. Inside the class:
- Define the `main` method.
- Create a `Scanner` object for input.
- Declare variables `m`, `n`, `r`, and `c` of type integer.
- Read integers `m` and `n` from the user.
- Create a 2D integer array `ar` of size `m` rows and `n` columns.
- Iterate over rows `r` from 0 to `m-1`:
- Iterate over columns `c` from 0 to `n-1`:
- Read integers from the user and store them in the array `ar` at position
`ar[r][c]`.
- Print the matrix `ar`.
- Create a new 2D integer array `dg` of size `n` rows and `m` columns.
- Iterate over rows `r` from 0 to `n-1`:
- Initialize a variable `x` to `n`.
- Iterate over columns `c` from 0 to `m-1`:
- Assign the value of `ar[x][r]` to `dg[r][c]`.
- Decrement `x` by 1.
- Print the transposed matrix `dg`.
5. End the main method.
6. End the class definition.
7. End the program.

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.

This algorithm describes the process of identifying Kaprekar numbers within


the specified range and counting their frequency.
Q4)
Here's the algorithm for the given Java program to check if a number is a
Krishna Murthy number:
1. Start
2. Initialize a Scanner object to take user input.
3. Prompt the user to enter a number.
4. Read the input number.
5. Close the Scanner object.
6. Initialize variables `sum` and `temp` to 0 and the input number,
respectively.
7. While `temp` is greater than 0, do steps 8-12.
8. Extract the last digit of `temp` and store it in a variable `digit`.
9. Initialize a variable `fact` to 1.
10. Calculate the factorial of `digit` and store it in `fact`.
11. Add `fact` to `sum`.
12. Remove the last digit from `temp`.
13. Check if the input number is equal to `sum`.
14. If they are equal, print that the number is a Krishna Murthy number.
15. If they are not equal, print that the number is not a Krishna Murthy
number.
16. End.
Q5)
1. Start
2. Initialize Scanner object sc to read input from the console
3. Declare String variables s, st="", d=""
4. Declare integer variables l, i, j, y=0, x=0, po=0, p=0
5. Declare char variable c
6. Print "Enter the sentence" to prompt the user
7. Read the sentence input and store it in s
8. Convert the input sentence to uppercase
9. Get the length of the input sentence and store it in l
10. Iterate through the characters of the input sentence using a loop (i from 0
to l-1)
11. Get the character at position i and store it in c
12. If c is not a space, question mark, or exclamation mark
13. Increment x by 1
14. Print "No of words = " followed by the value of x
15. Declare arrays wd[] and pt[] of size x to store words and their
corresponding total ASCII values
16. Initialize p to 0
17. Iterate through the characters of the input sentence using a loop (i from 0
to l-1)
18. Get the character at position i and store it in c
19. If c is not a space, question mark, or exclamation mark
20. Increment p by the ASCII value of c
21. Append c to string st
22. Else
23. Print st followed by "=" and p
24. Store st and p in wd[y] and pt[y] respectively
25. Reset st to an empty string and p to 0
26. Increment y by 1
27. Sort the words in descending order based on their total ASCII values using
nested loops
28. Outer loop (i from 0 to x-2)
29. Inner loop (j from i+1 to x-1)
30. If pt[i] is less than pt[j]
31. Swap pt[i] and pt[j]
32. Swap wd[i] and wd[j]
33. Print the sorted words in the array wd[]
34. End
Q6)
Algorithm: StringManipulation
1. Start

2. Initialize scanner to read input from the console


3. Prompt the user to input two strings
3.1 Read the first string and store it in firstString
3.2 Read the second string and store it in secondString
4. Concatenate the two strings and store the result in concatenatedString

4.1 concatenatedString = firstString + secondString


5. Display the concatenated string
5.1 Print "Concatenated String: " + concatenatedString
6. Display the length of each string
6.1 Print "Length of the first string: " + length of firstString

6.2 Print "Length of the second string: " + length of secondString


7. Check if the two strings are equal
7.1 If firstString equals secondString
7.1.1 Print "The two strings are equal."
7.2 Else
7.2.1 Print "The two strings are not equal."
8. Prompt the user to input starting and ending indices for substring in the first string

8.1 Read the starting index and store it in startIndex


8.2 Read the ending index and store it in endIndex
8.3 Extract the substring from firstString using startIndex and endIndex and store it in
substring
8.3.1 substring = firstString.substring(startIndex, endIndex)
9. Display the substring of the first string
9.1 Print "Substring of the first string: " + substring

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

10.4.1 replacedString = secondString.replace(oldChar, newChar)


11. Display the string after replacement
11.1 Print "String after replacement: " + replacedString
12. Prompt the user to input a prefix to check in the first string
12.1 Read the prefix and store it in prefix

12.2 Check if firstString starts with prefix


12.2.1 If true, print "The first string starts with the specified prefix."
12.2.2 Else, print "The first string does not start with the specified prefix."
13. Convert the first string to a character array and display it
13.1 Convert firstString to a character array and store it in charArray

13.1.1 charArray = firstString.toCharArray()


13.2 Print "Characters of the first string: " followed by each character in charArray
14. Close the scanner to prevent resource leak
15. End
Q7)
Here's a proper algorithm for the given Java program:
Algorithm for Date Manipulation Program
1. **Input:**
- Prompt the user to enter the first date (day, month, year).
- Read the values for day1, month1, and year1.
- Prompt the user to enter the second date (day, month, year).
- Read the values for day2, month2, and year2.
2. **Find Day of the Week:**
- Define a method `findDayOfWeek(day, month, year)` to determine
the day of the week for a given date using Zeller's Congruence algorithm.
- Calculate the day of the week for the first date (day1, month1,
year1) using the `findDayOfWeek` method.
- Display the day of the week for the first date.
3. **Find Date Difference:**
- Define a method `findDateDifference(day1, month1, year1, day2,
month2, year2)` to calculate the difference between two dates in days.
- Calculate the difference between the two dates (day1, month1,
year1) and (day2, month2, year2) using the `findDateDifference` method.
- Display the difference between the two dates.
4. **Display Calendar:**
- Define a method `displayCalendar(month, year)` to display the
calendar for a specific month and year.
- Adjust the number of days in February for leap years.
- Determine the weekday for the first day of the month using Zeller's
Congruence algorithm.
- Print the calendar with the days of the month.
5. **Main Function:**
- Call the `findDayOfWeek`, `findDateDifference`, and
`displayCalendar` methods with appropriate arguments.
- Close the Scanner object to prevent resource leak.
6. **Output:**
- Display the day of the week for the first date.
- Display the difference between the two dates.
- Display the calendar for the month and year of the second date.
End Algorithm
Q8)
1. Start
2. Create a Scanner object to read input from the console.
3. Prompt the user to enter the day, month, and year.
4. Read the input values for day, month, and year from the user.
5. Display the entered date in the format day/month/year.
6. Check if the year is a leap year:
a. If (year % 400 == 0) or ((year % 4 == 0) and (year % 100 != 0)), set isLeapYear to
true.
b. Otherwise, set isLeapYear to false.
7. If isLeapYear is true, display that the entered year is a leap year; otherwise,
display that it is not a leap year.
8. Check if the entered date is valid:
a. If the month is less than 1 or greater than 12, set isValidDate to false.
b. If the day is less than 1 or greater than 31, set isValidDate to false.
c. If the month is April, June, September, or November and the day is greater
than 30, set isValidDate to false.
d. If the month is February:
i. If it's a leap year and the day is greater than 29, set isValidDate to false.
ii. If it's not a leap year and the day is greater than 28, set isValidDate to false.
9. If isValidDate is true, display that the entered date is valid; otherwise, display
that it is invalid.
10. Close the Scanner object.
11. End.

You might also like