C: Sum from 1 to n

Read n and print 1 + 2 + ... + n.

What to build

A loop that builds up a total.

Requirements

  • Read n.
  • Add up the numbers from 1 to n.

Input

One whole number n, with 1 ≤ n ≤ 100000.

Output

The sum.

Constraints

n can be up to 100000, so the sum needs a long long.

Example

Input:

5

Output:

15

Why it works

The sum of the numbers up to 100000 is about five billion, which does not fit in an int.

Examples

Example 1 · Input
5
Expected output
15
Example 2 · Input
1
Expected output
1

Hints

Hint 1

Start a total at 0 and add i in a for loop from 1 to n.

Hint 2

For n up to 100000 the sum is bigger than an int can hold: use long long.

Solution

Show one correct solution (try it yourself first)
#include <stdio.h>

int main(void) {
    int n;
    scanf("%d", &n);
    long long sum = 0;
    for (int i = 1; i <= n; i++) sum += i;
    printf("%lld\n", sum);
    return 0;
}

Lesson: Loops: for, while and do-while →