C to program to evaluate the series 1/(1-x) = 1 + x + x^2 + x^3 + ................... + x^n

/* Write a C to program to evaluate the series
1/(1-x) = 1 + x + x^2 + x^3  + ................... + x^n
====================================
#include<stdio.h>
#include<conio.h>
#define LOOP 100
#define ACCURACY 0.0001
void main()
{
int n;
float x, term, sum;
printf(“input value of x :”);
scanf(“%f”, &x);
sum=0;
for(term=1, n=1; n<=LOOP; ++n)
{
sum+=term;
if(term<=ACCURACY)
goto output;
term*=x;
}
printf(“FINAL VALUE OF N IS NOT SUFFICIENT TO ACHIEVE DESIRED
ACCURACY”);
goto end;
output:
printf(“EXIT FROM LOOP”);
printf(“sum=%f; no. of terms=%d”, sum,n);
end:
;
}
Output:
Input value of x: .21
EXIT FROM LOOP
Sum=1.265800; No. of terms=7
Input value of x: .75
EXIT FROM LOOP
Sum=3.999774; No. of terms=34
Input value of x:.99
FINAL VALUE OF N IS NOT SUFFICIENT TO ACHIEVE DESIRED ACCURACY

No comments: