Sum of N Numbers Flowchart
Flowchart to find sum of n numbers: set sum = 0 and i = 1, add i to sum while i <= n, then print sum. With a trace table for n = 5, pseudocode, tested Python, C and Java code, and the n(n+1)/2 shortcut.
Flowchart to find sum of n numbers: set sum = 0 and i = 1, add i to sum while i <= n, then print sum. With a trace table for n = 5, pseudocode, tested Python, C and Java code, and the n(n+1)/2 shortcut.
Study guide
A flowchart to find sum of n numbers uses an accumulator: a variable sum that starts at 0 and grows by one number on every pass of a loop. A counter i walks from 1 to n, and the loop stops when i passes n.
This running total pattern is everywhere: averages, scores, shopping baskets. Learn it once here.
n.n numbers are in.sum holds 1 + 2 + ... + n.Follow the flowchart with n = 5. Each row is one visit to the i <= n? diamond.
| i | i <= n? | sum before | sum after | Next step |
|---|---|---|---|---|
| 1 | Yes | 0 | 1 | Set i = 2 |
| 2 | Yes | 1 | 3 | Set i = 3 |
| 3 | Yes | 3 | 6 | Set i = 4 |
| 4 | Yes | 6 | 10 | Set i = 5 |
| 5 | Yes | 10 | 15 | Set i = 6 |
| 6 | No | 15 | 15 | Print sum (15) |
Five passes add five numbers, and the sixth test ends the loop. Check: 5 x 6 / 2 = 15.
READ n
sum = 0
i = 1
WHILE i <= n
sum = sum + i
i = i + 1
ENDWHILE
PRINT "Sum =", sum
n = int(input())
total = 0 # "sum" is a built-in name in Python
i = 1
while i <= n:
total = total + i
i = i + 1
print("Sum =", total)
#include <stdio.h>
int main(void) {
int n;
scanf("%d", &n);
int sum = 0;
int i = 1;
while (i <= n) {
sum = sum + i;
i = i + 1;
}
printf("Sum = %d\n", sum);
return 0;
}
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
int sum = 0;
int i = 1;
while (i <= n) {
sum = sum + i;
i = i + 1;
}
System.out.println("Sum = " + sum);
}
}
5
Sum = 15
All three programs were compiled and run with the input 5 before this page was published, and they print exactly the output shown. The Python version names the accumulator total because sum is a built-in function in Python.
< the loop stops before adding n itself: for n = 5 it gives 10. The condition is i <= n.Sum = 0. With i = 1 the first test 1 <= 0 is No, so the loop body never runs and sum keeps its starting value.
This is why the test sits above the body: a pre-test loop can run zero times.
Add an input box Read x at the top of the loop body and change the addition to Set sum = sum + x. The counter i now only counts how many numbers were read.
READ n
sum = 0
i = 1
WHILE i <= n
READ x
sum = sum + x
i = i + 1
ENDWHILE
PRINT "Sum =", sum
100 x 101 / 2 = 5050, which is what the loop prints after 100 passes.
The formula only works for 1 to n. The loop pattern works for any list of numbers, which is why exams ask for it. The same flowchart with a for loop is equally correct.
| Symbol | Shape to draw on paper | Where it appears here | Why |
|---|---|---|---|
| Terminator | Rounded box | Start and End | One entry and one exit. |
| Input/output | Parallelogram | Read n and Print sum | They move data in or out of the program. |
| Process | Rectangle | Set sum = 0, Set i = 1, Set sum = sum + i and Set i = i + 1 | Each changes a stored value and has one exit. |
| Decision | Diamond | i <= n? | A yes-or-no question with two labelled exits. |
| Loop | A decision whose flow line returns to an earlier step | The arrow from Set i = i + 1 back up to i <= n? | Returning to the diamond repeats the body. The diamond is above the body, so this is a pre-test loop. |
| Flow line | Arrow | Every arrow | Arrows give the order. Only the loop arrow points upward. |
In the interactive diagram every step is a labelled card: the small word above each step names its kind (process, decision or data). When you draw this flowchart by hand, use the standard shapes in the table. The full list is on the flowchart symbols page.
Each link opens the editor with a prompt already filled in. Change the wording, then generate your own flowchart.
Download the Sum of N Numbers Flowchart as a PNG or SVG, or open it in the studio to edit your own copy. To draw a similar flowchart from a description, use the flowchart generator; for notation and a step-by-step walkthrough, read how to make a flowchart; and browse more flowchart examples for related layouts.
Type any system, process, or schema in plain English. ChartCraft will draw it in seconds.