Hey guys, I cant see whats going wrong here, values for theta calculate out to a very strange value. Function is suppose to estimate pi by doing sqrt(6*(1/1*1)+1/(2*2)+1/(3*3) etc using 5 and 10 terms. Please help if possible! Thanks alot.
Sam
Code:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
double theta(int terms);
const double piValue = 3.141592653589793;
int main (void)
{
int terms;
printf("The value of pi is %lf\n", piValue);
terms=5;
printf("When using %d terms:\n", terms);
printf("theta = %lf\n", theta(terms));
printf("The difference between theta and pi is %lf\n\n",
theta(terms) - piValue);
terms=10;
printf("When using %d terms:\n", terms);
printf("theta = %lf\n", theta(terms));
printf("The difference between theta and pi is %lf\n\n",
theta(terms) - piValue);
system("pause");
return 0;
}//main
double theta(int terms)
{
int denom;
double sumOfTerms;
for(denom = 1; denom <= terms; denom++)
{
sumOfTerms += (double)1/(denom*denom);
}
return sqrt (6*sumOfTerms);
}