Learn C++ from a baby! Record the questions in the process of Luogu C++ learning and test preparation, and record every moment.
Attached is a summary post: Luogu Brush Questions C++ Language | Summary
【Description】
Calculate the value of 1+2+3+⋯+( n −1)+ n , where the positive integer n is not greater than 100. Since you are not as smart as Gauss, you are not allowed to use the arithmetic sequence summation formula to find the answer directly.
【enter】
Enter a positive integer n .
【Output】
Output a positive integer representing the answer to the final summation.
【Input sample】
100
【Example of output】
5050
【Code Explanation】
#include <bits/stdc++.h>
using namespace std;
int main()
{
int n, sum=0;
cin >> n;
for (int i=1; i<=n; i++) {
sum += i;
}
cout << sum;
return 0;
}
【operation result】
100
5050