0

Write a program that prints the sum of the first five positive integers ,1+2+.....+5.

26th Feb 2017, 5:26 PM
Zeyad Sharo
Zeyad Sharo - avatar
4 Answers
+ 2
#include <iostream> using std::cout; int main() { int sum = 0; int A1 = 1; int An = 5; for(int i = A1; i <= An; ++i) sum += i; cout << sum; }
26th Feb 2017, 5:33 PM
SUPER_S
SUPER_S - avatar
+ 1
Or ( Sn = (A1 + An) / 2 * n) ) — sum of algebraic progression #include <iostream> using std::cout; int main() { int A1 = 1; int An = 5; int n = 5; int sum = float(A1 + An) / 2 * n; cout << sum; }
26th Feb 2017, 5:46 PM
SUPER_S
SUPER_S - avatar
0
thank you for answering
26th Feb 2017, 5:47 PM
Zeyad Sharo
Zeyad Sharo - avatar
0
Why not use Gaussian summation formula in the form it is taught in school? (n(n+1))/2 #include <iostream> using namespace std; int gauss(int); int main(){ int n; cin >> n; cout << gauss(n) << endl; } int gauss(int m){ return (m * (m + 1)) / 2; }
27th Feb 2017, 12:18 AM
Huegel
Huegel - avatar