目录

查找一年是否是闰年(Find if a year is leap year or not)

找到一年是不是飞跃有点棘手。 我们通常假设如果一年的数字可以被4整除,那就是闰年。 但这不是唯一的例子。 一年是闰年,如果 -

  • 它可以被100整除

    • 如果它可以被100整除,那么它也应该被400整除

  • 除此之外,所有其他可被4整除的年份都是闰年。

让我们看看如何创建一个程序来查找一年是否跳跃。

算法 (Algorithm)

该程序的算法是 -

START
   Step 1 → Take integer variable <code>year</code>
   Step 2 → Assign value to the variable
   Step 3 → Check if <code>year</code> is divisible by 4 but not 100, DISPLAY "leap year"
   Step 4 → Check if <code>year</code> is divisible by 400, DISPLAY "leap year"
   Step 5 → Otherwise, DISPLAY "not leap year"
STOP

流程图 (Flow Diagram)

我们可以绘制这个程序的流程图,如下所示 -

闰年流程图

伪代码 (Pseudocode)

这个算法的伪代码应该是这样的 -

procedure leap_year()
   IF year%4 = 0 AND year%100 != 0 OR year%400 = 0
      PRINT year is leap
   ELSE
      PRINT year is not leap
   END IF
end procedure

实现 (Implementation)

该算法的实现如下 -

#include <stdio.h>
int main() {
   int year;
   year = 2016;
   if (((year % 4 == 0) && (year % 100!= 0)) || (year%400 == 0))
      printf("%d is a leap year", year);
   else
      printf("%d is not a leap year", year);
   return 0;
}

输出 (Output)

该方案的产出应该是 -

2016 is a leap year
↑回到顶部↑
WIKI教程 @2018