Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Question 1 Write a program that given a number from 1 to 7 outputs what day of t

ID: 3591402 • Letter: Q

Question


Question 1 Write a program that given a number from 1 to 7 outputs what day of the week it corresponds to (1-Monday, 7 = Sunday) and whether it is a weekday or weekend. You may assume an integer as input but if the input is not from 1 to 7, you should output that it's not a valid day. Write two versions of this program: Version 1: Use an if/else statement Version 2: Use a Switch statement (instead of an if/else) Here are a few sample outputs to illustrate the expected behavior of your program. Note: user input is highlighted in grey Please enter the day of the week as a number (1-7): 7 It's Sunday! It's the weekend! Please enter the day of the week as a number (1-7): 5 It's Friday! It's a weekday!

Explanation / Answer

1. Using If/else statement

if/else statements will executes code, test expression is true and some other code, the test expression is false (0).

if (testExpression) {
// codes inside the body of if
}
else {
// codes inside the body of else
}

#include <stdio.h>
int main()
{
int week;

/* Input week number from user */ printf("Enter week number (1-7): ");
scanf("%d", &week);

if(week == 1)
{
printf("Monday");
}
else if(week == 2)
{
printf("Tuesday");
}
else if(week == 3)
{
printf("Wednesday");
}
else if(week == 4)
{
printf("Thursday");
}
else if(week == 5)
{
printf("Friday");
}
else if(week == 6)
{
printf("Saturday");
}
else if(week == 7)
{
printf("Sunday");
}
else
{
printf("Invalid Input! Please enter week number between 1-7.");
}

return 0;
}

Input

Input week number: 1

Output

2. Using Switch

switch statement will allow variable to be tested from the list of values. here each value is called a case, variable switched on is checked for each switch case.

#include <stdio.h>

int main() {  

int day;

printf("Enter Day Number (1 = Monday ..... 7 = Sunday) ");  

scanf("%d", &day); _

  

switch(day){

case 1 : printf("Monday ");

break;

case 2 : printf("Tuesday ");

break;

case 3 : printf("Wednesday ");

break;

case 4 : printf("Thursday ");

break;

case 5 : printf("Friday ");

break;

case 6 : printf("Saturday ");

break;

case 7 : printf("Sunday ");

break;

default: printf("Invalid Input !!!! ");   

}

  

return 0;  

}

Output

Enter Day Number (1 = Monday ..... 7 = Sunday)

4

Thursday

Enter Day Number (1 = Monday ..... 7 = Sunday)

11

Invalid Input

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote