# Thread: need some help with pi() func!!!!!!

1. ## need some help with pi() func!!!!!!

i need to build a basic program that print the num pi....
output: 3.0000000000
what is wrong??
Code:
```# include <stdio.h>
double long exp1(double base, unsigned long exp)
{
double sum=1;
unsigned long i;
for (i=0; i<exp; i++)
{
sum=sum*base;
}
return sum;
}
double long pi(int k)
{
double long sum=0;
if (k==0)
{
sum=(47/15);
return sum;
}
sum=((1/(exp1(16,k)))*((4/((8*k)+1))-(2/((8*k)+4))-(1/((8*k)+5))-(1/((8*k)+6)))) + pi(k-1);
return sum;
}
int main()
{
int k=0;
printf("please enter how many k of pi do you like\n");
scanf_s("%d",&k);

if (k<0)
{
while (k<0)
{
scanf_s("%d",&k);
}
}
printf("the number is %.10lf\n",pi(k));
return 0;
}```

2. The biggest problem I see is all the integer math. Remember when using integer math there are no fractions. So sum=(47/15); would evaluate to 3 not 3.1333333, 1/2 would evaluate to zero not .5.

Jim

3. When using integer fraction will be truncated.
Solution:add .0 extentions for integer(example:47.0/15) or type casting it(example:sum=(float(47),15)).
Sorry my english is very bad!.

4. I realize that you're trying to compute it in a very specific way, but FWIW, a very simple and useful equivalence to remember is PI == 4 * atan(1).

5. Your function declarations are wrong. You mean long double not double long.