0% found this document useful (0 votes)
32 views1 page

#Include #Include

This C program calculates an approximation of pi using the Leibniz formula. It takes user input for the number of terms, uses a for loop to calculate each term by alternating signs and decreasing denominators, and adds them to a running total pi which is printed at the end.

Uploaded by

KS Chong
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
32 views1 page

#Include #Include

This C program calculates an approximation of pi using the Leibniz formula. It takes user input for the number of terms, uses a for loop to calculate each term by alternating signs and decreasing denominators, and adds them to a running total pi which is printed at the end.

Uploaded by

KS Chong
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 1

#include<stdio.

h>
#include <math.h>

int main(void){
int a=1,n,b=0;
double pi=0;
double s=0.0;
scanf("%d",&n);

for(a=1;a<=n;a = a+1){ //a++


b=a+1;
s=pow(-1,b)*4.0/(1+2*(a-1));
pi=pi+s;
}
printf("%f",pi);

return 0;
}

You might also like