c语言判断某年某月是否为闰年该月有多少天

要求

用C语言判断某年某月是否为闰年该月有多少天。

判断是否闰年满足以下其中一个条件即可
1.该年份能被 4 整除同时不能被 100 整除。
2.该年份能被400整除。

上代码

#include <stdio.h>

// 主函数
int main(int argc, char **argv)
{
	int year, month;
	int day[] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 };

	printf("请输入年份(0-?):");
	scanf_s("%d", &year);

	if (year < 0) {
		printf("输入年份有误");
		return -1;
	}

	printf("请输入月份(1-12):");
	scanf_s("%d", &month);
	
	if (month > sizeof(day)/ sizeof(int) - 1) {
		printf("输入月份有误");
		return -2;
	}

	if ((year % 400 == 0) || (year % 4 == 0 && year % 100 != 0)) {
		printf("您输入的是%d年%d月,该年是闰年,该月有%d天\n", year, month, month == 2 ? day[month] + 1 : day[month]);
	}
	else { 
		printf("您输入的是%d年%d月,该年是平年,该月有%d天\n", year, month, day[month]);
	}

	return 0;
}


调试结果:

_End

完事儿。

posted @ 2021-12-14 17:04  想想就很离谱  阅读(348)  评论(0编辑  收藏  举报