Menu Close

使用递归函数计算一个自然数的阶乘

使用递归函数计算一个自然数的阶乘。

C Program to Compute Nth Factorial using Recursion

factorial of n (n!) = 1 * 2 * 3 * 4 *… * n

#include<stdio.h>
long int multiplyNumbers(int n);
int main() {
    int n;
    printf("Enter a positive integer: ");
    scanf("%d",&n);
    printf("Factorial of %d = %ld", n, multiplyNumbers(n));
    return 0;
}

long int multiplyNumbers(int n) {
    if (n>=1)
        return n*multiplyNumbers(n-1);
    else
        return 1;
}

递归函数求阶乘
递归函数求阶乘

Function in C

结果

Enter a positive integer: 6
Factorial of 6 = 720
除教程外,本网站大部分文章来自互联网,如果有内容冒犯到你,请联系我们删除!

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注

Leave the field below empty!

Posted in 函数