C中的Revere计数程序(Revere counting program in C)

优质
小牛编辑
122浏览
2023-12-01

反向计数是整数的序列,按降序排列,不为零。 开发一个用C编程语言计数的程序很简单,我们将在本章中看到。

算法 (Algorithm)

让我们首先看看反向计数的逐步程序应该是什么 -

START
   Step 1 → Define start and end of counting
   Step 2 → Iterate from end to start
   Step 3 → Display loop value at each iteration
STOP

伪代码 (Pseudocode)

现在让我们看看这个算法的伪代码 -

procedure counting()
   FOR value = END to START DO
      DISPLAY value
   END FOR
end procedure

实现 (Implementation)

现在,我们将看到该计划的实际执行情况 -

#include <stdio.h>
int main() {
   int i, start, end;
   start = 1;
   end = 10;
   //reverse counting, we'll interchange loop variables
   for(i = end; i >= start; i--) 
      printf("%2d\n", i);
   return 0;
}

输出 (Output)

该计划的输出应为 -

10
 9
 8
 7
 6
 5
 4
 3
 2
 1