Use switch statement to implement: calculate and output the number of days in a certain year and month

analysis program

Analyzing 12 months, three situations were combined.

Case 1: Months with 31 days, 1, 3, 5, 7, 8, 10, and December;

Situation 2: 30-day months: April, June, September, November;

Case 3: Use if-else to determine whether February has 29 days in a leap year and 28 days in a normal year.


Program implementation

#include<stdio.h>
int main()
{
	int year,month,days;
	printf("输入年月:");
	scanf("%d%d",&year,&month);
	switch(month)
	{
		case 1:
		case 3: 
		case 5:
		case 7:
		case 8:
		case 10:
		case 12:days=31;break;
		case 4:		
		case 6:		
		case 9:		
		case 11:days=30;break;
		case 2:
			if(year%4==0&&year%100!=0||year%400==0)
				days=29;
			else
				days=28;break;				
		default:printf("非法数字");
	}
	printf("%d年%d月有%d天",year,month,days);
	return 0;
}

Guess you like

Origin blog.csdn.net/studyup05/article/details/130196788