Sum of First n Natural Numbers: flowchart, algorithm and program
This program reads a number n and prints 1 + 2 + ... + n. It starts with sum = 0 and adds each number from 1 to n into sum, one at a time, using a for loop.
▶ Watch the dry run step by step →
Algorithm
- Step 1: Start.
- Step 2: Set sum = 0.
- Step 3: Read n.
- Step 4: Set i = 1.
- Step 5: If i <= n, go to step 6; otherwise go to step 8.
- Step 6: Set sum = sum + i.
- Step 7: Set i = i + 1. Go to step 5.
- Step 8: Print sum.
- Step 9: Stop.
Pseudocode
BEGIN
sum ← 0
READ n
i ← 1
WHILE i <= n DO
sum ← sum + i
i ← i + 1
END WHILE
PRINT sum
END
Program in C, Python and Java
Sum of First n Natural Numbers program in C
#include <stdio.h>
int main() {
int n, i, sum = 0;
printf("Enter n: ");
scanf("%d", &n);
for (i = 1; i <= n; i++) {
sum = sum + i;
}
printf("Sum = %d\n", sum);
return 0;
}Sum of First n Natural Numbers program in Python
n = int(input("Enter n: "))
sum = 0
for i in range(1, n + 1):
sum = sum + i
print("Sum =", sum)Sum of First n Natural Numbers program in Java
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n, i, sum = 0;
System.out.print("Enter n: ");
n = sc.nextInt();
for (i = 1; i <= n; i++) {
sum = sum + i;
}
System.out.println("Sum = " + sum);
}
}
Sample input: n = 5
Output: Sum = 15
How it works
- sum starts at 0, so adding numbers to it does not change the answer by mistake.
- The loop variable i begins at 1 and the loop keeps running while i <= n, so n itself is included.
- Each time through the loop, sum = sum + i adds the current i to the running total.
- After i++ the loop checks the condition again. When i becomes n + 1, the loop stops.
- For n = 5 the sum grows 1, 3, 6, 10, 15, so the program prints Sum = 15.
Dry run (trace table)
| Step | Line | Box | sum | n | i | Output |
|---|---|---|---|---|---|---|
| 1 | 3 | Start | ||||
| 2 | 4 | sum = 0 | 0 | |||
| 3 | 6 | Read n | 0 | 5 | Enter n: | |
| 4 | 7 | i = 1 | 0 | 5 | 1 | |
| 5 | 7 | i <= n? | 0 | 5 | 1 | |
| 6 | 8 | sum = sum + i | 1 | 5 | 1 | |
| 7 | 7 | i = i + 1 | 1 | 5 | 2 | |
| 8 | 7 | i <= n? | 1 | 5 | 2 | |
| 9 | 8 | sum = sum + i | 3 | 5 | 2 | |
| 10 | 7 | i = i + 1 | 3 | 5 | 3 | |
| 11 | 7 | i <= n? | 3 | 5 | 3 | |
| 12 | 8 | sum = sum + i | 6 | 5 | 3 | |
| 13 | 7 | i = i + 1 | 6 | 5 | 4 | |
| 14 | 7 | i <= n? | 6 | 5 | 4 | |
| 15 | 8 | sum = sum + i | 10 | 5 | 4 | |
| 16 | 7 | i = i + 1 | 10 | 5 | 5 | |
| 17 | 7 | i <= n? | 10 | 5 | 5 | |
| 18 | 8 | sum = sum + i | 15 | 5 | 5 | |
| 19 | 7 | i = i + 1 | 15 | 5 | 6 | |
| 20 | 7 | i <= n? | 15 | 5 | 6 |
Time and space complexity
Time: O(n) · Space: O(1). The loop runs n times doing constant work each time, using only a few variables.
Common mistakes
- Not setting sum = 0 at the start. The variable then holds a garbage value and the answer comes out wrong.
- Writing i < n instead of i <= n, which leaves out the last number n.
- Starting the loop at i = 0 or forgetting i++, which gives a wrong result or an infinite loop.
- Using int for a very large n. The sum can overflow, so use long long for big values.
Viva questions
Q1. Why is sum initialised to 0 and not 1?
0 is the identity for addition, so the total starts empty. If sum started at 1, every result would be one too large.
Q2. Explain what the condition i <= n in line 7 checks.
It checks whether the current number is still within the range 1 to n. The loop keeps running while it is true, and since it uses <=, n itself is added.
Q3. What does the program print when n is 0, and why?
It prints Sum = 0. The loop starts with i = 1, so 1 <= 0 is false, the body never runs and sum stays at its initial value 0.
Q4. What happens if the user enters a negative number such as -5?
The output is Sum = 0, because 1 <= -5 is false and the loop never executes. The program does not report an error, so you could add an input check if you want one.
Q5. Why do we write &n in scanf but just sum in printf?
scanf needs the address of n so it can store the typed value into that variable. printf only needs the value of sum to display it.
Q6. What is the time complexity of this program, and is there a faster way?
It is O(n), because the loop runs n times with one addition each. The formula n*(n+1)/2 gives the same answer in O(1) time.
Q7. What would change if you wanted the sum of only the even numbers up to n?
You could start i at 2 and use i += 2, or keep the loop and add i only when i % 2 == 0. The sum would then cover 2, 4, 6 and so on up to n.
Q8. What can go wrong if n is very large, for example 100000?
The true sum, about 5 billion, exceeds the range of a 32-bit int (about 2.1 billion), so it overflows and gives a wrong answer. Declaring sum as long long fixes this.
Frequently asked questions
What is the sum of the first n natural numbers?
It is 1 + 2 + ... + n. For n = 5 it is 15. The loop computes it by adding one number at a time.