WatchGuard DNSWatch reduces malware infections by detecting and blocking malicious DNS requests, improving your ability to protect employees from phishing attacks. Learn more about our newest service included in Total Security Suite today!

I need to know how to write a recursive function to calculate pi using the gregory series...based on user input.

For example I'm requesting the user to input the number of terms to calculate pi based on the Gregory series. Please help!

Thank you

For example I'm requesting the user to input the number of terms to calculate pi based on the Gregory series. Please help!

Thank you

I know how to calculate pi with the gregory series in C without a function but I can't figure out how to utilize a recursive function to calculate pi using the gregory series.

double piby4(int terms)

{

if (terms==1)

return(piby4(1.0/terms+ter

else if (terms %2==0)

return(piby4(-1.0/terms+te

else if (terms %2)

return(piby4(+1.0/terms+te

else

return (0);

}

down in int main

double pi, pidiv4;

int terms;

printf("How many terms do you want to use to calculate pi? ");

fgets(buffer, sizeof(buffer), stdin);

sscanf(buffer, "%d", &terms);

pidiv4=piby4(terms);

pi=pidiv4*=4;

printf("The term you entered is %d\n", terms);

printf("The pi value is %0.6f\n", pi);

Bases on the user input for # of terms That I'm returning to int main() piby4(1.0/terms+terms-1) ...until C decrements terms to 1....but I'm not exactly sure how a recursive function works.... I need help in putting it together basically.

First tell me what do You mean by terms

1,2,3,4, etc

or

1,3,5, etc

2.

If Your intension was to stop here You shouldn't call piby4

if (terms==1)

return(piby4(1.0/terms+ter

3.What argument You want to pass to piby4

for example

double fun(int k)

{

if(k==1)

return 13.25;

else

return 54.321;

}

You passing integer and returning double.

{

if (terms==1)

return(piby4(1.0/terms+ter

else if (terms %2==0)

return(piby4(-1.0/terms+te

else if (terms %2)

return(piby4(+1.0/terms+te

else

return (0);

}

--------------------------

Your argument in this call

piby4(1.0/terms+terms-1)

is

1.0/terms+terms-1

and it float

Is the basic setup of my function correct?

{

if (terms==1)

return(piby4(1.0/terms+ter

else if (terms %2==0)

return(piby4(-1.0/terms+te

else if (terms %2)

return(piby4(+1.0/terms+te

else // it is not necesarry

return (0); // it is not necesarry

}

If You guess what is something it is done and You understand.

Look at the basic formula

for example

piby4(5) should be equivalent to 1/1-1/3+1/5-1/7+1/9 right?

and

piby4(4) should be equivalent to 1/1-1/3+1/5-1/7 right?

so

piby(5) = piby(4) + what

piby4(5) should be equivalent to 1/1-1/3+1/5-1/7+1/9 right?

and

piby4(4) should be equivalent to 1/1-1/3+1/5-1/7 right?

so

piby4(5) = piby4(4) +1/9 right?

else if (terms %2)

return(piby4(+1.0/terms+te

so return(piby4(terms-1)+1/9?

Please write me the denominator of the first formula on the page

You use this formula by recursion and this expression about we are talking so long is

1./(2*term-1).

Please we must quickly come to an end.

It's getting here very very late.

I'm not sure what I'm suppose to return?

do I put the formula in int main or the function?

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.

{

if (terms==1)

// return(piby4(1.0/terms+ter

return 1.;

else if (terms %2==0)

// return(piby4(-1.0/terms+te

return( piby4(terms-1) + 1./(2*terms-1) );

else if (terms %2)

// return(piby4(+1.0/terms+te

return( piby4(terms-1) - 1./(2*terms-1) );

// else // it is not necesarry

// return (0); // it is not necesarry

}