C PROGRAM • DECISION MAKING

Check Whether a Number Is Even or Odd

Use the remainder operator and an if-else decision to classify an integer.

Beginnerif-elseModulusCondition

PROBLEM UNDERSTANDING

Input and expected output

Sample input
17
Sample output
Enter an integer: 17
17 is odd.

C PROGRAM

Complete program

even-or-odd.c
#include <stdio.h>

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

INTERACTIVE LEARNING

Debug this program line by line

Watch the active line, variables and output.

CURRENT STEP

Select Start to trace this program.

VARIABLES

ScopeNameValue
Not started

CALL STACK

main()

PROGRAM OUTPUT

Waiting to run…
0%Step 0 of 0

PROGRAM EXPLANATION

Algorithm and explanation

  1. Read an integer.
  2. Calculate number % 2.
  3. If the remainder is zero, print even; otherwise print odd.

An even integer is exactly divisible by 2. Therefore number % 2 gives 0 for even values and a non-zero remainder for odd values.

EFFICIENCY

Time and space complexity

Time complexity

O(1)

Auxiliary space

O(1)

DEBUGGING CHECKLIST

Common mistakes

Check this

Using = instead of == in the condition.

Check this

Using division instead of the remainder operator.