#include <iostream> using namespace std; int main() { int n, sum = 0; cout << "输入一个正整数: "; cin >> n; for (int i = 1; i <= n; ++i) { sum += i; } cout << "Sum = " << sum; return 0; }
运行结果:
输入一个正整数: 3 Sum = 6