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:
- 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.
- 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. - 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
- Reading Input: The program first reads the month and year entered by the user. It stores these values in
month
andyear
int variables. - Determining Days in Month: It calls the
getDaysInMonth()
function, which uses aswitch
statement to determine the number of days based on the month. For February, it checks if the year is a leap year by calling theisLeapYear()
function. - Print Result: If the entered month and year is valid, it prints the number of days as output, else it prints an error message.
Leave a Reply