Write a C program to input week number(1-7) and print day of week name using switch case. C program to find week day name using switch case. How to find day name of week using switch case in C programming.
Example
Input
Input
Input week number(1-7): 2
Output
Tuesday
Required knowledge
Basic C programming, Switch case statement
Logic to print day of week name using switch...case
Step by step descriptive logic to print day name of week.
- Input day number from user. Store it in some variable say week.
- Switch the value of week i.e. use
switch(week)
and match with cases. - There can be 7 possible values(choices) of week i.e. 1 to 7. Therefore write 7
case
insideswitch
. In addition, adddefault
case as an else block. - For
case 1:
print “MONDAY”, forcase 2:
print “TUESDAY” and so on. Print “SUNDAY” forcase 7:
. - If any case does not matches then, for
default:
case print “Invalid week number”.
You can also print day of week name using if...else
statement.
Program to print day of week name using switch...case
/**
* C program to print day of week using switch case
*/
#include <stdio.h>
int main()
{
int week;
/* Input week number from user */
printf("Enter week number(1-7): ");
scanf("%d", &week);
switch(week)
{
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! Please enter week number between 1-7.");
}
return 0;
}
In the above program I have assumed “Monday” as the first day of week.
Output
Enter week number(1-7): 1 Monday
Happy coding 😉
Recommended posts
- Switch case programming exercise index
- C program to check whether an alphabet is vowel or consonant using switch case
- C program to print number of days in a month using switch case
- C program to find maximum between two numbers using switch case
- C program to find even or odd using switch case.
- C program to create Simple calculator using switch case.