Write a program to print corresponding day based on given inputInput Format:Accept integer as a inputOutput Format:Print corresponding day(0-6) or "Invalid" day- Sunday,Monday,Tuesday,Wednesday,Thursday,Friday,SaturdayConstraints:1 <= day_num <= 10^15
Question
Write a program to print corresponding day based on given inputInput Format:Accept integer as a inputOutput Format:Print corresponding day(0-6) or "Invalid" day- Sunday,Monday,Tuesday,Wednesday,Thursday,Friday,SaturdayConstraints:1 <= day_num <= 10^15
Solution
Sure, here is a step-by-step solution to write a program that prints the corresponding day based on the given input:
-
Start by accepting an integer as input. Let's call this input variable "day_num".
-
Check if the value of "day_num" is between 0 and 6 (inclusive). If it is, proceed to the next step. Otherwise, print "Invalid day" and end the program.
-
Create a list or an array of strings that represents the days of the week. Let's call this list "days_of_week". The first element in the list should be "Sunday", the second element should be "Monday", and so on.
-
Print the corresponding day by accessing the element in the "days_of_week" list at the index specified by "day_num". For example, if "day_num" is 0, print "Sunday". If "day_num" is 1, print "Monday", and so on.
-
End the program.
Here is an example implementation in Python:
def print_corresponding_day(day_num):
if 0 <= day_num <= 6:
days_of_week = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"]
print(days_of_week[day_num])
else:
print("Invalid day")
# Accept input from the user
day_num = int(input("Enter a number between 0 and 6: "))
# Call the function to print the corresponding day
print_corresponding_day(day_num)
This program prompts the user to enter a number between 0 and 6, and then calls the print_corresponding_day function to print the corresponding day based on the input. If the input is not within the valid range, it will print "Invalid day".
Similar Questions
Define a function named is_valid_day() which takes no parameters. The function prompts the user to enter a date and validates the day value only. The function returns True if the day value is within the specific range based on the particular month, and returns False otherwise. The months and their corresponding number of days are listed below:January, March, May, July, August, October, December: 31 days.February: 29 days (we consider a leap year for all cases).April, June, September, November: 30 days.For example:Test Input Resultprint(is_valid_day())2002-02-30Enter your birth date (yyyy-mm-dd): 2002-02-30Falseprint(is_valid_day())2003-04-31Enter your birth date (yyyy-mm-dd): 2003-04-31Falseprint(is_valid_day())2005-07-31Enter your birth date (yyyy-mm-dd): 2005-07-31True
Solution.java:23: error: ')' expected System.out.printf("The day of the week is -> %s %n%n%n", "daysOfWeek[dayOfWeek]"; ^
Create a Date class. Use polymorphism to increment the date by 1 day and print in different formats.
Create a short program that uses list_of_days to store a list of the seven days of the week. Ask the user to choose a day of the week by picking a number from 1-7. Print the day in the index position with that number. Use conditionals to display which days are weekdays and which are weekend days.
12345678910111213#include <stdio.h> int main() { int day = 2; switch (day * 2) { case 2: printf("Tuesday "); case 4: printf("Thursday "); default: printf("Weekday "); } return 0; }
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.