BeginnersBook

  • Home
  • Java
    • Java OOPs
    • Java Collections
    • Java Examples
  • C
    • C Examples
  • C++
    • C++ Examples
  • DBMS
  • Computer Network
  • Python
    • Python Examples
  • More…
    • jQuery
    • Kotlin
    • WordPress
    • SEO
    • JSON
    • JSP
    • JSTL
    • Servlet
    • MongoDB
    • XML
    • Perl

C Program to print number of days in a month

Last Updated: December 1, 2024 by Chaitanya Singh | Filed Under: C Programs

In this C Programs series, We will write a C program to print number of days in a month. If the entered month is 2 then it also checks whether the year is leap year or not.

C Program to print number of days in a given month

The explanation of the program and output is provided at the end of the code for better understanding of the logic used here.

#include <stdio.h>

// Function to check if a year is a leap year
int isLeapYear(int year) {
if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) {
return 1;
} else {
return 0;
}
}

// Function to get the number of days in a month
int getDaysInMonth(int month, int year) {
switch (month) {
case 1: // January
case 3: // March
case 5: // May
case 7: // July
case 8: // August
case 10: // October
case 12: // December
return 31;
case 4: // April
case 6: // June
case 9: // September
case 11: // November
return 30;
case 2: // In case of February
if (isLeapYear(year)) {
return 29;
} else {
return 28;
}
default:
return -1; // Invalid month
}
}

int main() {
int month, year;

// Prompt user for month and year
printf("Enter month (1-12): ");
scanf("%d", &month);
printf("Enter year: ");
scanf("%d", &year);

// Get the number of days in the given month
int days = getDaysInMonth(month, year);

// Print the result
if (days == -1) {
printf("Invalid month entered.n");
} else {
printf("Number of days: %dn", days);
}

return 0;
}

Explanation:

  1. isLeapYear() Function: This function checks if a given year is a leap year. A year is a leap year if it is divisible by 4 but not by 100, or if it is divisible by 400. This will ensure that number of days in Feb month gets printed as 29 days in leap year.
  2. getDaysInMonth Function(): This is the important function of our program as this is where the main logic is present. It returns the number of days in the entered month and year. It uses a switch statement to return the appropriate number of days for each month. For February, it checks if the year is a leap year to determine if it should return 28 or 29 days.
  3. main() Function: This is the entry point of the program. It prompts the user to enter a month and year, for which user wants to know the number of days. It calls getDaysInMonth() with the entered month and year and returns the output. If an invalid month is entered, it prints an error message.

Output:

1. Normal Year

Enter month (1-12): 2
Enter year: 2023
Number of days: 28

2. Leap Year

Enter month (1-12): 2
Enter year: 2024
Number of days: 29

3. Month with 31 days

Enter month (1-12): 7
Enter year: 2023
Number of days: 31

4. Month with 30 days

Enter month (1-12): 11
Enter year: 2023
Number of days: 30

5. Invalid month

Enter month (1-12): 13
Enter year: 2023
Invalid month entered.

Explanation of the Output

  1. Reading Input: The program first reads the month and year entered by the user. It stores these values in month and year int variables.
  2. Determining Days in Month: It calls the getDaysInMonth() function, which uses a switch statement to determine the number of days based on the month. For February, it checks if the year is a leap year by calling the isLeapYear() function.
  3. Print Result: If the entered month and year is valid, it prints the number of days as output, else it prints an error message.

Top Related Articles:

  1. C Program to print current date and time
  2. C Program to print date of birth using structure
  3. C Program to concatenate two strings without using strcat
  4. C Program to Check whether an Alphabet is Vowel or Consonant
  5. C Program to print the day for an input of date month and year

About the Author

I have 15 years of experience in the IT industry, working with renowned multinational corporations. Additionally, I have dedicated over a decade to teaching, allowing me to refine my skills in delivering information in a simple and easily understandable manner.

– Chaitanya

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Copyright © 2012 – 2025 BeginnersBook . Privacy Policy . Sitemap