Explain switch case statement with example to read number between 1 to 7 and print relatively day Sunday to Saturday.

Question: Explain switch case statement with example to read number between 1 to 7 and print relatively day Sunday to Saturday.


switch case statement with example to read number between 1 to 7 and print relatively day Sunday to Saturday.

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 week number(1-7): 2
Output:
Tuesday

How the switch case works to print the day of the week name using the switch case:
  1. Input day number from user. Store it in some variable say week.
  2. Switch the value of week i.e. use switch(week) and match with cases.
  3. There can be 7 possible values(choices) of week i.e. 1 to 7. Therefore write 7 case inside switch. In addition, add default case as an else block.
  4. For case 1: print "MONDAY", for case 2: print "TUESDAY" and so on. Print "SUNDAY" for case 7:.
  5. 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
  1. /**
  2.  * C program to print day of week using switch case
  3.  */

  4. #include <stdio.h>

  5. int main()
  6. {
  7.     int week;
  8.     
  9.     /* Input week number from user */
  10.     printf("Enter week number(1-7): ");
  11.     scanf("%d", &week);
  12.     
  13.     switch(week)
  14.     {
  15.         case 1: 
  16.             printf("Monday");
  17.             break;
  18.         case 2: 
  19.             printf("Tuesday");
  20.             break;
  21.         case 3: 
  22.             printf("Wednesday");
  23.             break;
  24.         case 4: 
  25.             printf("Thursday");
  26.             break;
  27.         case 5: 
  28.             printf("Friday");
  29.             break;
  30.         case 6: 
  31.             printf("Saturday");
  32.             break;
  33.         case 7: 
  34.             printf("Sunday");
  35.             break;
  36.         default: 
  37.             printf("Invalid input! Please enter week number between 1-7.");
  38.     }

  39.     return 0;
  40. }

"Monday" is as the first day of week.

Output of switch case statement with example to read number between 1 to 7 and print relatively day Sunday to Saturday.

Download the Android app to get all Government Job Notifications on your Mobile.
Download Now
Important: Please always Check and Confirm the above details with the official Advertisement / Notification.
Previous Post Next Post