黑马程序员技术交流社区
标题:
C语言计算阶乘
[打印本页]
作者:
菜鸟adambo
时间:
2015-10-23 23:37
标题:
C语言计算阶乘
如果数值是负数,那么阶乘就不存在。并且我们规定,0的阶乘就是1。
#include <stdio.h>
int main()
{
int n, count;
unsigned long long int factorial=1;
printf("Enter an integer: ");
scanf("%d",&n);
if ( n< 0)
printf("Error!!! Factorial of negative number doesn't exist.");
else
{
for(count=1;count<=n;++count) /* for loop terminates if count>n */
{
factorial*=count; /* factorial=factorial*count */
}
printf("Factorial = %lu",factorial);
}
return 0;
}
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2