C program to print number of days in a month using switch case

Previous Program Next Program

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.

MonthTotal days
January31 days
February28/29 days
March31 days
April30 days
May31 days
June30 days
July31 days
August31 days
September30 days
October31 days
November30 days
December31 days
From the above table you can conclude that the month 1, 3, 5, 7, 8, 10, 12 contains 31 days. The months 4, 6, 9, 11 contains 30 days. And 2 month contains 28/29 days.

Let us now get down to the logic section for this program. Below is the step by step descriptive logic.

  1. Read month number from user, store it in some variable say month.
  2. Switch the month variable and match with cases.
  3. Print 31 for case 1, 3, 5, 7, 8, 10, 12.
  4. Print 30 for case 4, 6, 9, 11
  5. Print 28/29 for case 2.
  6. 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.

Output
Enter month number(1-12): 3
31 days

Happy coding ;)

You may also like

Previous Program Next Program
Any doubt or suggestion write here. I will try my best to help. Before posting your code you must escape it to view. To format your source code and use format highlighting, post your source code inside
< pre>< code >----Your Source Code---- < / code > < / pre > (Remove spaces from pre and code tags)

No comments:

Post a Comment