6-8 简单阶乘计算 (10分)

左心房为你撑大大i 提交于 2020-01-23 10:41:07

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

函数接口定义:

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
int Factorial( const int N )
{
    int factor = 1;
    int is_factor = 0;
    if(N <= 12 && N > 0)
    {
    for(int i = 1; i <= N; i++)
    {
        factor *= i;
    }
     is_factor = factor;
    }
    else if(N == 0)
    {
        is_factor = 1;
    }
    else
    {
        is_factor = 0;
    }
       
     
   return is_factor; 
}

 

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!