Enter an integer between 1800 and 2800 as the year to determine whether the year is a leap
year. A leap year is one of the following:
① is divisible by 4 but not by 100;
② is divisible by 400.
#include <stdio.h>
main()
{
int year,flag;
printf("请输入年份:");
【1】;
【2】
printf("年份超出范围。");
else
{
if(year%4==0)
{
【3】
{
if(year%400==0)
flag=1;
【4】
flag=0;
}
else
【5】;
}
else
【6】;
【7】
printf("%d 是闰年。\n",year);
else
printf("%d 不是闰年。\n",year);
}
}
0 Answer
the reference answer GPT ᴼ ᴾ ᴱ ᴺ ᴬ ᴵ < br / > here are the modified code:
#include
int main() {
int year, flag = 0;
printf("请输入年份:");
scanf("%d", &year);
if (year < 1800 || year > 2800) {
printf("年份超出范围。");
} else {
if (year % 4 == 0) {
if (year % 100 != 0) {
flag = 1;
} else {
if (year % 400 == 0) {
flag = 1;
}
}
}
if (flag) {
printf("%d 是闰年。\n", year);
} else {
printf("%d 不是闰年。\n", year);
}
}
return 0;
}
Modification description:
This answer quotes ChatGPT
Full C code implementation, using the if-else structure and scanf function to get the year the user entered:
#include
int main() {
int year, flag;
printf("请输入年份:");
scanf("%d", &year);
if (year < 1800 || year > 2800) {
printf("年份超出范围。");
} else {
if (year % 4 == 0) {
if (year % 100 != 0) {
flag = 1;
} else {
if (year % 400 == 0) {
flag = 1;
} else {
flag = 0;
}
}
} else {
flag = 0;
}
if (flag == 1) {
printf("%d 是闰年。\n", year);
} else {
printf("%d 不是闰年。\n", year);
}
}
return 0;
}
1. Obtain the year entered by the user through the scanf function and store it in the year variable.
2. If the entered year is not in the range of 1800 to 2800, an error message is displayed, and the program ends.
3. If the year is divisible by 4, go to Step 4. Otherwise, go to Step 6.
4. If the year is divisible by 100, perform Step 5. Otherwise, it means leap year, flag = 1.
5. If the year is divisible by 400, it is a leap year, marked as flag = 1; Otherwise, it is not a leap year and flag = 0.
6. If the year is not divisible by 4, it is not a leap year and flag = 0.
7. Judge the result according to flag.
这家伙很懒,什么都没留下...