Need a project done?

C++ Programming Developer

Search This Blog

Pi Recursion C++

The value of π can be determined by the series equation
π=4∗(1-1/3+1/5−1/7+1/9−1/11+1/13…)
Write a program that prompts the user to enter a positive odd number n and uses recursion to approximate the value of π using the given formula including term up through 1/n.
Solution:


#include <iostream>
#include <cmath>
using namespace std;

double pi (double);

int main()
{
cout << "Enter n to value of pi: "; int n; cin>>n;
cout << pi(n) << endl;

return 0;
}


double pi (double n)
{
if (n==1)
return 4*1;
else
return ( 4*(pow(-1,n+1)*(1/(2*n-1))) + pi(n-1) );
}
"Don't let anyone ever make you feel like you don't deserve what you want."