pta exercise 3-5 output leap years (15 points)

Topic Collection of Zhejiang University Edition "C Language Programming (3rd 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 judgment condition for a leap year is that the year of the year can be divisible by 4 but not by 100 or by 400.

Input format:

Enter a certain cutoff 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 sample 1:

2004
2008
2012
2016
2020
2024
2028
2032
2036
2040
2044
2048

Input example 2:

2000

Output sample 2:

Invalid year!

Code:

#include <stdio.h>
#include <stdlib.h>

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

Submit result:

Insert picture description here

to sum up:

Standards for judging leap years:
1. Divides 4 but not 100.
2. Divides 400 evenly.
That is, there is a leap in four years, no leap in a hundred years, and a leap in four hundred years. It means: a year that is not a whole hundred is a leap year if it is divisible by 4, and a year that is a whole hundred must be divisible by 400 to be a leap year.

Guess you like

Origin blog.csdn.net/crraxx/article/details/109231622