求c语言大神指导!Problems:1) The factorial of a nonnegative integer n is written (pronounced “n factorial”) and is defined as follows:=n*(n-1)*(n-2)*.*1(for value of n greater than or equal to 1)and= 1For example,= 5 × 4 × 3 × 2 × 1 = 1

来源:学生作业帮助网 编辑:六六作业网 时间:2024/11/23 11:32:55
求c语言大神指导!Problems:1)Thefactorialofanonnegativeintegerniswritten(pronounced“nfactorial”)andisdefineda

求c语言大神指导!Problems:1) The factorial of a nonnegative integer n is written (pronounced “n factorial”) and is defined as follows:=n*(n-1)*(n-2)*.*1(for value of n greater than or equal to 1)and= 1For example,= 5 × 4 × 3 × 2 × 1 = 1
求c语言大神指导!
Problems:
1) The factorial of a nonnegative integer n is written (pronounced “n factorial”) and is defined as follows:
=n*(n-1)*(n-2)*.*1(for value of n greater than or equal to 1)
and
= 1
For example,= 5 × 4 × 3 × 2 × 1 = 120
Factorials may be used to estimate the value of the mathematical constant e by using the following formula:
e=1+ 1/1!+ 1/2!+ 1/3!+.
Write a program that reads a nonnegative integer n and then computes its factorial and an estimate for the mathematical constant e (n in the formula for e will be the same value for which a factorial is found using the first equation).The original number entered by the user,the factorial and the estimate for e should all be printed to the screen.
Finally compute and output the value of using the following formula.x will be input by the user.
e^x = 1+ x/1!+ x^2/2!+x^3/3!+.

求c语言大神指导!Problems:1) The factorial of a nonnegative integer n is written (pronounced “n factorial”) and is defined as follows:=n*(n-1)*(n-2)*.*1(for value of n greater than or equal to 1)and= 1For example,= 5 × 4 × 3 × 2 × 1 = 1

这些英文理解起来真费劲呢,不过我知道了:

#include<stdio.h>
long factorial(int n)/*计算阶乘*/
{
if(n<0) return -1;/*小于0,返回-1说明无效*/
if(n=0) return 1;
return n*factorial(n-1);
}
double xx(double x,int i)/*计算x的整数次方*/
{
if(i==0) return 1;
return xx(x,i-1)*x;
}
double power(int x)/*计算上述级数*/
{
double result=0;/*存放结果*/
double t=0;/*存放每节的结果,目的是为了获取高精度*/
for(int i=0;t<1e-6;i++)
{
t=xx(x,i)/factorial(i);
result+=t;
}
return result;
}
main()
{
int x;
printf("Input an integer number:");/*提示语什么的,你自己看着办好了*/
scanf("%d",&x);
if(x<0){
printf("The factorial of %d is not valid.",x);/*负数的阶乘无效*/
printf("The e is %lf.",power(1));
printf("The Power x of e is %lf.",1/power(-x));/*负a次方等于正a次方的倒数*/
}else{
printf("The factorial of %d is &ld.",x,factorial(x));
printf("The e is %lf.",power(1));
printf("The Power x of e is %lf.",power(x));
}
}