You are currently viewing C Program To Print Number Of Days For A Given Input Month Using If-else

C Program To Print Number Of Days For A Given Input Month Using If-else

In this tutorial, we will learn to write a C program to print the number of days for a given input month using if-else statements. In this article, we have explained the program with examples and algorithms for your better understanding.

Problem Statement

You have to Write a C program that prints the number of days for a given input month. Our program will take the input between 1 – 12. Here 1 is the first month and 12 is the last month of the year.

Examples

Enter the month number (1-12): 1
Output: The month January has 31 days.

Enter the month number (1-12): 4
Output: The month April has 30 days.

Enter the month number (1-12): 2
Output: The month February has 28 or 29 days.

Algorithm to Print Number Of Days For A Given Input Month

  1. Start the program.
  2. Declare a variable month of type int to store the input month number.
  3. Prompt the user to enter the month number by printing the message “Enter the month number (1-12): “.
  4. Read the input month number from the user and store it in the month variable using scanf().
  5. Use an if-else statement to determine the corresponding number of days based on the input month number:
    • If month is 1, print the message “The month January has 31 days.”
    • If month is 2, print the message “The month February has 28 or 29 days.”
    • If month is 3, print the message “The month March has 31 days.”
    • If month is 4, print the message “The month April has 30 days.”
    • If month is 5, print the message “The month May has 31 days.”
    • If month is 6, print the message “The month June has 30 days.”
    • If month is 7, print the message “The month July has 31 days.”
    • If month is 8, print the message “The month August has 31 days.”
    • If month is 9, print the message “The month September has 30 days.”
    • If month is 10, print the message “The month October has 31 days.”
    • If month is 11, print the message “The month November has 30 days.”
    • If month is 12, print the message “The month December has 31 days.”
    • If month is not between 1 and 12, print an error message “Invalid month number. Please enter a number between 1 and 12.”
  6. End the program

C Program to Print number of days for a given input month

#include <stdio.h>
int main() {
    int month;
    printf("Enter the month number (1-12): ");
    scanf("%d", &month);
    if (month == 1) {
        printf("The month January has 31 days.\n");
    }
    else if (month == 2) {
        printf("The month February has 28 or 29 days.\n");
    }
    else if (month == 3) {
        printf("The month March has 31 days.\n");
    }
    else if (month == 4) {
        printf("The month April has 30 days.\n");
    }
    else if (month == 5) {
        printf("The month May has 31 days.\n");
    }
    else if (month == 6) {
        printf("The month June has 30 days.\n");
    }
    else if (month == 7) {
        printf("The month July has 31 days.\n");
    }
    else if (month == 8) {
        printf("The month August has 31 days.\n");
    }
    else if (month == 9) {
        printf("The month September has 30 days.\n");
    }
    else if (month == 10) {
        printf("The month October has 31 days.\n");
    }
    else if (month == 11) {
        printf("The month November has 30 days.\n");
    }
    else if (month == 12) {
        printf("The month December has 31 days.\n");
    }
    else {
        printf("Invalid month number. Please enter a number between 1 and 12.\n");
    }
    return 0;
}

Output

Enter the month number (1-12): 5
The month May has 31 days

Program Explanation

  • The program starts by including the necessary header file stdio.h.
  • The main() function is defined.
  • Inside the main() function, a variable month of type int is declared to store the input month number.
  • The program prompts the user to enter the month number by printing the message “Enter the month number (1-12): “.
  • The scanf() function is used to read an integer from the user, and the input is stored in the month variable using the %d format specifier.
  • An if-else statement is used to determine the corresponding number of days based on the input month number:
    • The conditions month == 1, month == 2, …, month == 12 are checked to determine the month number.
    • If the month number matches one of the conditions, it prints the corresponding message using printf().
    • If the month number is not between 1 and 12, it prints an error message “Invalid month number. Please enter a number between 1 and 12.”
  • The return 0 statement ends the program.

Conclusion

This C program allows the user to enter a month number and prints the corresponding number of days using if-else statements. It covers all possible cases from 1 to 12 and provides the appropriate output based on the input month number.