Official

A - Triangular Number Editorial by en_translator


One can use a for statement to find the sum from \(1\) to \(N\).
Alternatively, one can use the formula \(1+2+\cdots+N=\frac{N(N+1)}{2}\) to find the answer.

Sample code in C++:

#include <bits/stdc++.h>
using namespace std;

int main() {
	int n, ans=0;
	cin >> n;
	for(int i=1;i<=n;i++){
		ans+=i;
	}
	cout << ans << endl;
	return 0;
}

Sample code in Python:

n=int(input())
ans=0

for i in range(1,n+1):
    ans+=i

print(ans)

posted:
last update: