[ create a new paste ] login | about

Link: http://codepad.org/hk13trGc    [ raw code | output | fork ]

C++, pasted on Jan 14:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <stdio.h>
#include <math.h>
int fact(int x) { 
    if (x == 0) return 1;
    return x * fact(x - 1);
}
 
int main(void){
double x = 2, sum = 0, e = 0.1, truesumm;
int k =0;
truesumm = (exp(x)-exp(-x))/2;
printf("True summ =\t%f\n", truesumm);
while ((sum-truesumm)<e){
k=k+1;
sum=sum+(pow(x,(2*k-1))/fact(2*k-1));
}
printf("Summ of array =\t%f\n", sum);
printf("Numbers of iteration =\t%d\n",k);
return 0;
}


Output:
1
2
3
True summ =	3.626860
Summ of array =	3.745736
Numbers of iteration =	14


Create a new paste based on this one


Comments: