c语言用函数实现: 计算并输出2的阶乘,6的阶乘,10的阶乘的值
时间: 2024-05-24 14:11:00 浏览: 23
#include <stdio.h>
int factorial(int n);
int main() {
int n1 = 2, n2 = 6, n3 = 10;
int result1 = factorial(n1);
int result2 = factorial(n2);
int result3 = factorial(n3);
printf("2的阶乘为:%d\n", result1);
printf("6的阶乘为:%d\n", result2);
printf("10的阶乘为:%d\n", result3);
return 0;
}
int factorial(int n) {
if (n == 1) {
return 1;
} else {
return n * factorial(n - 1);
}
}
相关问题
用c语言函数实现: 计算并输出2的阶乘,6的阶乘,10的阶乘的值
#include<stdio.h>
int factorial(int n) {
int result = 1;
for(int i=1; i<=n; i++) {
result *= i;
}
return result;
}
int main() {
int num1 = 2, num2 = 6, num3 = 10;
printf("%d的阶乘为%d\n", num1, factorial(num1));
printf("%d的阶乘为%d\n", num2, factorial(num2));
printf("%d的阶乘为%d\n", num3, factorial(num3));
return 0;
}
用c语言写出. 编程题 用函数实现: 计算并输出2的阶乘,6的阶乘,10的阶乘的值。
当然,很高兴为您解答这个问题。以下是C语言的函数实现方式:
```
#include <stdio.h>
int factorial(int n) {
if(n == 1) {
return 1;
} else {
return n * factorial(n-1);
}
}
int main() {
int n1 = 2, n2 = 6, n3 = 10;
printf("2的阶乘为%d\n", factorial(n1));
printf("6的阶乘为%d\n", factorial(n2));
printf("10的阶乘为%d\n", factorial(n3));
return 0;
}
```
希望这能帮到您。还有什么需要我帮忙的吗?
阅读全文