C语言输入年、月、日,输出该日期是当年的第几天,考虑闰年和平年情况
时间: 2024-06-13 10:03:32 浏览: 148
这是一个C语言程序,可以输入年、月、日,输出该日期是当年的第几天,考虑闰年和平年情况。程序使用了一个数组来存储每个月的天数,然后使用循环来计算前几个月的总天数,最后加上当月的天数即可得到该日期是当年的第几天。同时,程序还考虑了闰年和平年的情况,如果是闰年并且输入的月份大于2月,则总天数需要加1。
下面是代码示例:
```
#include <stdio.h>
#include <stdlib.h>
int main() {
int year, mon, day, total, i;
int a[12] = { 31,28,31,30,31,30,31,31,30,31,30,31 }; //该数组存放了12个月各自的天数
while (scanf_s("%d%d%d", &year, &mon, &day) != EOF) {
int total = 0;
for (i = 0; i < mon - 1; i++) { //这个循环是为了统计前i个月的总天数
total = total + a[i];
}
total = total + day;
if (mon > 2) {
total = total + (year % 4 == 0 && year % 100 || year % 400 == 0);
}
printf("您输入的日期为:%d年 %d月 %d日\n", year, mon, day);
printf("%4d-%02d-%02d 是第 %d 天", year, mon, day, total);
}
}
```
阅读全文
相关推荐


















