1)
Algorithm:
1. Accept Input:
Create a Scanner object to read input from the user.
Prompt the user to enter the day number (between 1 and 366).
Read and store the entered day number.
Prompt the user to enter the year (in yyyy format).
Read and store the entered year.
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).
Read and store the entered value of 'N'.
Check if the entered value of 'N' is within the valid range (1 to 100). If not, terminate
the program.
3. Calculate Future Date:
Create an array daysInMonth to store the number of days in each month
(considering a leap year).
Call the isLeapYear function to check if the entered year is a leap year and update
the days in February accordingly.
Validate the entered day number.
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.
Display the entered date using the getMonthName function.
Calculate the future day number by adding 'N' to the entered day number.
Adjust the future date if it goes beyond the current year.
Display the future date using the getMonthName function.
4. Helper Functions:
getMonthName: Takes a month number as input and returns the corresponding
month name.
isLeapYear: Takes a year as input and returns true if it is a leap year, false
otherwise.
5. Close Scanner:
Close the Scanner object to prevent resource leaks.
Program Code:
import java.util.Scanner;
public class FutureDateCalculator {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// Accept day number
System.out.print("Enter day number (between 1 and 366): ");
int dayNumber = scanner.nextInt();
// Accept year
System.out.print("Enter year (yyyy): ");
int year = scanner.nextInt();
// Check if day number is within the limit
if (dayNumber < 1 || dayNumber > 366) {
System.out.println("Incorrect day number. Program terminated.");
return;
}
// Accept N
System.out.print("Enter N (1 <= N <= 100): ");
int N = scanner.nextInt();
// Check if N is within the limit
if (N < 1 || N > 100) {
System.out.println("Incorrect value of 'N'. Program terminated.");
return;
// Calculate the future date
calculateFutureDate(dayNumber, year, N);
// Close the scanner
scanner.close();
private static void calculateFutureDate(int dayNumber, int year, int N) {
// Array to store the number of days in each month
int[] daysInMonth = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
// Check for a leap year
if (isLeapYear(year)) {
daysInMonth[2] = 29;
// Validate the day number
if (dayNumber > 0 && dayNumber <= 366) {
int month = 1;
// Find the month and day corresponding to the day number
while (dayNumber > daysInMonth[month]) {
dayNumber -= daysInMonth[month];
month++;
// Display the entered date
System.out.printf("Entered date: %s %d, %d\n", getMonthName(month), dayNumber, year);
// Calculate the future date
int futureDayNumber = dayNumber + N;
int futureMonth = month;
int futureYear = year;
// Adjust the future date if it goes beyond the current year
while (futureDayNumber > daysInMonth[futureMonth]) {
futureDayNumber -= daysInMonth[futureMonth];
futureMonth++;
if (futureMonth > 12) {
futureMonth = 1;
futureYear++;
// Display the future date
System.out.printf("%d days later: %s %d, %d\n", N, getMonthName(futureMonth),
futureDayNumber, futureYear);
} else {
System.out.println("Incorrect day number. Program terminated.");
private static String getMonthName(int month) {
String[] monthNames = {"", "January", "February", "March", "April", "May", "June",
"July", "August", "September", "October", "November", "December"};
return monthNames[month];
private static boolean isLeapYear(int year) {
return (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0);
}
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;
public class WordFrequencyAnalyzer {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// Accept a sentence terminated by '.', '?', or '!'
System.out.print("Enter a sentence (terminated by '.', '?', or '!'): ");
String sentence = scanner.nextLine();
// Validate the terminating character
char lastChar = sentence.charAt(sentence.length() - 1);
if (lastChar != '.' && lastChar != '?' && lastChar != '!') {
System.out.println("Incorrect terminating character. Invalid input.");
return;
// Process the sentence
processSentence(sentence);
// Close the scanner
scanner.close();
private static void processSentence(String sentence) {
// Display the header
System.out.println("WORD\t\tCOUNT");
// Split the sentence into words
String[] words = sentence.split(" ");
// Sort the words alphabetically
Arrays.sort(words);
// Process each word
for (String word : words) {
// Display the word
System.out.print(word + "\t\t");
// Count vowels and consonants
int vowelCount = 0;
int consonantCount = 0;
// Process each character in the word
for (char ch : word.toCharArray()) {
if (isVowel(ch)) {
vowelCount++;
} else if (Character.isLetter(ch)) {
consonantCount++;
// Display the count in one column
System.out.println("V: " + vowelCount + "\tC: " + consonantCount);
private static boolean isVowel(char ch) {
ch = Character.toUpperCase(ch);
return ch == 'A' || ch == 'E' || ch == 'I' || ch == 'O' || ch == 'U';
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;
public class MatrixOperations {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// Accept the dimensions of the matrix
System.out.print("Enter the number of rows (M): ");
int M = scanner.nextInt();
System.out.print("Enter the number of columns (N): ");
int N = scanner.nextInt();
// Validate the dimensions
if (M <= 2 || N <= 2 || M >= 10 || N >= 10) {
System.out.println("Invalid input. Both M and N must be greater than 2 and less than 10.");
return;
// Declare and input elements into the matrix
int[][] matrixA = new int[M][N];
System.out.println("Enter elements for the matrix:");
for (int i = 0; i < M; i++) {
for (int j = 0; j < N; j++) {
matrixA[i][j] = scanner.nextInt();
// (a) Display the input matrix
System.out.println("\nORIGINAL MATRIX:");
displayMatrix(matrixA);
// (b) Rotate the matrix by 270 degrees anti-clockwise
int[][] rotatedMatrix = rotateMatrix(matrixA);
// Display the rotated matrix
System.out.println("\nROTATED MATRIX (270 DEGREES ANTI-CLOCKWISE):");
displayMatrix(rotatedMatrix);
// (c) Calculate the sum of odd elements in the matrix
int oddSum = calculateOddSum(matrixA);
System.out.println("\nSUM OF THE ODD ELEMENTS = " + oddSum);
scanner.close();
private static void displayMatrix(int[][] matrix) {
for (int[] row : matrix) {
for (int element : row) {
System.out.print(element + "\t");
System.out.println();
private static int[][] rotateMatrix(int[][] matrix) {
int rows = matrix.length;
int cols = matrix[0].length;
int[][] rotatedMatrix = new int[cols][rows];
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
rotatedMatrix[j][rows - 1 - i] = matrix[i][j];
return rotatedMatrix;
private static int calculateOddSum(int[][] matrix) {
int sum = 0;
for (int[] row : matrix) {
for (int element : row) {
if (element % 2 != 0) {
sum += element;
return sum;
}
Variable Description: