Given the integer day denoting the day number, print on the screen which day of the week it is. Week starts from Monday and for values greater than 7 or less than 1, print Invalid.
Ensure only the 1st letter of the answer is capitalised.
For printing use:-
Input: day = 3
Output: Wednesday
Input: day = 8
Output: Invalid
Input: day = 2
#include <bits/stdc++.h>
using namespace std;
class Solution {
public:
/* Function to determine the day of
the week based on day number */
void whichWeekDay(int day) {
// Check if the day number is valid
if (day < 1 || day > 7) {
cout << "Invalid";
return;
}
// Print the corresponding day of the week
switch (day) {
case 1: cout << "Monday"; break;
case 2: cout << "Tuesday"; break;
case 3: cout << "Wednesday"; break;
case 4: cout << "Thursday"; break;
case 5: cout << "Friday"; break;
case 6: cout << "Saturday"; break;
case 7: cout << "Sunday"; break;
}
}
};
int main() {
// Creating an instance of Solution class
Solution sol;
int day;
// Taking user input
cout << "Enter the day number: ";
cin >> day;
/* Function call to determine the day of
the week based on day number */
sol.whichWeekDay(day);
return 0;
}
import java.util.Scanner;
class Solution {
/* Function to determine the day of
the week based on day number */
public void whichWeekDay(int day) {
// Check if the day number is valid
if (day < 1 || day > 7) {
System.out.print("Invalid");
return;
}
// Print the corresponding day of the week
switch (day) {
case 1: System.out.print("Monday"); break;
case 2: System.out.print("Tuesday"); break;
case 3: System.out.print("Wednesday"); break;
case 4: System.out.print("Thursday"); break;
case 5: System.out.print("Friday"); break;
case 6: System.out.print("Saturday"); break;
case 7: System.out.print("Sunday"); break;
}
}
}
class Main {
public static void main(String[] args) {
// Creating an instance of Solution class
Solution sol = new Solution();
Scanner scanner = new Scanner(System.in);
int day;
// Taking user input
System.out.print("Enter the day number: ");
day = scanner.nextInt();
/* Function call to determine the day
of the week based on day number */
sol.whichWeekDay(day);
}
}
class Solution:
# Function to determine the day of
# the week based on day number
def whichWeekDay(self, day):
# Check if the day number is valid
if day < 1 or day > 7:
print("Invalid")
return
# Print the corresponding day of the week
if day == 1: print("Monday")
elif day == 2: print("Tuesday")
elif day == 3: print("Wednesday")
elif day == 4: print("Thursday")
elif day == 5: print("Friday")
elif day == 6: print("Saturday")
elif day == 7: print("Sunday")
# Creating an instance of Solution class
sol = Solution()
# Taking user input
day = int(input("Enter the day number: "))
# Function call to determine the day
# of the week based on day number
sol.whichWeekDay(day)
class Solution {
/* Function to determine the day of
the week based on day number */
whichWeekDay(day) {
// Check if the day number is valid
if (day < 1 || day > 7) {
console.log("Invalid");
return;
}
// Print the corresponding day of the week
switch (day) {
case 1: console.log("Monday"); break;
case 2: console.log("Tuesday"); break;
case 3: console.log("Wednesday"); break;
case 4: console.log("Thursday"); break;
case 5: console.log("Friday"); break;
case 6: console.log("Saturday"); break;
case 7: console.log("Sunday"); break;
}
}
}
// Creating an instance of Solution class
const sol = new Solution();
const readline = require('readline');
// Setting up readline interface for input
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
// Taking user input
rl.question("Enter the day number: ", function(day) {
/* Function call to determine the day
of the week based on day number */
sol.whichWeekDay(parseInt(day));
rl.close();
});
Time Complexity: O(1), The operations of receiving input and printing output are executed once regardless of the size of the input.
Space Complexity: O(1), We only store a single integer in the day variable, and no additional space is used that grows with the input size.