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¶
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();
}
}
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¶
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