您好,欢迎访问一九零五行业门户网

C程序中的阶乘程序

given with the number n the task is to calculate the factorial of a number. factorial of a number is calculated by multiplying the number with its smallest or equal integer values.
factorial is calculated as −
0! = 11! = 12! = 2x1 = 23! = 3x2x1 = 64! = 4x3x2x1= 245! = 5x4x3x2x1 = 120...n! = n * (n-1) * (n-2) * . . . . . . . . . .*1
example的中文翻译为:示例input 1 -: n=5 output : 120input 2 -: n=6 output : 720
there are multiple methods that can be used −
through the loopsthrough recursion which is not at all effective through a functiongiven below is the implementation using functions
algorithmstartstep 1 -> declare function to calculate factorial int factorial(int n) if n = 0 return 1 end return n * factorial(n - 1)step 2 -> in main() declare variable as int num = 10 print factorial(num))stop
使用c语言例子#include<stdio.h>// function to find factorialint factorial(int n){ if (n == 0) return 1; return n * factorial(n - 1);}int main(){ int num = 10; printf("factorial of %d is %d", num, factorial(num)); return 0;}
输出factorial of 10 is 3628800
使用c++示例#include<iostream>using namespace std;// function to find factorialint factorial(int n){ if (n == 0) return 1; return n * factorial(n - 1);} int main(){ int num = 7; cout << "factorial of " << num << " is " << factorial(num) << endl; return 0;}
输出factorial of 7 is 5040
以上就是c程序中的阶乘程序的详细内容。
其它类似信息

推荐信息