C program to print number of days in a month

C program to print number of days in a month

In this blog post, we learn how to write a C program to print number of days in a month?. We will write the C program to print number of days in a month. Write a C program to input the month from the user and print the number of days. Write a C program to enter month number between(1-12) and print number of days using if-else. How to print the number of days in a given month using switch case in C programming. Logic to find the number of days for a given month.

Example,

Input: 3
Output: 31 days


Input: 12
Output: 31 days

 

We will find the number of days in a given month using the below description,

January, March, May, August, October, and December => 31 Days

April, June, September, and November => 30 Days

February => 28 or 29 (Leap Year) Days

 

 

C program to print number of days in a month using if-else:

The below program ask the user to enter the valid month. After getting the value of a month from the user program display the number of days using the if-else condition. We have used the above-mentioned description to find the number of days.

#include <stdio.h>

int main()
{
    int month;

    //Ask user to input month between 1 to 12
    printf("Enter month number(1-12): ");
    scanf("%d", &month);

    if(month == 1)
    {
        printf("31 days");
    }
    else if(month == 2)
    {
        printf("28 0r 29 days");
    }
    else if(month == 3)
    {
        printf("31 days");
    }
    else if(month == 4)
    {
        printf("30 days");
    }
    else if(month == 5)
    {
        printf("31 days");
    }
    else if(month == 6)
    {
        printf("30 days");
    }
    else if(month == 7)
    {
        printf("31 days");
    }
    else if(month == 8)
    {
        printf("31 days");
    }
    else if(month == 9)
    {
        printf("30 days");
    }
    else if(month == 10)
    {
        printf("31 days");
    }
    else if(month == 11)
    {
        printf("30 days");
    }
    else if(month == 12)
    {
        printf("31 days");
    }
    else
    {
        printf("Please enter month number between 1-12.");
    }

    return 0;
}

Output 1:

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

Output 2:

Enter month number(1-12): 15
Please enter month number between 1-12.

 

 

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

The below program ask the user to enter the valid month. After getting the value of a month from the user program display the number of days using the switch case. We have used the above-mentioned description to find the number of days.

#include <stdio.h>

int main()
{
    int month;

    //Ask user to input month between 1 to 12
    printf("Enter month number(1-12): ");
    scanf("%d", &month);

    switch(month)
    {
    case 1:
        printf("31 days");
        break;
    case 2:
        printf("28 or 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("Please enter month number between 1-12");

    }

    return 0;
}

Output 1:

Enter month number(1-12): 8
31 days

Output 2:

Enter month number(1-12): 14
Please enter month number between 1-12.

 

In the above code, you can see many cases or executing the same statements. So here we can combine the cases and reduce the code size. Let see the code,

#include <stdio.h>

int main()
{
    int month;

    //Ask user to input month between 1 to 12
    printf("Enter month number(1-12): ");
    scanf("%d", &month);

    switch(month)
    {
    // Group all 31 days cases together
    case 1:
    case 3:
    case 5:
    case 7:
    case 8:
    case 10:
    case 12:
        printf("31 days");
        break;

    //Group all 30 days cases together
    case 4:
    case 6:
    case 9:
    case 11:
        printf("30 days");
        break;

    //Remaining case
    case 2:
        printf("28/29 days");
        break;

    default:
        printf("Please enter month number between 1-12");
        break;
    }

    return 0;
}

Output :

Enter month number(1-12): 6
30 days

 

C program to print number of days in a month using an array:

The below program used a const string array to store the number of days on the corresponding array index. Now ask the user to enter the valid month number. After getting the value from the user get the number of days from the array using this value as an array index and display on the console.

#include <stdio.h>

int main()
{
    unsigned int month;

    // Declare constant name of weeks
    const char * daysInMonth[] = {"31","28 or 29", "31",
                                  "30","31","30","31",
                                  "31","30","31","30","31"
                                 };

    //Ask user to input month number
    printf("Enter month number (1-12): ");
    scanf("%u", &month);

    //prevent from go beyond array boundary
    if(month > 0 && month < 13)
    {
        // Print number of days for given month
        printf("%s %s", daysInMonth[month-1],"days");
    }
    else
    {
        printf("Please enter month number between 1-12.");
    }
    return 0;
}

Output :

Enter month number(1-12): 5
31 days

 

Recommended Articles for you: