help for C program!!!

来源:百度知道 编辑:UC知道 时间:2024/04/28 00:35:29
use of "for" loops to calcuate the following value:E(x)=1+x+x2/2!+x3/3!+x4/4!+.....+xn/n! where n!=1*2*3*4*........*n, value of float "x" and interger"n" are supplied by user

please give me a hand !... thank you everybody!

#include<stdio.h>
#include<stdlib.h>

float E(float x, int n);
void main(void)
{
float a;
int b;
scanf("%f %d",&a,&b);
printf("%f",E(a,b));
}

float E(float x, int n)
{

float re=1;
int i;
float xx=x;
for(i=1;i<=n;i++)
{
re+=x;
xx*=x*1.0/(i+1);
}
return re;
}

这样才对:

#include "stdio.h"
#include "conio.h"
void main()
{
float x;
int n;
float sum;
float temp;
sum = 1;
int div;
printf("input n(int): ");
scanf("%d", &n);
printf("input x(float): ");
scanf("%f", &x);
for(int i=1; i<=n; i++)
{
div = 1;
for(int j=1; j<=i; j++)
div *= j;
temp = (float)i/div;
sum+=temp*x;
}
printf("sum: %f", sum);