Find PI recursively using the formula:

The exercise asks for 1000 multiples. Here we ask the user to enter how many multiples he wants.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | #include <stdio.h> #include <math.h> double f(int n){ if(n==1) return sqrt(2); else return sqrt(2+f(n-1)); } double product(int n){ if(n==1) return 2; else return 2/f(n-1)*product(n-1); } int main(){ double prod=1; int i,multiples; printf("How many multiples: "); scanf("%d",&multiples); printf("pi= %.15lf",product(multiples)); return 0; } |
