Current location - Plastic Surgery and Aesthetics Network - Plastic surgery and medical aesthetics - Is there a problem in finding the value of pi with C++?
Is there a problem in finding the value of pi with C++?
First off the topic, your statement is wrong. ...

The square of (pi)/6 =1+1/2 * 2+1/3 * 3+1/4 * 4 ...+1/n * n.

it should be

The square of (pi)/6 =1+1(2 * 2)+1(3 * 3)+1/(4 * 4) ...+1(n * n

Get to the point:

Of course there is a problem, pay attention to type conversion! !

# include & ltiostream.h & gt

# include & ltmath.h & gt

void main()

{

int i,n;

float sum = 0; //An initial value must be assigned here, long means long integer, and floating-point type must be used here.

Floating point pi;

Cout & lt& lt "Please enter n = ";;

CIN & gt; & gtn;

for(I = 1; I < = n; i++)

{

sum = sum+ 1/(float)(I * I); //Force type conversion, otherwise 1/(i*i) will always be 0.

}

pi = sqrt(6 * sum);

cout & lt& ltpi & lt& ltendl

}