Q:

Write a Python program and java program that asks the user for a number in the range of 1 through 7. The program should display the corresponding day of the week

0

Write a Python program that asks the user for a number in the range of 1 through 7. The program should display the corresponding day of the week, where 1=Monday, 2=Tuesday, 3=Wednesday, 4=Thursday, 5=Friday, 6=Saturday, and 7=Sunday. The program should display an error message if the user enters a number that is outside the range of 1 through 7.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

public class DaysOfweek {

public static void main (String args[]) {

/**creates an array of Strings with identifier days and

initialized. This array contains 7 elements */

String days [ ] = {"Monday", "Tuesday", "Wednesday", "Thursday",

"Friday", "Saturday", "Sunday"};


for(int i=0;i<7;i++){

System.out.println(days[i]);

}

}

}

 

one more example:

To get Sunday as 1 through Saturday as 7, this is the simplest solution to your question:

datetime.date.today().toordinal()%7 + 1

All of them:

import datetime

today = datetime.date.today()
sunday = today - datetime.timedelta(today.weekday()+1)

for i in range(7):
    tmp_date = sunday + datetime.timedelta(i)
    print tmp_date.toordinal()%7 + 1, '==', tmp_date.strftime('%A')

Output:

1 == Sunday
2 == Monday
3 == Tuesday
4 == Wednesday
5 == Thursday
6 == Friday
7 == Saturday

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Similar questions


need a help?


find thousands of online teachers now