c语言输入年月日判断第几天
作者:野牛程序员:2023-07-02 10:49:45C语言阅读 2796
以下是一个使用C语言编写的程序,可以输入年月日并判断是该年的第几天:
#include <stdio.h> int isLeapYear(int year) { if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) return 1; // 是闰年 else return 0; // 不是闰年 } int main() { int year, month, day; int daysInMonth[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; int totalDays = 0; // 输入年月日 printf("请输入年份:"); scanf("%d", &year); printf("请输入月份:"); scanf("%d", &month); printf("请输入日期:"); scanf("%d", &day); // 判断闰年,修改二月天数 if (isLeapYear(year)) daysInMonth[1] = 29; // 计算总天数 for (int i = 0; i < month - 1; i++) { totalDays += daysInMonth[i]; } totalDays += day; // 输出结果 printf("%d年%d月%d日是该年的第%d天。\\n", year, month, day, totalDays); return 0; }
在该程序中,isLeapYear()
函数用于判断给定的年份是否是闰年。在主函数中,首先通过scanf()
函数分别输入年、月、日。然后,根据闰年的情况修改二月的天数。接下来,使用循环计算给定月份之前所有月份的天数,并将其累加到totalDays
变量中。最后,将输入的年、月、日以及计算得到的总天数输出到屏幕上。
野牛程序员教少儿编程与信息学奥赛-微信|电话:15892516892

- 上一篇:Python判断某年某月是这一年的第几天
- 下一篇:c语言编程判断回文数