用C语言输入任意年、月、日,输出其在本年的第几天
时间: 2024-04-07 18:33:03 浏览: 122
之前已经给出了相同的问题的解答,以下是对应的C语言代码:
```c
#include <stdio.h>
int isLeapYear(int year) {
if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0)
return 1;
else
return 0;
}
int getDayOfYear(int year, int month, int day) {
int daysInMonth[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
int dayOfYear = day;
for (int i = 1; i < month; i++) {
dayOfYear += daysInMonth[i];
if (i == 2 && isLeapYear(year))
dayOfYear++;
}
return dayOfYear;
}
int main() {
int year, month, day;
printf("请输入年份:");
scanf("%d", &year);
printf("请输入月份:");
scanf("%d", &month);
printf("请输入日期:");
scanf("%d", &day);
int dayOfYear = getDayOfYear(year, month, day);
printf("%d年%d月%d日是本年的第%d天\n", year, month, day, dayOfYear);
return 0;
}
```
运行程序后,按照提示输入年、月、日,即可输出其在本年的第几天。
阅读全文