Day 4: Even or Odd?

Objective

Today’s task is to create a program that checks whether a number entered by the user is even or odd. This will introduce you to conditional statements (like if and else) which allow your program to make decisions based on the input.

An even number is divisible by 2 with no remainder, while an odd number has a remainder of 1 when divided by 2. The goal of today’s task is to check the remainder of the division and print a message indicating whether the number is even or odd.

Helpful Tips to Solve the Task

1. Understanding Even and Odd Numbers

  • A number is even if it’s divisible by 2 with no remainder. In programming, you can check if a number is even using the modulo operator (%).
    • Even number condition: number % 2 == 0
  • A number is odd if there is a remainder of 1 when divided by 2.
    • Odd number condition: number % 2 != 0

2. Using Conditional Statements

A conditional statement (like if or else) allows your program to execute different code depending on whether a condition is true or false. In this case, the condition is whether the number is divisible by 2.

Syntax:

Python:

if number % 2 == 0:
    print("The number is even.")
else:
    print("The number is odd.")

Java:

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

JavaScript:

if (number % 2 === 0) {
    console.log("The number is even.");
} else {
    console.log("The number is odd.");
}

3. Getting User Input

First, prompt the user to enter a number. Since the input from the user is usually a string, you will need to convert it to an integer before performing any calculations.

Python: Use input() and convert the input to an integer using int().

number = int(input("Enter a number: "))

Java: Use Scanner and capture the integer input with nextInt().

Scanner scanner = new Scanner(System.in);
System.out.print("Enter a number: ");
int number = scanner.nextInt();

JavaScript: Use prompt() to get the input and convert it to an integer using parseInt().

let number = parseInt(prompt("Enter a number:"));

4. Modulo Operator

  • The modulo operator (%) gives the remainder when a number is divided by another number. For example:
    • 5 % 2 will return 1 (because 5 divided by 2 leaves a remainder of 1).
    • 6 % 2 will return 0 (because 6 is evenly divisible by 2).

This operator will help you determine if a number is even or odd.

5. Displaying the Result

  • Once you determine if the number is even or odd, use print() (Python), System.out.println() (Java), or console.log() (JavaScript) to display the result.

Example Output

  • For input 4, the output will be: The number is even.
  • For input 7, the output will be: The number is odd.

6. Edge Cases to Consider

  • Negative Numbers: Negative numbers are still divisible by 2, so the program should work with negative even or odd numbers as well.
  • Zero: Zero is an even number because 0 % 2 == 0.

What You’ve Learned

  • Modulo Operator: You’ve learned how to use the modulo operator to check for even or odd numbers.
  • Conditional Statements: You’ve learned how to make decisions in your program using if and else.
  • User Input: You’ve gathered user input and performed basic operations on it.

Next Steps

Once you’ve successfully completed this task, you can expand your program:

  • Allow the user to input multiple numbers and check whether each one is even or odd.
  • Add some error handling to ensure the user inputs only valid integers.

This simple exercise will help you become more comfortable with basic conditionals and mathematical operations, which are essential for more complex programming challenges down the road.