C程序中的阶乘程序

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 function

Given below is the implementation using functions

Algorithm

StartStep 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// 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++

示例

#includeusing 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 7 is 5040

登录后复制

以上就是C程序中的阶乘程序的详细内容,更多请关注【创想鸟】其它相关文章!

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至253000106@qq.com举报,一经查实,本站将立刻删除。

发布者:PHP中文网,转转请注明出处:https://www.chuangxiangniao.com/p/2582787.html

(0)
上一篇 2025年3月6日 14:24:15
下一篇 2025年3月6日 14:24:24

AD推荐 黄金广告位招租... 更多推荐

相关推荐

发表回复

登录后才能评论