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;
}
``````

## All 9 Replies

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

commented: Nice :) +15
commented: This is really awesome; I will give this a go :) +0

if i try with 27/7 ?

i dont understand...

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.

which value has Presision ?

precision is the value of the decimals desired by the user ..as input

aaa ok ty

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.