Series of pi with three aproximations in C Lang

Hector3436

Honorable
Sep 5, 2012
5
0
10,510
This is my code for calculating pi. My professor asked that the program's loops end when 3.14,3.141, and 3.1415 appear for the first time, no matter the decimals after the ones asked for. The problem is that it stops at 3.141 two n terms after it actually reaches it the first time, and when it reaches 3.1415 appears it is numerous n terms after the first time it appears as well. Any suggestions to fix this? Is the optimization needed in the condition or in the actual variable declarations? Any suggestions will be appreciated. Written in C Language.

#include<stdio.h>
#include<stdlib.h>
#include<math.h>
int main()
{
int term=0,a,b,c;
double n=0,sum=0;

printf("\n\tTerm\t\tSum\n\tN\n\n");

do{
sum+=4*pow(-1.,n)/(2*n+1);
n=n++;
printf("\t%d\t\t%f\n",term++,sum);
a=sum*100;
b=sum*1000;
c=sum*10000;
}
while(a!=314);
system("pause");

do{
sum+=4*pow(-1.,n)/(2*n+1);
n=n++;
printf("\t%d\t\t%f\n",term++,sum);
a=sum*100;
b=sum*1000;
c=sum*10000;
}
while(b!=3141);
system("pause");

do{
sum+=4*pow(-1.,n)/(2*n+1) ;
n=n++;
printf("\t%d\t\t%f\n",term++,sum);
a=sum*100;
b=sum*1000;
c=sum*10000;
}
while(c!=31415);
system("pause");
system("cls");
}
 
Solution
Thing is, it's calculating correctly, but it's outputing wrong. Let's say it calculated a sum of 3.1414995. It would print it as 3.141500, because it rounds the 7th digit.

Sunius

Distinguished
Dec 19, 2010
390
0
19,060
Thing is, it's calculating correctly, but it's outputing wrong. Let's say it calculated a sum of 3.1414995. It would print it as 3.141500, because it rounds the 7th digit.
 
Solution