Write a C program to enter month number and print total number of days in month using switch case. C program to find total number of days in a month. Logic to print number of days in a month using switch case.
Example
Input
Input month number: 3
Output
Total number of days = 31
Required knowledge
Basic programming, Switch case
Logic to find number of days in month
If you are a Codeforwin lover then you will find that. I have already explained this program using if else approach. Check out here, learn another method to solve this program.
Before we get into logic section for this program. Let us have a look to the table of days each month contains.
Month | Total days |
January | 31 days |
February | 28/29 days |
March | 31 days |
April | 30 days |
May | 31 days |
June | 30 days |
July | 31 days |
August | 31 days |
September | 30 days |
October | 31 days |
November | 30 days |
December | 31 days |
Let us now get down to the logic section for this program. Below is the step by step descriptive logic.
- Read month number from user, store it in some variable say month.
- Switch the month variable and match with cases.
- Print 31 for case 1, 3, 5, 7, 8, 10, 12.
- Print 30 for case 4, 6, 9, 11
- Print 28/29 for case 2.
- Print invalid input for default case.
Let us now implement this on to a C program
Program to print number of days in month using switch case
/** * C program to print number of days in a month using switch case */ #include <stdio.h> int main() { int month; /* * Read month number from user */ printf("Enter month number(1-12): "); scanf("%d", &month); switch(month) { case 1: printf("31 days"); break; case 2: printf("28/29 days"); break; case 3: printf("31 days"); break; case 4: printf("30 days"); break; case 5: printf("31 days"); break; case 6: printf("30 days"); break; case 7: printf("31 days"); break; case 8: printf("31 days"); break; case 9: printf("30 days"); break; case 10: printf("31 days"); break; case 11: printf("30 days"); break; case 12: printf("31 days"); break; default: printf("Invalid input! Please enter month number between 1-12"); } return 0; }Observe the above program carefully for a minute. You will notice that I have repeated printf("31 days") and 30 days for many cases. If a switch undergoes same task for various cases. Then you are free to remove the break statement and put all the repetitive statements in last repetitive case. As I did in below program, observe it carefully.
Program to find number of days in a month using switch case
/** * C program to print number of days in a month using switch case */ #include <stdio.h> int main() { int month; /* * Read month number from user */ printf("Enter month number(1-12): "); scanf("%d", &month); switch(month) { case 1: case 3: case 5: case 7: case 8: case 10: case 12: printf("31 days"); break; case 4: case 6: case 9: case 11: printf("30 days"); break; case 2: printf("28/29 days"); break; default: printf("Invalid input! Please enter month number between 1-12"); } return 0; }
Note: In the above program for any case in 1, 3, 5, 7, 8, 10, 12 the output will be 31 days. And for any case 4, 6, 9, 11 output will be 30 days.
Enter month number(1-12): 3 31 days
Happy coding ;)
You may also like
- Switch case programming exercise index.
- C program to create simple Calculator using switch case.
- C program to check whether an alphabet is vowel or consonant using switch case
- C program to enter week number and print day of week name using switch case
- C program to find maximum between two numbers using switch case
- C program to check whether a triangle is valid or not if all angles are given.
- C program to check whether a triangle is Equilateral, Isosceles or Scalene triangle.