Sum of Even Numbers up to n: flowchart, algorithm and program

This program reads a number n and prints the sum of all even numbers from 1 to n. It loops through every number from 1 to n, checks which ones are even using i % 2 == 0, and adds only those to sum.

▶ 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 9.
  6. Step 6: If i % 2 == 0, go to step 7; otherwise go to step 8.
  7. Step 7: Set sum = sum + i.
  8. Step 8: Set i = i + 1. Go to step 5.
  9. Step 9: Print sum.
  10. Step 10: Stop.

Pseudocode

BEGIN
    sum ← 0
    READ n
    i ← 1
    WHILE i <= n DO
        IF i % 2 == 0 THEN
            sum ← sum + i
        END IF
        i ← i + 1
    END WHILE
    PRINT sum
END

Program in C, Python and Java

Sum of Even Numbers up to n 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++) {
        if (i % 2 == 0) {
            sum = sum + i;
        }
    }
    printf("Sum of even numbers = %d\n", sum);
    return 0;
}

Sum of Even Numbers up to n program in Python

n = int(input("Enter n: "))
total = 0
for i in range(1, n + 1):
    if i % 2 == 0:
        total = total + i
print("Sum of even numbers =", total)

Sum of Even Numbers up to n 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++) {
            if (i % 2 == 0) {
                sum = sum + i;
            }
        }
        System.out.println("Sum of even numbers = " + sum);
    }
}

Sample input: n = 10
Output: Sum of even numbers = 30

How it works

  • sum starts at 0 so that the first even number added gives the correct total.
  • The for loop runs i from 1 to n, so every number in that range is checked once.
  • The condition i % 2 == 0 is true only when i leaves no remainder after dividing by 2, which means i is even.
  • When i is even, sum = sum + i adds it to the running total. Odd numbers are skipped.
  • For n = 10 the even numbers are 2, 4, 6, 8, 10, so sum becomes 2, 6, 12, 20, 30. After the loop ends, 30 is printed.

Dry run (trace table)

StepLineBoxsumniOutput
13Start
24sum = 00
36Read n010
47i = 10101
57i <= n?0101
68i % 2 == 0?0101
77i = i + 10102
87i <= n?0102
98i % 2 == 0?0102
109sum = sum + i2102
117i = i + 12103
127i <= n?2103
138i % 2 == 0?2103
147i = i + 12104
157i <= n?2104
168i % 2 == 0?2104
179sum = sum + i6104
187i = i + 16105
197i <= n?6105
208i % 2 == 0?6105

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

  • Forgetting to set sum = 0 before the loop, so it starts with a garbage value in C.
  • Using i < n instead of i <= n, which leaves out n when n itself is even (for n = 10 the answer would be 20, not 30).
  • Writing i % 2 = 0 with a single = (assignment) instead of == (comparison).

Viva questions

Q1. Why is sum initialised to 0 and not left uninitialised or set to 1?

0 is the identity for addition, so the first even number is added to 0 and the total stays correct. If sum were uninitialised it would hold a garbage value, and starting at 1 would make the answer off by one.

Q2. What does the condition i % 2 == 0 check?

The % operator gives the remainder after division. If i divided by 2 leaves remainder 0, then i is even, so only then is it added to sum.

Q3. What is the output if the user enters n = 10? Walk me through it.

The loop adds 2, 4, 6, 8 and 10, so the output is 30. Odd values of i fail the if test and are skipped.

Q4. What happens if n is 0, 1 or a negative number?

The loop condition i <= n is false at the start (or, for n = 1, 1 is odd and never added). The body does nothing and the program prints 0. There is no error message for negative input, which could be considered a weakness.

Q5. Which kind of loop have you used and why is it suitable here?

A for loop, because the number of iterations is known in advance, from 1 to n. It keeps the initialisation, condition and increment together on one line.

Q6. What is the time complexity of this program, and how could you improve it?

It is O(n) because the loop runs n times with a constant-time body. You can start at 2 and step by 2 (i += 2) to halve the iterations, or use the formula k*(k+1) with k = n/2 to get O(1).

Q7. What would change if you wanted the sum of odd numbers instead?

Change the condition to i % 2 != 0 (or i % 2 == 1). Everything else stays the same.

Q8. What could go wrong with the int type for sum when n is very large?

The sum grows roughly as n squared divided by 4, so for large n it can exceed the int limit and overflow, giving a wrong or negative result. Using long long for sum would fix this.

Frequently asked questions

How do you check if a number is even in C?

Use the remainder operator: if i % 2 == 0, the number is even. If the remainder is 1, it is odd.

Can this program be made faster without checking every number?

Yes. Start i at 2 and increase it by 2 each time (i += 2), so only even numbers are visited and the if check is not needed. There is also a formula: with k = n / 2, the sum is k * (k + 1).

What is the output if n is 1 or less?

The loop never adds anything, so sum stays 0 and the program prints 'Sum of even numbers = 0'. For n = 1 no even number exists in the range.

More programs

← All programs

Try AI Flowchart free →