C语言作业

33 阅读1分钟

题目

image.png

答案

image.png

代码如下:

以下运用了 switch语句以及if...else语句,先输入要查询的月份,如果需要查询2月份,则要根据年份判断;如若是闰年则2月份为29天,否则为28天。

int main(){
	
	int mouth = 0;
	printf("请输入你查询的月份:");
	scanf("%d", &mouth);
	
	int year = 0;
	printf("请输入你的年份:");
	scanf("%d", &year);
	switch(mouth){
		case 1:
		case 3:
			printf("31天");
			break;
		case 2:
			if((year % 4 == 0 && year % 100 != 0) ||(year % 400 == 0)){
				printf("是闰年,29天");
		   }
		    else{
			    printf("不是闰年,28天");
		   }   
		   break;
		case 4:
			printf("30天");
			break;
		default:
			printf("你的月份不在处理范围内");
			break;
	}
}