93.7k views
3 votes
4) Days in a Month Write a C program that asks the user to enter the month (letting the user enter an integer in the range of 1 through 12) and the year. The program should then display the number of days in that month. Use the following criteria to identify leap years: 1. Determine whether the year is divisible by 100. If it is, then it is a leap year if and only if it is divisible by 400. For example, 2000 is a leap year but 2100 is not. 2. If the year is not divisible by 100, then it is a leap year if and if only it is divisible by 4. For example, 2008 is a leap year but 2009 is not. Remember that January, March, May, July, August, October and December have 31 days. April, June, September, November have 30 days. Use ints for the month and year.

User Vimo
by
5.5k points

1 Answer

3 votes

Answer:

// program in C

#include <stdio.h>

int main()

{

// variable

int m,y;

int days;

// validate month

do

{

printf("Enter a month (1-12) : ");

// read month

scanf("%d",&m);

}while(m<=0 || m >12);

printf("Enter a year : ");

// read year

scanf("%d",&y);

// months in which days are 30

if (m == 4 || m == 6 || m == 9 || m == 11)

days = 30;

// if month is feb

else if (m == 2)

// month in which days are 31

else

days = 31;

// print days in the given month

printf("Number of days:%d",days);

return 0;

}

Step-by-step explanation:

Read month from user.If entered month is not from 1-12 then ask until user enter a valid month.Read year from user.After this if month is 4,6,9 or 11 then days in the month are 30.If the month is 2 then check year is leap or not.If year is leap then 29 day else 28 days.If month is other than this then days are 31.

Output:

Enter a month (1-12) : 2

Enter a year : 2008

Number of days:29

User Jpgbarbosa
by
6.0k points