C++ examples for Operator:Arithmetic Operator
Calculate the value of PI from the infinite series,
formula 4 4 4 4 4 PI = 4 - --- + --- - --- + --- - ---- + 3 5 7 9 11
Print a table that shows the approximate value of PI after each of the first 1000 terms of this series.
#include <iomanip> #include <iostream> int main(int argc, const char *argv[]) { int toggle = 0; int limit = 1000; double pi = 4.0f; double divisor = 3.0f; std::cout << "Term" << std::setw(4) << "\tPI Approx" << std::endl; for (int i = 1; i <= limit; i++) { if (toggle == 0) pi -= (4.0f / divisor);/*from www. j a va2s . c o m*/ else pi += (4.0f / divisor); // bit toggle for + - switching above toggle = (1 - toggle); divisor += 2; std::cout << i << std::setw(4) << "\t" << pi << std::endl; } return 0; }