I have been working on this lab for a couple of days. I need to write a program (using a 'for loop') that approximates pi using the following series:

pi = sqrt(6* (1/(1)2 + 1/(2)2 + 1/(3)2+ 1/(4)2 + 1/(5)2 +...... + 1/(n)2)) where n is the number of terms. Make the interger datatypes = long, and the floating point datatypes to long double.

This is the code I have written so far. I don't know what i'm missing but the results I'm getting is not correct.

#include <iostream>
#include <iomanip>
#include <cmath>
 
using namespace std;
 
int main()
{
    long int terms, count;     
    long double pi;
    double sqrt; 
           
    
      cout << "Enter the number of terms to use:   " << endl;
       cin >> terms; 
   
    
       for (count =1; count <= terms; count++);
       {          
       pi = (1.0 /(terms * terms));                                   
       }
       pi = sqrt * pi;
       pi = pi * 6;
       
                   
     cout <<setprecision (15)<< "The approximate value of pi is:   " << pi << endl;
   
system ("pause");

return 0;
} // end main
for (count =1; count <= terms; count++);

This pretty makes your loop useless, you don't want that semi-colon.

You also need to check the logic behind the steps you take to calculate pi. E.g. I don't see any square root function used.

thank you. The codes I have has the sqrt * pi listed.
My understanding of this problem is that:
using the series given, it would be (1 / N^2), which I wrote as pi = (1.0/(terms * terms);
then sqrt * pi; and eventually, pi * 6.


Thanks.

pi = sqrt(6* (1/(1)2 + 1/(2)2 + 1/(3)2+ 1/(4)2 + 1/(5)2 +...... + 1/(n)2))

Ok, so looking at this equation you have the series of summing 1/(n)2 . This is not equivalent to the sum of 1/(n^2).

I did notice you are using sqrt in your code, but 2 points. 1, surely sqrt means square root and not multiplication (the sqrt is also undefined in your code). 2, the sqrt is located outside of the parenthesis indicating it should be the last operation (you currently have *6 as the final operation).

Hope this helps.

I appologise about the ^2, you are correct here. I assume those "2"s are supposed to be in superscript :). Anyway I felt nice so:

#include <iostream>
#include <iomanip>
#include <cmath>
 
using namespace std;
 
int main() {
    long int terms, count;     
    long double pi = 0;
    
    cout << "Enter the number of terms to use:   " << endl;
    cin >> terms;
    
    for (count=1; count<=terms; count++) {
        pi += (1.0 / (count * count));
    }
    pi = pi * 6;
    pi = sqrt(pi);

    cout << "The approximate value of pi is:   " << pi << endl;

    return 0;
} // end main

You will probably want to put you system pause back in, but that should do what you want.

A BIG thank you.

I tried the codes so many different ways. I had several people who were nice enough to help, but to no avail.... You are the first one that pointed out about the "sqrt" being outside the parenthesis meant that it should be the last operation.

I am very knew to C++, (on my 9th day of class); and I really appreciate the fact that you didn't make me feel "stupid" for not knowing what should be done. This kind of attitude makes a huge difference to those of us that really wants to learn.....

Thanks again.
:)

hi my friend i have more problems to find the value of pi!!! can you help me???

commented: This post is approximately 18 months too late -6

hi my friend i have more problems to find the value of pi!!! can you help me???

It's about 3,14159265358979323846 :icon_wink:

commented: :P +4
Be a part of the DaniWeb community

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