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
- Start.
- Get an integer (
num) from the user. - Calculate the remainder when
numis divided by 2 (remainder = num % 2). - If
remainderis 0, thennumis even. - Else (
remainderis 1), thennumis odd. - Display the result.
- 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
- Java (JDK 17)
- Python (3.10+)
- C (C99)
- Oracle (19c)
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 anif-elsestatement to check divisibility by 2.Scannerhandles integer input. - Python: Similar to Java, uses the modulo operator (
%) and anif-elsestructure.input()reads as string,int()converts to integer. - C: Reads integer input using
scanf("%d", ...). The modulo operator (%) is used for the check within anif-elseblock. - Oracle: Implemented in PL/SQL. Uses the
MOD()function andIF-ELSEstatements. 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
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