Exercise 3-5 of the title collection of "C Language Programming (3rd Edition)" of Zhejiang University Edition

Exercise 3-5 Leap Year Output (15 points)

Output all leap years since the end of a certain year in the 21st century. Note: The judging condition for leap years is that the year of the year can be divisible by 4 but not by 100 or by 400.
Input format:

Enter a certain cut-off year for the 21st century in one line.
Output format:
output all leap year years that meet the conditions line by line, that is, each year occupies one line. If you input a year other than the 21st century, output "Invalid year!". If there is no leap year, output "None".
Input example 1:
2048
Output example 1:
2004
2008
2012
2016
2020
2024
2028
2032
2036
2040
2044
2048
Input example 2:
2000
Output example 2:
Invalid year!
Author
Chen Jianhai
Unit
Zhejiang University
Code length limit

16 KB
Time limit
400 ms
memory limit
64 MB

#include <stdio.h>


int main(void) {
    
    
    int year = 0;
    int count = 0;
    if (scanf("%d", &year) == 1) {
    
    
        if (2000 < year && year <= 2100) {
    
    
            for (int i = 2001; i <= year; i++) {
    
    
                if ((i % 4 == 0 && i % 100 != 0) || i % 400 == 0) {
    
    
                    printf("%d\n", i);
                    count++;
                }
            }
        } else {
    
    
            printf("Invalid year!");
        }
        if (count == 0 && 2000 < year && year < 2100) {
    
    
            printf("None");
        }
    }
    return 0;
}

Guess you like

Origin blog.csdn.net/DoMoreSpeakLess/article/details/109348285