因子数(Factorial Number)

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

正整数n的因子是从n到1的所有值的乘积。例如,3的阶乘是(3 * 2 * 1 = 6)。

算法 (Algorithm)

这个程序的算法很简单 -

START
   Step 1 → Take integer variable A
   Step 2 → Assign value to the variable
   Step 3 → From value A upto 1 multiply each digit and store
   Step 4 → the final stored value is factorial of A
STOP

伪代码 (Pseudocode)

我们可以按如下方式起草上述算法的伪代码 -

procedure find_factorial(number)
   FOR value = 1 to number
      factorial = factorial * value
   END FOR
   DISPLAY factorial
end procedure

实现 (Implementation)

该算法的实现如下 -

#include <stdio.h>
int main() {
   int loop;
   int factorial=1;
   int number = 5;
   for(loop = 1; loop<= number; loop++) {
      factorial = factorial * loop;
   }
   printf("Factorial of %d = %d \n", number, factorial);
   return 0;
}

输出 (Output)

该方案的产出应该是 -

Factorial of 5 = 120