Number of days in a month
C program to read any Month Number in integer and display the number of days for that month.
Program
#include<stdio.h>
#include<conio.h>
void main()
{
int num;
printf("Enter the month number between 1 to 12\t:");
scanf("%d",&num);
if(num == 1 || num == 3 || num == 5 || num == 7 || num == 8 || num == 10 || num == 12)
{
printf("Month has 31 days");
}
else if(num == 2)
{
printf("February month has 28 days and in leap year 29 days");
}
else if(num == 4 || num == 6 || num == 9 || num == 11)
{
printf("Month has 30 days");
}
else
{
printf("Invalid input");
}
getch();
}
Output
********** Run1 **********
Enter the month number between 1 to 12 :5
Month has 31 days
********** Run2 **********
Enter the month number between 1 to 12 :9
Month has 30 days
Explanation
Months in number are as follows:
1 - January
2 - February
3 - March
4 - April
5 - May
6 - June
7 - July
8 - August
9 - September
10 - October
11 - November
12 - December
January, March, May, July, August, October, December has 31 days. So, instead of making Different conditions for each month, OR (||) logical operator is used.
February has 28 or 29 days depending on the year (leap year or not)
April, June, September, November has 30 days. So, the condition is as follows:
1 - January
2 - February
3 - March
4 - April
5 - May
6 - June
7 - July
8 - August
9 - September
10 - October
11 - November
12 - December
January, March, May, July, August, October, December has 31 days. So, instead of making Different conditions for each month, OR (||) logical operator is used.
if(num == 1 || num == 3 || num == 5 || num == 7 || num == 8 || num == 10 || num == 12)
February has 28 or 29 days depending on the year (leap year or not)
else if(num == 2)
April, June, September, November has 30 days. So, the condition is as follows:
else if(num == 4 || num == 6 || num == 9 || num == 11)