0
Write a program that prints the sum of the first five positive integers ,1+2+.....+5.
4 odpowiedzi
+ 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;
}
+ 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;
}
0
thank you for answering
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;
}