Skip to content

← Back to Overview

Odd or Even Number Check

Concept Explanation

What is it?

An "odd or even" check determines whether an integer is perfectly divisible by two. - An even number is any integer that can be divided by 2 without leaving a remainder. - An odd number is any integer that, when divided by 2, leaves a remainder of 1.

Why is it important?

This is a fundamental concept in number theory and programming logic. It's often used as a basic example to introduce conditional statements and the modulo operator. It also has practical applications in algorithms that require distinguishing between elements based on their parity.

Where is it used?

  • Data processing: Separating data into groups based on numerical properties.
  • Game development: Alternating turns or actions (e.g., odd rounds vs. even rounds).
  • Array/List manipulation: Processing elements at even or odd indices.
  • Checksum algorithms: Some algorithms use parity checks.

Real-world example

Imagine a game where players take turns. Player 1 goes on odd-numbered rounds, and Player 2 goes on even-numbered rounds. Checking if the current round number is odd or even determines whose turn it is.


Algorithm

  1. Start.
  2. Get an integer (num) from the user.
  3. Calculate the remainder when num is divided by 2 (remainder = num % 2).
  4. If remainder is 0, then num is even.
  5. Else (remainder is 1), then num is odd.
  6. Display the result.
  7. End.

Edge Cases: - Non-integer input (handled by language-specific error mechanisms or explicit validation). - Zero: By definition, 0 is an even number because it is divisible by 2 with no remainder (0 % 2 = 0). - Negative numbers: The definition of odd/even extends to negative integers. For example, -2 is even, -3 is odd.


Implementations

import java.util.Scanner;

public class OddOrEven {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        System.out.print("Enter an integer: ");
        int number = scanner.nextInt();

        System.out.println("Number entered: " + number);

        if (number % 2 == 0) {
            System.out.println("The number is EVEN.");
        } else {
            System.out.println("The number is ODD.");
        }

        scanner.close();
    }
}
# Get an integer from the user
num_str = input("Enter an integer: ")
num = int(num_str)

print(f"Number entered: {num}")

if num % 2 == 0:
    print("The number is EVEN.")
else:
    print("The number is ODD.")
#include <stdio.h>

int main() {
    int num;

    printf("Enter an integer: ");
    scanf("%d", &num);

    printf("Number entered: %d\n", num);

    if (num % 2 == 0) {
        printf("The number is EVEN.\n");
    } else {
        printf("The number is ODD.\n");
    }

    return 0;
}
SET SERVEROUTPUT ON;
DECLARE
  input_num   NUMBER := &Enter_an_Integer;
BEGIN
  DBMS_OUTPUT.PUT_LINE('Input Number: ' || input_num);

  IF MOD(input_num, 2) = 0 THEN
    DBMS_OUTPUT.PUT_LINE('The number is EVEN.');
  ELSE
    DBMS_OUTPUT.PUT_LINE('The number is ODD.');
  END IF;
END;
/

Explanation

  • Java: Uses the modulo operator (%) with an if-else statement to check divisibility by 2. Scanner handles integer input.
  • Python: Similar to Java, uses the modulo operator (%) and an if-else structure. input() reads as string, int() converts to integer.
  • C: Reads integer input using scanf("%d", ...). The modulo operator (%) is used for the check within an if-else block.
  • Oracle: Implemented in PL/SQL. Uses the MOD() function and IF-ELSE statements. Substitution variables (&Enter_an_Integer) allow for user input.

Complexity Analysis

  • Time Complexity: O(1) - The number of operations is constant, regardless of the input number's magnitude.
  • Space Complexity: O(1) - A fixed number of variables are used.

Flowchart

graph TD
    A[Start] --> B[Get Integer num]
    B --> C{num % 2 == 0?}
    C -- Yes --> D[Display EVEN]
    C -- No --> E[Display ODD]
    D --> F[End]
    E --> F

Sample Dry Run

Step num num % 2 Description
Input 7 - User enters 7
Process 7 1 Calculate 7 % 2
Decision - 1 1 is not equal to 0 (False)
Output - - Display "The number is ODD."
End - - Program terminates

Practice Problems

Easy

  • Modify the program to check if a number is divisible by 3, 5, or both.
  • Extend the program to check if a number is positive, negative, or zero.

Medium

  • Write a program that counts the number of odd and even numbers in a given range.
  • Take multiple numbers as input and classify each as odd or even.

Hard

  • Implement a function that efficiently finds the sum of all odd numbers and the sum of all even numbers up to a given limit.

"The only way to learn a new programming language is by writing programs in it." - Dennis Ritchie