Hi All,
I'm trying to write a small program that approximates PI using Liebniz series (4*Pi = 1 - 1/3 + 1/5 - 1/7 + 1/9 - ...)
I think the implementation logic is correct ( though not efficient), the problem I am faced with is that any time i run the program by providing the number of terms of liebniz series required, the output is still 0.000. I have tried every diognostic method in my limited knowledge. Plz check out the code n see what is not right.
Code:#include <stdio.h> int main() { long double pi, v, temp2=0; int i=1,n,flg=1,count=0; printf("Enter the number of terms of Liebniz's series to use: "); scanf("%d",&n); while(count!=n) { if(i%2!=0){ count++; /* Problem HeRe */ v = 1.0/i; printf("\nCount%d",count); printf("\t%d",i); printf("\t%Lf",v); if(flg==1){ temp2+=v; flg=0; } else if (flg==0){ temp2-=v; flg=1; } } i++; } pi=4*temp2; printf("The Approximation of Pi = %Lf",pi); return 0; }
Thanx n looking forward to ur insights



LinkBack URL
About LinkBacks


