C language--judging whether this year is a leap year

First of all, we need to know what is the criterion for judging a leap year

There are two criteria for judging a leap year:

1. Divisible by 100 and not divisible by 100 at the same time.

2. Divisible by 400.

At this point we can write code

#include <stdio.h>

int main()
{
	int year=0;
	scanf("%d",&year);
	if(year%4==0 && year%100!=0)
	{
		printf("%d 是闰年",year);
	}
	if(year%400==0)
	{
		printf("%d 是闰年",year);
	}
	else
		printf("%d 不是闰年",year);
	
	return 0;
}

Of course, if you are more proficient in logic, you can also simplify the code, or use other statements to run, such as while statements, etc., it depends on your personal habits.

The simplified code is as follows:

#include <stdio.h>

int main()
{
	int year=0;
	scanf("%d",&year);
	if((year%4==0 && year%100!=0) || year%400==0)
	{
		printf("%d 是闰年",year);
	}
    else 
     printf("%d 不是闰年",year);
	
	return 0;
}

 The code for judging leap year is relatively simple, but it is also the most basic, so everyone should do the basics well, so that they can be handy when writing code.

Guess you like

Origin blog.csdn.net/m0_59314318/article/details/125636643