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

  1. Step 1: Start.
  2. Step 2: Set sum = 0.
  3. Step 3: Read n.
  4. Step 4: Set i = 1.
  5. Step 5: If i <= n, go to step 6; otherwise go to step 8.
  6. Step 6: Set sum = sum + i.
  7. Step 7: Set i = i + 1. Go to step 5.
  8. Step 8: Print sum.
  9. 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)

StepLineBoxsumniOutput
13Start
24sum = 00
36Read n05Enter n:
47i = 1051
57i <= n?051
68sum = sum + i151
77i = i + 1152
87i <= n?152
98sum = sum + i352
107i = i + 1353
117i <= n?353
128sum = sum + i653
137i = i + 1654
147i <= n?654
158sum = sum + i1054
167i = i + 11055
177i <= n?1055
188sum = sum + i1555
197i = i + 11556
207i <= n?1556

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.

More programs

← All programs

Try AI Flowchart free →