Write a program that asks the user to enter the month (letting the user enter an
ID: 3796360 • Letter: W
Question
Write a program that asks the user to enter the month (letting the user enter an integer in the range of 1 through12) and the yer. The program should then display the number of days in that month. Us the following creteria to identify leap year:
1. Determine whether the year is divisble by 100. if it is, then it is a leap year and only if it is divisible by 400. For example,2000 is a leap year but yet 2100 us not.
2. if th year is not divisible by 100, then it is a lep year if and only if it is divisible by 4. For example, 2008 is a leap year but 2009 is not.
here is a sample run of the program:
enter a month (1-12) : 2 [Enter]
enter a year : 2008 [Enter]
29 days
Explanation / Answer
#include <stdio.h>
int main()
{
int month;
int year;
printf("Enter a month(1-12): ");
scanf("%d", &month);
printf("Enter a Year: ");
scanf("%d", &year);
switch(month)
{
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12: printf("31 days");
break;
case 4:
case 6:
case 9:
case 11: printf("30 days");
break;
case 2:
if((year%400==0)||((year%100!=0)&&(year%4==0)))
{
printf("29 days");
}
else
{
printf("28 days");
}
break;
default: printf("Invalid input! Please enter vaild month number between 1-12");
}
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.