Learn-c-by-examples-factorial-program-in-c
提供:Dev Guides
Cの要因プログラム
正の整数nの階乗は、nから1までのすべての値の積です。 たとえば、3の階乗は(3 2 1 = 6)です。
アルゴリズム
このプログラムのアルゴリズムは非常に簡単です-
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
疑似コード
次のように上記のアルゴリズムの擬似コードを作成できます-
procedure find_factorial(number)
FOR value = 1 to number
factorial = factorial *value
END FOR
DISPLAY factorial
end procedure
実装
このアルゴリズムの実装は以下のとおりです-
#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;
}
出力
プログラムの出力は次のようになります-
Factorial of 5 = 120