Challenge: Sum of 1 to N
Completed
Read a whole number n and print the sum 1 + 2 + ... + n.
Input
One integer n (1 ≤ n ≤ 10000).
Output
The sum.
Example
Input
5
Output
15
Editor
C
Hints
Start total at 0 before the loop.
Loop i from 1 to n and add i each time.
Solution explanation
The loop visits every number from 1 to n and adds it to the running total.
Show solution
#include <stdio.h>
int main() {
int n;
scanf("%d", &n);
int total = 0;
for (int i = 1; i <= n; i++) {
total += i;
}
printf("%d\n", total);
return 0;
} Output
Answer
You can practice without logging in. Log in to save completion.
Save your progress
You're practising as a guest, so your progress won't be saved.
- Track every lab you complete
- Earn verifiable lab certificates
- See your progress in every module