Ex: write a C program to print day of week using switch-case. How to write a C program to print day of week using switch-case. C program to print day of week using switch-case.
Input from user:
Enter number of day: 4
Expected output:
Thursday
Quick links:
1. Accept day number from user declare variable say no.
2. Switch the value of no using switch(no).
3. There are six possible values of no which are 1,2,3,4,5,6&7.
4. Therefore write 6 cases inside the switch.
5. If any case does not matches then for default case print enter number between 1-7.
#include<stdio.h>
int main()
{
int no;
printf("Enter number of day:");
scanf("%d",&no);
switch(no)
{
case 1: printf("\nMonday");
break;
case 2:
printf("\nTuesday");
break;
case 3:
printf("\nWednesday");
break;
case 4:
printf("\nThursday");
break;
case 5:
printf("\nFriday");
break;
case 6:
printf("\nSaturday");
break;
case 7:
printf("\nSunday");
break;
default:
printf("Enter number between 1-7");
}
return 0;
}
Above program shows the following output:
Input from user:
Enter number of day: 4
Expected output:
Thursday
Quick links:
Step by step logic of the given program:
1. Accept day number from user declare variable say no.
2. Switch the value of no using switch(no).
3. There are six possible values of no which are 1,2,3,4,5,6&7.
4. Therefore write 6 cases inside the switch.
5. If any case does not matches then for default case print enter number between 1-7.
C program to print day of week using switch-case:
#include<stdio.h>
int main()
{
int no;
printf("Enter number of day:");
scanf("%d",&no);
switch(no)
{
case 1: printf("\nMonday");
break;
case 2:
printf("\nTuesday");
break;
case 3:
printf("\nWednesday");
break;
case 4:
printf("\nThursday");
break;
case 5:
printf("\nFriday");
break;
case 6:
printf("\nSaturday");
break;
case 7:
printf("\nSunday");
break;
default:
printf("Enter number between 1-7");
}
return 0;
}
Above program shows the following output:
No comments:
Post a Comment
If you have any doubts, please discuss here...👇