Home >Computer Tutorials >Computer Knowledge >After the year is entered, the number of days from January 1st of the year AD to January 1st of this year is calculated.
1. Enter a year from the keyboard and calculate how many days there are from January 1st of the year AD to January 1st of this year?
The following is a simple C program example for calculating the number of days in a specified year:
#include <stdio.h> int main() { int year; // 从键盘输入年份 printf("请输入年份:"); scanf("%d", &year); // 计算天数 int days = (year - 1) * 365 + (year - 1) / 4 - (year - 1) / 100 + (year - 1) / 400; // 输出结果 printf("公元1年1月1日到%d年1月1日有%d天。\n", year, days); return 0; }
2. Prepare a program to input the year and month from the keyboard to calculate and output How many days are there in this month of this year?
The following is an example of a simple C program to calculate the number of days in a specified year and month:
#include <stdio.h> int main() { int year, month; // 从键盘输入年份和月份 printf("请输入年份:"); scanf("%d", &year); printf("请输入月份:"); scanf("%d", &month); // 计算天数 int days; if (month == 2) { // 对于2月,判断是否是闰年 days = ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) ? 29 : 28; } else if (month == 4 || month == 6 || month == 9 || month == 11) { // 对于4、6、9、11月,设定为30天 days = 30; } else { // 其他月份设定为31天 days = 31; } // 输出结果 printf("%d年%d月共有%d天。\n", year, month, days); return 0; }
Summary
The above is the detailed content of After the year is entered, the number of days from January 1st of the year AD to January 1st of this year is calculated.. For more information, please follow other related articles on the PHP Chinese website!