-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlab1_q5
More file actions
20 lines (20 loc) · 872 Bytes
/
lab1_q5
File metadata and controls
20 lines (20 loc) · 872 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <stdio.h>
#include <stdlib.h>
/* Calculate the value of from the infinite series PI = 4-(4/3)+(4/5)-(4/7)+......
Write a program which reads in a positive integer n(i.e., n>=1)and calculates the value of by adding up the first n terms of the above series */
int main()
{ /* This program will calculate and print the value of
pi up to the first n terms using an infinite series */
int n,i=0;
float x=0,sum1=0,sign=-1;
printf("Please input a positive integer, greater than or equal to 1 ");
scanf("%d",&n);
do{ //A do while loop is used here to to calculate the sum of pi.
i++;
sign = sign*-1;
sum1 = sum1 +(4/(2*x+1))*sign; // an infinite series is used here, to calculate the sum of pi
x ++;
}while(i<n); // runs while i is less that n (inputted intger)
printf("%f",sum1);
return 0;
}