使用C语言从键盘输入年月日输出,这一天是这一年中的第多少天考虑闰年
时间: 2023-04-05 10:05:11 浏览: 145
这个问题可以回答。以下是一个简单的 C 语言程序,可以实现这个功能:
```
#include <stdio.h>
int main() {
int year, month, day;
int days_in_month[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
int is_leap_year = 0;
int total_days = 0;
printf("请输入年月日,格式为 yyyy mm dd:");
scanf("%d %d %d", &year, &month, &day);
// 判断是否是闰年
if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) {
is_leap_year = 1;
}
// 计算总天数
for (int i = 0; i < month - 1; i++) {
total_days += days_in_month[i];
}
total_days += day;
if (is_leap_year && month > 2) {
total_days += 1;
}
printf("%d 年 %d 月 %d 日是这一年中的第 %d 天。\n", year, month, day, total_days);
return 0;
}
```
这个程序会先从键盘输入年月日,然后判断是否是闰年,接着根据每个月的天数计算出这一天是这一年中的第多少天,最后输出结果。
阅读全文
相关推荐















