Skip to content

Instantly share code, notes, and snippets.

@gcasa
Created September 28, 2022 17:17
Show Gist options
  • Save gcasa/d0d961aa2e57f6b0bd462f8202721f8d to your computer and use it in GitHub Desktop.
Save gcasa/d0d961aa2e57f6b0bd462f8202721f8d to your computer and use it in GitHub Desktop.
Quick fun with Copilot, calculate pi!
#include <stdio.h>
#include <math.h>
// a function to calculate the value of pi
double calculate_pi(int n) {
double pi = 0.0;
int i;
for (i = 0; i < n; i++) {
pi += pow(-1, i) / (2 * i + 1);
}
return 4 * pi;
}
int main() {
int n;
printf("Enter the number of terms: ");
scanf("%d", &n);
printf("The value of pi is %f\n", calculate_pi(n));
return 0;
}
#include <math.h>
#include <stdio.h>
int main(void)
{
double pi = 0.0;
double denom = 1.0;
double sign = 1.0;
int i;
for (i = 0; i < 1000000; i++) {
pi += sign / denom;
denom += 2.0;
sign = -sign;
}
pi *= 4.0;
printf("pi = %f\n", pi);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment