You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

26 lines
516 B
C

#include <stdio.h>
#include <stdbool.h>
double approximate_pi(unsigned int steps) {
double approximate_pi = 4.0;
bool subtract = true;
for (int i = 2; i <= steps; i++) {
if (subtract) {
approximate_pi -= (4.0 / (i * 2 - 1));
} else {
approximate_pi += (4.0 / (i * 2 - 1));
}
subtract = !subtract;
}
return approximate_pi;
}
int main(int argc, char *argv[]) {
// printf("sizeof(int) = %d\n", sizeof(int));
printf("pi = %8.7f\n", approximate_pi(10000000));
return 0;
}