L3_T2
unknown
c_cpp
2 years ago
992 B
4
Indexable
#include <iostream> #include <cmath> using namespace std; double ForProduct(int n); double WhileProduct(int n); double DoProduct(int n); int main() { int n; while(true) { cout << "enter n: "<< endl; cin >> n; if(n > 0 ) break; cout << "\nIncorrect input values!"; } cout << "Result with for: " << ForProduct(n) << endl << "Result with while: " << WhileProduct(n) << endl << "Result with do-while: " << DoProduct(n) << endl; return 0; } double ForProduct(int n) { double sum = 0; for(int i = 1; i <= n; i++) { sum += 1.0 / (i * (i + 1)); } return sum; } double WhileProduct(int n) { double sum = 0; int i = 1; while(i <= n) { sum += 1.0 / (i * (i + 1)); i++; } return sum; } double DoProduct(int n) { double sum = 0; int i = 1; do { sum += 1.0 / (i * (i + 1)); i++; } while(i <= n); return sum; }
Editor is loading...