HI everyone,

I need help in c++. Problem : I want to write a program that calculate x digits of pi.

Like
if my input is 3 output should like this 3,14 or this 3,145
if my input is 10 output should like this 3,14529xxx or lik this 3,14529xxxx.

I write my program but i dont what formula i have to use.. i alot of search but cant find or understand..

#include <iostream>
using namespace std;
int main(){
 int i,j,pi,;//declear variable

/*

??????????
i have no idee..
*/


return 0;
}

Lets assume for example PI = 180 / 57. Division essentially is nothing more than successive subtractions, so based on that premis, one could construct a loop as follows

for (Cnt = 0; Cnt < Presision; Cnt++ ) {
  Result = 0;
  while (Value > Divisor)
    { Value -= Divisor; Result++ }
  // This is where you'd convert Result into a character
  Value *= 10;
  }

So 180 / 57 = 3.1578947368421052631578947368421.

180 - 57 = 123 (1)
123 - 57 = 66 (2)
66 - 57 = 9 (3) > Result = 3

9 x 10 = 90

90 - 57 = 33 (1) > Result = 1

33 x 10 = 330

330 - 57 = 273 (1)
273 - 57 = 216 (2)
216 - 57 = 159 (3)
159 - 57 = 102 (4)
102 - 57 = 45 (5) > Result = 5

So you can see how this mentod is beginning to correspond to calculation above using windows calc function.

Dependant upon the level of accuracy you want you may need to begin with

1800000000000000000
572957795130823208

because 180 / PI = 57.295779513082320876798154814105

Comments
This is really awesome; I will give this a go :)
Nice :)

I'm not sure how else to explain it, but I'm sure if you implement the snippet into a functioning application and then trace through it with a debugger, you will begin to see how it works.

This article has been dead for over six months. Start a new discussion instead.