Advertisement
Guest User

Untitled

a guest
Jul 21st, 2017
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.40 KB | None | 0 0
  1. #include <iostream>
  2. #include <iomanip>
  3. #include <string>
  4.  
  5. using namespace std;
  6.  
  7. void input(string& x, int& y, int& z);
  8.  
  9. int main()
  10. {
  11.     int denom = 3;
  12.     int terms=0;
  13.     int steps=0;
  14.  
  15.     double newPi = 4.0;
  16.  
  17.     bool condition = true;
  18.  
  19.     string prompt= "Enter the number of terms to use: ";
  20.  
  21.     input( prompt, terms, steps );
  22.  
  23.     cout << "Results:";
  24.     cout << endl << endl;
  25.  
  26.  
  27.     for ( int i = 1; i <= terms; i++ )
  28.     {
  29.         if (condition)
  30.         {
  31.             newPi -= ( 4.0 / denom );
  32.         }
  33.         else
  34.         {
  35.             newPi += ( 4.0 / denom );
  36.         }
  37.         condition = !condition;
  38.         denom += 2;
  39.  
  40.         if ((i % steps) ==0)
  41.         {
  42.             cout << fixed << showpoint << setprecision(9);
  43.             cout << newPi << endl;
  44.         }
  45.     }
  46.  
  47.     cout << endl << endl;
  48.     system ("PAUSE");
  49.     return 0;
  50. }
  51.  
  52. void input(string& x, int& y, int& z)
  53. {
  54.     do
  55.     {
  56.  
  57.         //string x = "Enter the number of terms to use: ";
  58.  
  59.         cout << x;
  60.         cin >> y;
  61.         cout << endl;
  62.  
  63.         if ( y <= 0 )
  64.  
  65.         cout << "Error -- invalid input" << endl << endl;
  66.  
  67.     } while ( y <= 0 );
  68.  
  69.     do
  70.     {
  71.         cout << "Display Pi after every how many steps? ";
  72.         cin >> z;
  73.         cout << endl;
  74.  
  75.         if ( z <= 0 )
  76.  
  77.         cout << "Error -- invalid input" << endl << endl;
  78.  
  79.     }while ( z <= 0);
  80. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement