6-8 简单阶乘计算 (10point(s)).c

本题要求实现一个计算非负整数阶乘的简单函数。

函数接口定义:

int Factorial( const int N );

其中N是用户传入的参数,其值不超过12。如果N是非负整数,则该函数必须返回N的阶乘,否则返回0。

裁判测试程序样例:

#include <stdio.h>

int Factorial( const int N );

int main()
{
    int N, NF;
	
    scanf("%d", &N);
    NF = Factorial(N);
    if (NF)  printf("%d! = %d\n", N, NF);
    else printf("Invalid input\n");

    return 0;
}

/* 你的代码将被嵌在这里 */

输入样例:

5

输出样例:

5! = 120
//   Date:2020/3/25
//   Author:xiezhg5
#include <stdio.h>

int Factorial( const int N );

int main()
{
    int N, NF;
	
    scanf("%d", &N);
    NF = Factorial(N);
    if (NF)  printf("%d! = %d\n", N, NF);
    else printf("Invalid input\n");

    return 0;
}

/* 你的代码将被嵌在这里 */
int Factorial( const int N )
{
	if(N==0) return 1;
	if(N<0) return 0;
	if(N>0) return N*Factorial(N-1); //递归计算阶乘 
}
发布了131 篇原创文章 · 获赞 94 · 访问量 2951

猜你喜欢

转载自blog.csdn.net/qq_45645641/article/details/105103319