**Write a Modular C Programming code to solve CALCULATING π hacker rank, So why calculate π? Because it’s easy and computers are good for exhaustive calculations.**

In 1966 Donald G. Fink published a book entitled “Computers and the Human Mind”, which introduced the idea of artificial intelligence as it related to computers. One of the first problems he introduced to illustrate the utility of computers was the calculation of π. He posed the question as to why compute π to 100,000 decimal places when 30 decimal places suffice to calculate the circumference of the known universe? This estimate of 30 decimal places was made by astronomer Simon Newcomb (1835-1909). In 2006, Arndt & Haenel estimated that “39 digits of π are sufficient to calculate the volume of the universe to the nearest atom”.

So why calculate π? Because it’s easy and computers are good for exhaustive calculations.

Fink used a series of the following form:

π = 4/1 – 4/3 + 4/5 – 4/7 + 4/9 – 4/11 + 4/13…

Then goes on the say that using this formula to derive 100,000 decimal places of π would require 10to power of 100,000 terms. So many in fact that “shortcuts must be used even with a fast computer”. It was 1966, and computers were slow, and used punch cards!

He postulates that a speed-up comes by way of simple averaging.

Here’s a summary:

• sum of the first 98 terms = 3.131388 (too low)

• sum of the first 99 terms = 3.151693 (too high)

• sum of the first 100 terms = 3.131593 (too low)

• average of 98 and 99 term sums = 3.141541 (still too low)

• average of the 99 and 100 terms = 3.141643 (still too high)

• average of the two averages = 3.141592 (nearby value of π)

TASK:

Based on N terms, determine the average of the two averages i.e. ACCURATE VALUE OF π.

**Input Format**

N indicates the number of terms for series.

**Constraints**

100 <= N <=1000

For N = 150, the sum of the first 148 terms, sum of the first 149 terms, sum of the first 150 terms, and so on…

**Output Format**

Calculated accurate value of π.

**Sample Input 0**

```
100
```

**Sample Output 0**

```
Pi value = 3.141592
```

**Sample Input 1**

```
10
```

**Sample Output 1**

`Not permitted to calculate`

**Refer :** C Programming HackerRank all solutions for Loops | Arrays | strings

**CODE:**

#include<stdio.h> #include<stdlib.h> #include<string.h> #include<math.h> void calculate(int n); float pi_value_single(int n); int main() { int n; scanf("%d",&n); if(n>=100 && n<=1000) { calculate(n); } else { printf("Not permitted to calculate"); } } void calculate(int n) { float pi_1,pi_2,pi_0,pi_avg_1,pi_avg_2,pi; int n1,n2; n1=n-1; n2=n-2; pi_0=pi_value_single(n); pi_1=pi_value_single(n1); pi_2=pi_value_single(n2); pi_avg_1=(pi_1+pi_2)/2; pi_avg_2=(pi_0+pi_1)/2; pi=(pi_avg_1+pi_avg_2)/2; printf("Pi value = %lf",pi); } float pi_value_single(int n) { float i,j=1; float sum=0; for(i=1;i<=n;i++) { sum=sum+((4/j)*pow(-1,i-1)); j=j+2; } return sum; }

**OUTPUT**

Congratulations, you passed the sample test case. Click the Submit Code button to run your code against all the test cases. Input (stdin) 100 Your Output (stdout) Pi value = 3.141592 Expected Output Pi value = 3.141592

**Please find some more codes of Loops, Condition Statements, 1D Arrays, 2D Arrays, Strings, Pointers, Data Structures, Files, Linked lists, MISC, Solved model question papers & Hacker Rank all solutions on the below page:**

Top 100+ C Programming codes – KLE Technological University