C Program to Find Sum of Natural Numbers
C Program to Find Sum of Natural Numbers
A classroom arithmetic challenge needs a result built from a sequence, not a formula copied without understanding its starting value.
Add each integer from one through n, with zero as the starting sum.
Before this exercise, review repeating work with loops. The worked program below applies those ideas to this specific problem.
Explain
Explain why sum of natural numbers uses this sequence of operations.
Trace
Trace the sample values and predict the printed result.
Apply
Use 6 and explain how the initial accumulator affects the answer.
Sum of Natural Numbers: follow the data
Given data
A classroom arithmetic challenge needs a result built from a sequence, not a formula copied without understanding its starting value.
Rule
Add each integer from one through n, with zero as the starting sum.
Observed result
Sum of Natural Numbers: 15
Understand the operation
The accumulator changes 0 → 1 → 3 → 6 → 10 → 15. Equality in the loop test includes n.
An accumulator stores the result after each completed iteration. Choose its initial value to match the operation: zero for addition, one for multiplication. The loop must eventually stop before the counter or result exceeds its representation.
Read the complete C program
The input values are written into this complete C17 example so the calculation can be reproduced without an unavailable service. Change the named data, rebuild and compare the actual output with your prediction.
C17 · complete program
#include <stdio.h>
int main(void) {
int n = 5;
unsigned long long result = 0;
for (int i = 1; i <= n; i++) result += i;
printf("Sum of Natural Numbers: %llu\n", result);
return 0;
}
Expected output
Sum of Natural Numbers: 15
Run in C compiler →Follow the values through the code
C17 · code used in this explanation
#include <stdio.h>
int main(void) {
int n = 5;
unsigned long long result = 0;
for (int i = 1; i <= n; i++) result += i;
printf("Sum of Natural Numbers: %llu\n", result);
return 0;
}
The chosen n is 5.
The accumulator changes 0 → 1 → 3 → 6 → 10 → 15. Equality in the loop test includes n.
Follow the worked calculation
Start with the sample
The chosen n is 5.
Apply the rule
The accumulator changes 0 → 1 → 3 → 6 → 10 → 15. Equality in the loop test includes n.
Check the result
Sum of Natural Numbers: 15
Sum of Natural Numbers: 15
Try a small variation
C17 · code used in this explanation
#include <stdio.h>
int main(void) {
int n = 5;
unsigned long long result = 0;
for (int i = 1; i <= n; i++) result += i;
printf("Sum of Natural Numbers: %llu\n", result);
return 0;
}
Use 6 and explain how the initial accumulator affects the answer.
Worked answer
The changed loop produces 21. With zero iterations, the initialized accumulator is the result; this is why multiplying examples must not start from zero.
C17 · complete program
#include <stdio.h>
int main(void) {
int n = 6;
unsigned long long result = 0;
for (int i = 1; i <= n; i++) result += i;
printf("Sum of Natural Numbers: %llu\n", result);
return 0;
}
Expected output
Sum of Natural Numbers: 21
Run in C compiler →Recognize the important boundaries
For arbitrary n, both repeated addition and n*(n+1)/2 can overflow; rearranging a formula does not remove the range requirement.
The example uses a deliberately small nonnegative input. A negative input needs an explicit rejection or a separately defined interpretation.
Check your understanding
Choose the answer, then compare the reasoning for every option.
Use the idea independently
Add each integer from one through n, with zero as the starting sum.
Continue with loop initialization and termination.
Continue with recursive alternatives.
Language reference: WG14 C11 committee draft. These examples use the C17-compatible subset of the explained language features.
Reaching the recap and completing the quizzes are separate milestones.