Solutions
Solutions
Algorithm:
1. Accept Input:
Prompt the user to enter the day number (between 1 and 366).
Check if the entered day number is within the valid range (1 to 366). If not,
terminate the program.
2. Accept N:
Prompt the user to enter the offset 'N' (1 <= N <= 100).
Check if the entered value of 'N' is within the valid range (1 to 100). If not, terminate
the program.
Call the isLeapYear function to check if the entered year is a leap year and update
the days in February accordingly.
Initialize variables for the current month, future day number, future month, and
future year.
Use a loop to find the month and day corresponding to the entered day number.
Calculate the future day number by adding 'N' to the entered day number.
isLeapYear: Takes a year as input and returns true if it is a leap year, false
otherwise.
5. Close Scanner:
Program Code:
import java.util.Scanner;
// Accept year
return;
}
// Accept N
int N = scanner.nextInt();
return;
scanner.close();
int[] daysInMonth = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
if (isLeapYear(year)) {
daysInMonth[2] = 29;
int month = 1;
dayNumber -= daysInMonth[month];
month++;
futureDayNumber -= daysInMonth[futureMonth];
futureMonth++;
if (futureMonth > 12) {
futureMonth = 1;
futureYear++;
} else {
return monthNames[month];
}
Variable Description:
2)
Algorithm:
1. Accept Input:
Create a Scanner object to read input from the user.
Prompt the user to enter a sentence terminated by '.', '?', or '!'.
Read and store the entered sentence.
Validate the terminating character. If it is not one of '.', '?', or '!', display an error
message and terminate the program.
2. Process Sentence:
Display the header "WORD\t\tCOUNT".
Split the sentence into words using a space as the delimiter.
Sort the words alphabetically.
Process each word:
Display the word.
Count the number of vowels and consonants in the word.
Display the vowel and consonant counts in one column.
3. Helper Function:
isVowel: Takes a character as input and returns true if it is a vowel (case-
insensitive), false otherwise.
4. Close Scanner:
Close the Scanner object to prevent resource leaks.
Program code:
import java.util.Arrays;
import java.util.Comparator;
import java.util.Scanner;
return;
processSentence(sentence);
System.out.println("WORD\t\tCOUNT");
Arrays.sort(words);
System.out.print(word + "\t\t");
int vowelCount = 0;
int consonantCount = 0;
if (isVowel(ch)) {
vowelCount++;
} else if (Character.isLetter(ch)) {
consonantCount++;
ch = Character.toUpperCase(ch);
Variable Description:
3)
Algorithm:
1. Accept Input:
Create a Scanner object to read input from the user.
Prompt the user to enter the number of rows (M) and columns (N).
Validate that both M and N are greater than 2 and less than 10. If not, display an
error message and terminate the program.
Declare a matrix A of size (M x N).
Prompt the user to enter elements for the matrix A.
2. Display Original Matrix:
Display the header "ORIGINAL MATRIX."
Use a nested loop to display the elements of matrix A.
3. Rotate Matrix:
Create a function rotateMatrix that takes a matrix as input and returns a new
matrix rotated by 270 degrees anti-clockwise.
Display the header "ROTATED MATRIX (270 DEGREES ANTI-CLOCKWISE)."
Call the rotateMatrix function on matrix A and display the rotated matrix.
4. Calculate Odd Sum:
Create a function calculateOddSum that takes a matrix as input and returns the
sum of odd elements.
Display the sum of odd elements calculated using the calculateOddSum function.
5. Close Scanner:
Close the Scanner object to prevent resource leaks.
Program code:
import java.util.Scanner;
int N = scanner.nextInt();
System.out.println("Invalid input. Both M and N must be greater than 2 and less than 10.");
return;
matrixA[i][j] = scanner.nextInt();
System.out.println("\nORIGINAL MATRIX:");
displayMatrix(matrixA);
displayMatrix(rotatedMatrix);
scanner.close();
System.out.print(element + "\t");
System.out.println();
rotatedMatrix[j][rows - 1 - i] = matrix[i][j];
return rotatedMatrix;
int sum = 0;
if (element % 2 != 0) {
sum += element;
return sum;
}
Variable Description: