loading
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <iostream>

float pi;
int m_terms;
float denominator;
int i = 0;
float sum1 = 0; 

int main (){
  //Type in a number
  std::cout << "Type in a number for how many terms you want to compute pi : " << std::endl;
  std::cin >> m_terms;
  
  
  // Approximate pi based on sum1 with help of a loop
      while(i<=m_terms){
        //set denominator variable 
        denominator = i;
        //consider the negative terms
        if(i%2==1){
          pi += (pi-1)/(2*denominator+1);}
        //consider the positive terms  
        else{
          pi += (pi+1)/(2*denominator+1);}
          ++i;
        }
        sum1=pi*4;
  
  std::cout << "pi equals: " << sum1 << std::endl;

  return 0;
}
Fetching token