首页 > 试题广场 >

#编程序# ①定义函数 double fact( int n

[问答题]
#编程序#
①定义函数 double fact( int n) 计算 n! 的值。
②定义函数 double cal(float e) 计算下列算式的值,直到最后一项的绝对值小于 e ,函数返回值类型是 double 。要求调用函数 fact(n) 计算阶乘值。
③定义函数 main() ,输入正整数 n ,当精度 e 分别取值为 10-1 10-2 10-3 ……10-n 、时,分别计算并输出下列算式的值,直到最后一项的绝对值小于精度 e 。要求调用函数 cal(e) 计算下列算式的值。


  1. #include<stdio.h>
    double fact(int n);
    int main()
    {
        int n;
        printf("input an integer:");
        scanf("%d",&n);
        if(n==0)
        {
            printf("0!=1\n");
        }
        else if(n<0)
        {
            printf("Error!The factorial of negative does not exist.\n");
        }
        else
        printf("The factorial of the %d! = %.0lf\n",n,fact(n));
        return 0;
    }
    double fact (int n)
    {
        float factorial=1;
        int i;
        for(i=1;i<=n;i++)
        {
            factorial*=i;
        }
        return (factorial);
    }
    

发表于 2019-12-13 16:11:53 回复(0)