After the year is entered, the number of days from January 1st of the year AD to January 1st of this year is calculated.

WBOY
Release: 2024-01-23 19:00:22
forward
854 people have browsed it

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;
}
Copy after login

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;
}
Copy after login

Summary

  1. (1) The first program calculates the number of days from January 1st of the year AD to January 1st of the specified year through a simple formula.
  2. (2) The second program calculates the number of days in the specified year and month by judging the number of days in the month and considering the situation of leap years.

After the year is entered, the number of days from January 1st of the year AD to January 1st of this year is calculated.

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!

source:docexcel.net
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!