C语言求阶乘

#include <stdio.h>

//求n的阶乘
long factorial(int n) {
    if (n == 0 || n == 1) {
        return 1;
    }
    else {
        return factorial(n - 1) * n;  // 递归调用
    }
}
long jc(int n)
{
    long s=1;
    int a;
    for(a=1;a<=n;a++)
    s=s*a;
    return s;
 } 
int main() {
    int a;
    printf("Input a number: ");
    scanf("%d", &a);
    printf("Factorial(%d) = %ld\n", a, factorial(a));
    printf("jc(%d) = %ld\n", a, jc(a));

    return 0;
}

 

posted @ 2023-02-18 09:26  myrj  阅读(269)  评论(0编辑  收藏  举报