Even or Odd: flowchart, algorithm and program

This program reads an integer n and prints whether it is even or odd. It uses the remainder operator %: if n divided by 2 leaves remainder 0, the number is even, otherwise it is odd.

▶ Watch the dry run step by step →

Algorithm

  1. Step 1: Start.
  2. Step 2: Read n.
  3. Step 3: If n % 2 == 0, go to step 4; otherwise go to step 5.
  4. Step 4: Print n is even. Go to step 6.
  5. Step 5: Print n is odd.
  6. Step 6: Stop.

Pseudocode

BEGIN
    READ n
    IF n % 2 == 0 THEN
        PRINT n is even
    ELSE
        PRINT n is odd
    END IF
END

Program in C, Python and Java

Even or Odd program in C

#include <stdio.h>

int main() {
    int n;
    printf("Enter n: ");
    scanf("%d", &n);
    if (n % 2 == 0)
        printf("%d is even\n", n);
    else
        printf("%d is odd\n", n);
    return 0;
}

Even or Odd program in Python

n = int(input("Enter n: "))
if n % 2 == 0:
    print(n, "is even")
else:
    print(n, "is odd")

Even or Odd program in Java

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter n: ");
        int n = sc.nextInt();
        if (n % 2 == 0)
            System.out.println(n + " is even");
        else
            System.out.println(n + " is odd");
    }
}

Sample input: n = 7
Output: Enter n: 7 is odd

How it works

  • scanf reads the integer into the variable n.
  • The expression n % 2 gives the remainder when n is divided by 2, which is always 0 or 1 for positive numbers.
  • If n % 2 == 0, the if branch runs and prints "n is even".
  • Otherwise the else branch runs and prints "n is odd".
  • For n = 7, 7 % 2 is 1, which is not 0, so the program prints "7 is odd".

Dry run (trace table)

StepLineBoxnOutput
13Start
26Read n7Enter n:
37n % 2 == 0?7
410Print n is odd77 is odd
512Stop7

Time and space complexity

Time: O(1) · Space: O(1). It does a single modulo check and one print, regardless of the value of n.

Common mistakes

  • Writing n % 2 = 0 with a single = instead of ==, which assigns instead of comparing (and is a compile error in this position).
  • Forgetting the & in scanf("%d", &n), so the input is not stored in n.
  • Assuming n % 2 == 1 is the test for odd: for negative numbers like -7, n % 2 is -1 in C, so testing for == 0 (even) and treating everything else as odd is safer.

Viva questions

Q1. What does the condition n % 2 == 0 check in line 7?

It checks whether the remainder after dividing n by 2 is zero. If it is, n is divisible by 2, so it is even.

Q2. Why do we write &n in scanf on line 6?

scanf needs the address of n so it can store the value typed by the user directly into that variable. Without & it would not know where to write.

Q3. What will the program print for n = 0?

It prints '0 is even', because 0 % 2 is 0, so the condition is true.

Q4. What happens for a negative number such as -3?

In C, -3 % 2 gives -1, not 1, because the remainder takes the sign of the dividend. Since it is not 0, the else branch runs and prints 'is odd', so the program is still correct. This is also why we test == 0 rather than == 1, which would fail for negative odd numbers.

Q5. What is the difference between = and == in line 7?

= assigns a value, while == compares two values. Using = here would assign 0 to the result instead of testing it, giving a wrong result.

Q6. Why is there an if-else instead of two separate if statements?

A number is either even or odd, never both, so else covers the other case. It also avoids evaluating a second condition unnecessarily.

Q7. What is the time complexity of this program?

O(1), constant time, because it does one remainder operation and one comparison regardless of how large n is.

Q8. What would change if you wanted to check whether n is divisible by 5 instead?

Replace 2 with 5 in the condition, giving n % 5 == 0, and change the messages accordingly. The same remainder idea works for divisibility by any number.

Q9. Could you check even or odd without using the % operator?

Yes, use the bitwise AND: n & 1. It is 0 for even numbers and 1 for odd numbers, since the last binary bit decides parity. This also works correctly for negative numbers in two's complement.

Frequently asked questions

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

Use the remainder operator: if n % 2 == 0 the number is even, otherwise it is odd.

Does this program work for negative numbers?

Yes. For -4, n % 2 is 0 so it is even; for -7, n % 2 is -1 (not 0), so it falls into the else branch and is correctly reported as odd.

Is 0 even or odd?

0 is even. 0 % 2 equals 0, so the program prints "0 is even".

More programs

← All programs

Try AI Flowchart free →