[C language] Three ways to output leap year

Series Article Directory

 C language basic overall framework (2) http://t.csdn.cn/QyW6l

C language basic overall framework (2) http://t.csdn.cn/BqPr5

Guess the number game -- branch loop  http://t.csdn.cn/SNUTz


Table of contents

Series Article Directory

foreword

Problem Description: 

1. Branch cycle method

1. Knowledge combing:

 2. Nested use of if statement:

3. The use of operators:

2. Call function method

1. Use custom function to output leap year

at last


foreword

This article describes three methods of leap year output in detail from the perspective of loop branch, operator and function call.


96e755afbe0b4cc09edf82570228d418.jpeg  

Problem Description: 

       Print out the leap years between 1000 and 2000 on the screen, and output how many leap years there are in total.

提示:以下是本篇文章正文内容,下面案例可供参考,欢迎大佬批评指正~

1. Branch cycle method

1. Knowledge combing:

for loop:

for( expression1 ; expression2 ; expression3 )

{

        loop statement;

}

Expression 1:

      For the initialization part, it is used to initialize the loop variable;

Expression 2:

      It is the condition judgment part, which is used to judge the termination of the loop;

Expression 3:

       It is the adjustment part, which is used to adjust the cycle conditions.

//前闭后开
for(i=0;i<10;i++)
{}

//两边都是闭区间
for(i=0;i<=9;i++)
{}

//两个循环结构一样

Suggestion: The value of the loop control variable of the for statement adopts the writing method of "close before and open after".

 2. Nested use of if statement:

Nesting of if means that given two conditions must be met

       In the example for loop, if year%4==0 is satisfied, enter the first code block (a {} is a code block), execute the if(year%100!=0) statement, if the condition is met, then Output the leap year, otherwise execute the if(year%400==0) statement.

int main()
{
	int year;
	int count = 0;
	for (year = 1000; year <= 2000; year++)
	{
		if (year % 4 == 0)
		{
			if (year % 100 != 0)
			{
				count++;
				printf("%d ", year);
			}
		}
		if (year % 400 == 0)
		{
			count++;
			printf("%d ", year);
		}
	}
	printf("\ncount=%d", count);
	return 0;
}


3. The use of operators:

&& is true if both sides of the expression are true, otherwise it is false

  || One of them is true, the expression is true


int main()
{
	int i;
	int count = 0;
	for (i = 1000; i <= 2000; i++)
	{
		if ((i % 4 == 0 && i % 100 != 0) || (i % 400 == 0))
		{
			count++;
			printf("%d ",i);
		}
		
	}
	printf("\n%d", count);
	return 0;
}

2. Call function method

1. Use custom function to output leap year

If the condition is met in the self-defined function, return 1, return to the if statement in the main() function, and output leap year; otherwise return 0, do not meet the leap year condition, and cannot output.

//用函数输出闰年
int is_leap_year(int y)
{
	return((y % 4 == 0) && (y % 100 != 0) || (y % 400 == 0));
}
int main()
{
	int year;
	int count=0;
	for (year = 1000; year <= 2000; year++)
	{
		if (is_leap_year(year) == 1)
		{
			count++;
			printf("%d ", year);
		}
	}
	printf("\n%d\n", count);
	return 0;
}

at last

Happy time is always short. The above is what I want to talk about today. This article continues to briefly introduce Comrade Xiao Zhao's preliminary understanding of C language f and leap year output, and briefly introduces the knowledge used. Family members are welcome to criticize and correct. Comrade Xiao Zhao continues to update, the motivation for continuous learning is the support of Baozi with one button and three consecutive links~.

ade8068ba880467092e77593fcc9409f.gif

Guess you like

Origin blog.csdn.net/weixin_70411664/article/details/127284121