if limit is 4
how to use for calculat this program?
#include<stdio.h>
int main(void)
{
double tpt =0;
int i, limit;
for(i=0; i<=limit; i++)
{
1 1 1 1 1
tot += - + - + - + - + -
1*1 1*2 1*2*3 1*2*3*4 (limit-1)
(4-1)
1*2*3
}
if limit is 4
how to use for calculat this program?
#include<stdio.h>
int main(void)
{
double tpt =0;
int i, limit;
for(i=0; i<=limit; i++)
{
1 1 1 1 1
tot += - + - + - + - + -
1*1 1*2 1*2*3 1*2*3*4 (limit-1)
(4-1)
1*2*3
}
>1 1 1 1 1 tot += - + - + - + - + - 1*1 1*2 1*2*3 1*2*3*4 (limit-1) (4-1) 1*2*3
What is this? This isn't even *close* to a valid expression.
-Prelude
My best code is written with the delete key.
Hmmm, saw this post too late. There was another posting by you with this question. Can you tell us in words what you try to do?
Perhaps you should set limit to a value before comparing it.
Euler's equation
> Euler's equation
Um... okay.... Still doesn't explain your problem... Almost nothing in your code is valid C.
-Govtcheez
[email protected]
"Bad programming = Compiler errors" -- Euler's equation.
What's wrong in this program?
How to fix it?
use loop to terminates when the difference between
two succesive values of e differ by less than
0.0000001
#include<stdio.h>
void getLimit(int *);
void calculation(int);
int main(void)
{
int limit;
getLimit(&limit);
calculation(limit);
return 0;
}
/**************************GET LIMIT********************/
void getLimit(int *limit)
{
printf("Enter limit: ");
scanf("%d", limit);
return;
}
/**************************CALCULATION************* *******/
void calculation(int limit)
{
int i, j, p, flag=0;
double sum, diff, psum=1;
for(i=1; i<limit; i++)
{
p=1;
for(j=1; j<=i; j++)
p=p*j;
sum=psum + (1/p);
diff=psum-sum;
if(diff < 0.0000001)
{
flag=1;
break;
}
if(flag==1)
printf("Prev-next:%.2lf %.2lf %.2lf\n", psum, sum, diff);
else
printf("Num of iteration: %d, e=%.5lf\n", limit, sum);
}
return;
}
Your program looks a bit unstructured. Take some more effort in designing your algorithm.
Second, there are many formula's of Euler. But I think your pointing at the formula to calculate e.
e = sigma (x=0->inf) (1 / x!)
Which you need to implement as
e = sigma (x=0->limit) (1 / x!)
Which is
e = 1 / 0! + 1 / 1! + 1 / 2! + ... + 1 / limit!
As you can see there are two main calculations to be made
1. Sum
2. Faculty
A sum can be calculated by using a for-loop and faculty can be a function:
The faculty function is a recursive oneCode:e = 0 for (x = 0; x < limit; x++) { e += 1 / fac (x); }
x! = x * (x-1) * (x-2) * ... * 3 * 2 * 1
So you see the end-step is reached if x is 1 and the recursion step is x * (x - 1). The function is now easily created:
Code:int fac (int i) { if (i == 1 || i == 0) { return 1; } else { return (i * fac (i - 1)); } }