2、计算n的阶乘;计算1*3*5*7*....*(2n+1)
时间: 2023-12-03 18:03:32 浏览: 172
以下是两种计算n的阶乘和计算1*3*5*7*....*(2n+1)的方法:
1.计算n的阶乘
```python
def factorial(n):
if n == 0:
return 1
else:
return n * factorial(n-1)
n = 5
print(factorial(n)) # 输出:120
```
2.计算1*3*5*7*....*(2n+1)
```python
def odd_product(n):
if n == 0:
return 1
else:
return (2*n+1) * odd_product(n-1)
n = 5
print(odd_product(n)) # 输出:105
```
相关问题
计算n的阶乘;计算1*3*5*7*....*(2n+1)
计算n的阶乘:
```c
#include <stdio.h>
#include <stdlib.h>
int Factor(int n) //用函数调用来实现阶乘
{
int ret = 1; //保存每次相乘的结果,最终阶乘的结果
for (int i = 1; i <= n; ++i) {
ret = ret * i;
}
return ret;
}
int main()
{
int n;
scanf("%d", &n);
printf("%d\n", Factor(n)); //调用 Factor 函数实现 n!
system("pause");
return 0;
}
```
计算1*3*5*7*....*(2n+1):
```c
#include <stdio.h>
#include <stdlib.h>
int main()
{
int n;
scanf("%d", &n);
int ret = 1;
for (int i = 1; i <= n; ++i) {
ret = ret * (2 * i - 1);
}
printf("%d\n", ret);
system("pause");
return 0;
}
```
C语言计算n的阶乘;计算1*3*5*7*....*(2n+1)
Sure, I can help you with that. Here's a code snippet in C language to calculate the factorial of n:
```c
#include <stdio.h>
int main() {
int n, i, factorial = 1;
printf("Enter a number: ");
scanf("%d", &n);
for (i = 1; i <= n; i++) {
factorial *= (2*i - 1);
}
printf("Factorial of %d is %d\n", n, factorial);
return 0;
}
```
This program uses a `for` loop to iterate through the odd numbers (`2n-1`) up to `2n-1`, and multiplies them together to calculate the factorial of n.
阅读全文