简答题 C语言
题号 #2256717

下列给定程序中函数proc()的功能是计算1/n!的值。
例如,给n输入3,则输出0.166667。
请修改程序中的错误,使它能得到正确结果。
注意:不要改动main()函数,不得增行或删行,也不得更改程序的结构。
试题程序:
#include<stdio.h>
#include<conio.h>
//****found****
int proc(int n)
{
double t=1.0;
if(n==0)
return 1.0;
while(n>1&&n<170)
//****found****
t*=n++:
t=1/t:
return t;
}
void main()
{
int n;
printf("Input N:");
scanf("%d",&n);

printf("\hi/%d!=%lf\n",n,proc(n));
}

进入在线模考

参考答案

暂无

答案解析

(1)错误:int proc(int n)
正确:double proc(int n)
(2)错误:t*=n++;
正确:t*=n--;
【解析】由主函数中的函数调用以及函数proc()中的返回值可知,函数proc()的返回值类型为double型,因此,“int proc(int n)”应改为“double proc(int n)”;题目要求计算1/n!的值,整数n的阶乘为整数1到n相乘,因此,“t*=n ++;”应改为“t*=n--;”。

你可能感兴趣的试题

找试题 · 找答案 · 找资料 在线模考