c语言输入年份和月份输出对应天数
作者:野牛程序员:2023-11-23 17:17:10C语言阅读 5160
输入年份和月份,输出对应的天数。
#include <stdio.h>
int main() {
int year, month, days;
// 输入年份和月份
printf("请输入年份: ");
scanf("%d", &year);
printf("请输入月份: ");
scanf("%d", &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("无效的月份\\n");
return 1; // 退出程序,返回错误码
}
// 输出结果
printf("%d年%d月有%d天\\n", year, month, days);
return 0; // 返回成功码
}这段代码通过输入年份和月份,使用switch语句判断每个月的天数,并考虑了闰年的情况。最后输出结果,表示输入的年份和月份对应的天数。
野牛程序员教少儿编程与信息学奥赛-微信|电话:15892516892

- 上一篇:python中字典与json相互转换的方法
- 下一篇:c++计算两个日期之间的天数
