Previous Next
0 / 7
Module DEMO-32-58DEMO-C-32

Sum of Natural Numbers

DEMO-C-32 • Programming in C

Browser-only practice

Problem Statement

Write a C program that calculates the sum of all natural numbers from 1 to n, where n is a positive integer provided by the user.

For example, if n = 5, the sum would be 1 + 2 + 3 + 4 + 5 = 15.

Use a loop to solve this problem.

Constraints

1 ≤ n ≤ 1000

Input Format

The input consists of a single positive integer n (1 ≤ n ≤ 1000).

Output Format

Output a single integer representing the sum of all natural numbers from 1 to n.

Sample Testcases

Submit runs every public testcase in this browser. Results and code never leave this device.

Sample #1
Public sample
5
15
1 + 2 + 3 + 4 + 5 = 15
Sample #2
Public sample
1
1
Only one number to sum: 1
Sample #3
Public sample
100
5050
Sum of first 100 natural numbers
Sample #4
Public sample
7
28
1 + 2 + 3 + 4 + 5 + 6 + 7 = 28

Web terminal

C, C++, Java, and Python run locally in a browser VM. No worker or visualizer is used.

Saved in this browser
Editor settings