So I have to find approximate value of pi using a formula recursively but when I pass a value into function it returns 4 as answer.
Formula: π = 4 ∗ (1−1/3+1/5−1/7+1 /9−1/11+1/13+...) for odd value
My code:
#include <iostream>
#include <cmath>
using namespace std;
float PiValue(int n){
if (n == 0)
return 4*1;
else {
return (4*(pow(-1,n)*(1/(2*n+1)))) + PiValue(n-1);
}
}
int main(){
int odd;
float ans;
while(odd % 2 == 0){
cout << "Enter odd number: ";
cin >> odd;
if (odd % 2 == 0)
cout << "**It is not a odd number**" << endl;
}
ans = PiValue(odd);
cout << ans;
return 0;
}
If I pass 1875, I should be getting 3.14052655581 as my answer.