Programming Example
Program to use switch statement. Display Monday to Sunday.
Program to use switch statement. Display Monday to Sunday.
/* Program to use switch statement. Display Monday to Sunday.
Author: Atnyla Developer */
#include<stdio.h>
#include<conio.h>
void main()
{
char ch;
printf("enter m for Monday \nt for Tuesday\nw for Wednesday\nh for Thursday\nf for Friday\ns for Saturday\nu for Sunday \n");
printf("...............");
scanf("%c",&ch);
switch(ch)
{
case 'm':
case 'M':
printf("monday \n");
break;
case 't':
case 'T':
printf("tuesday \n");
break;
case 'w':
case 'W':
printf("wednesday \n");
break;
case 'h':
case 'H':
printf("thursday \n");
break;
case 'f ':
case 'F':
printf("friday \n");
break;
case 's':
case 'S':
printf("saturday \n");
break;
case 'u':
case 'U':
printf("sunday \n");
break;
default:
printf("wrong input \n");
break;
}
getch();
}
enter m for Monday
t for Tuesday
w for Wednesday
h for Thursday
f for Friday
s for Saturday
u for Sunday
...............m
monday
Press any key to continue . . .
First read the algorithm, then study the program code line by line. After that, compare the code with the output and finally go through the explanation. This approach helps learners understand both the logic and the implementation properly.
After understanding this example, try to rewrite the same program without looking at the code. Then change some values or logic and run it again. This helps improve confidence and keeps learners engaged on the page for longer.