Hello,
This program that is supposed to find pi, and it should approximate pi to some number of terms. This number is an input from user. However, for some reason, when I run it and enter 10 as my input, it doesn't do anything. No error, no return. Here is a screenshot of what I am stuck at.
Code:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
long double approx1 (int terms);
long double approx2 (int terms);
int main ()
{
long double pi, first, second;
int terms;
char ch;
printf ("\nEnter the number of terms to approximate pi, or 0 to terminate: ");
scanf ("%d", &terms);
while (terms != 0)
{
if (terms == 1)
{
pi = 4;
printf ("\nFirst approximation of pi to 1 term is %.8Lf\n\n", pi);
pi = sqrt(12);
printf ("Second approximation of pi to 1 term is %.8Lf\n\n", pi);
}
else if (terms > 1 )
{
first = approx1 (terms);
printf ("\nFirst approximation of pi to %d term is %.8Lf\n\n", terms, first);
second = approx2 (terms);
printf ("Second approximation of pi to %d term is %.8Lf\n\n", terms, second);
terms = 0;
}
else
{
printf ("\nWrong input, please try again\n\n");
}
printf ("Enter the number of terms to approximate pi, or 0 to terminate: ");
scanf ("%d", &terms);
}
printf ("\n\n***** Program Terminated *****\n");
return 0;
}
long double approx1 (int terms)
{
int i;
long double pi=0,denom=1.0,num=4.0;
for ( i =1; i = (terms+1); i++)
{
if ((i % 2) != 0)
{
pi = pi + (num / denom);
}
else
{
pi = pi - (num / denom);
}
denom = denom + 2.0;
}
return pi;
}
long double approx2 (int terms)
{
int i;
long double pi=0.0,denom = 1.0,num=12.0;
for ( i =1; i = (terms+1); i++)
{
if ((i % 2) != 0)
{
pi = pi + (num / denom);
}
else
{
pi = pi - (num / denom);
}
denom = (denom + 1.0)*(denom +1.0);
}
pi = sqrt(pi);
return pi;
}