2-11 阶乘表

阶乘表

程序核心——自定义函数

程序

#include<stdio.h>
double fact(int n);//自定义函数声明
int main()
{
    int i,n;
    double result;
    
    printf("Enter n:");
    scanf("%d",&n);
    for(i=0;i<=n;i++)
    {
        result=fact(i);
        printf("%d!=%.0f\n",i,result);
    }
    return 0;
 } 
 //定义求n!函数 
 double fact(int n)
 {
    int i;
    double product;
    
    product=1;
    for(i=1;i<=n;i++)
    {
        product=product*i; 
     }
    return product;
 }

结果

Enter n:3
0!=1
1!=1
2!=2
3!=6

--------------------------------
Process exited after 2.453 seconds with return value 0
请按任意键继续. . .

分析

重点:自定义函数在运行时只能返回一个值,如需要返回多个值可用全局变量(影响代码安全,慎用)、数组名与指针(适用返回值类型一致)、结构体指针(类型不一致)

猜你喜欢

转载自www.cnblogs.com/5236288kai/p/10534717.html