Example 2: Check Even or Odd - 4.6.2 | 4. Program Coding | ICSE 8 Computer Applications
Students

Academic Programs

AI-powered learning for grades 8-12, aligned with major curricula

Professional

Professional Courses

Industry-relevant training in Business, Technology, and Design

Games

Interactive Games

Fun games to boost memory, math, typing, and English skills

Example 2: Check Even or Odd

4.6.2 - Example 2: Check Even or Odd

Enroll to start learning

You’ve not yet enrolled in this course. Please enroll for free to listen to audio lessons, classroom podcasts and take practice test.

Practice

Interactive Audio Lesson

Listen to a student-teacher conversation explaining the topic in a relatable way.

Understanding Conditions and the Modulo Operator

🔒 Unlock Audio Lesson

Sign up and enroll to listen to this audio lesson

0:00
--:--
Teacher
Teacher Instructor

Today, we're going to learn how to check whether a number is even or odd using a simple Python program. First, let's understand what the modulo operator does. Can anyone tell me what the modulo operator is?

Student 1
Student 1

Isn't the modulo operator used to find the remainder of a division?

Teacher
Teacher Instructor

Exactly, Student_1! For example, when we divide 5 by 2, the remainder is 1. So, we can use `%` to check if a number is even or odd. If `num % 2` equals 0, the number is even. Otherwise, it's odd. Can someone provide an example of what that looks like in Python?

Student 2
Student 2

Could you use 4 as an example? If we do `4 % 2`, it equals 0, so it's even!

Teacher
Teacher Instructor

Great illustration, Student_2! It’s a simple but powerful way to check conditions in programming.

Writing the Program

🔒 Unlock Audio Lesson

Sign up and enroll to listen to this audio lesson

0:00
--:--
Teacher
Teacher Instructor

Now, let’s write our program together. First, we need to get the user's input. What function do we use to take input in Python?

Student 3
Student 3

We can use the `input()` function!

Teacher
Teacher Instructor

That's correct, Student_3! Let's write the first line: `num = int(input('Enter a number: '))`. This will store the input as an integer in the variable `num`. What should we do next?

Student 4
Student 4

Now we need to add the `if` condition to check if this number is even or odd.

Teacher
Teacher Instructor

Right, Student_4! So we write: `if num % 2 == 0:` followed by the code to print 'Even'. In the `else` part, we'll print 'Odd'.

Student 1
Student 1

So the complete program prints whether the entered number is even or odd?

Teacher
Teacher Instructor

Exactly! Let’s summarize what we learned: We used input to capture a number, applied the modulo operator to check evenness, and created an `if-else` structure for decision-making.

Code Review and Testing

🔒 Unlock Audio Lesson

Sign up and enroll to listen to this audio lesson

0:00
--:--
Teacher
Teacher Instructor

Now that we’ve written our program, let’s test it out! What types of numbers should we test?

Student 3
Student 3

We should try both even and odd numbers to see if the program works correctly!

Teacher
Teacher Instructor

Good thinking! If we test with 8, what do you expect the output to be?

Student 2
Student 2

The output should say 'Even'!

Teacher
Teacher Instructor

Correct! And if we input 7, what should happen?

Student 4
Student 4

It should say 'Odd'!

Teacher
Teacher Instructor

Nice job! Testing different inputs helps us ensure our program functions as expected. Remember, always test extensively!

Introduction & Overview

Read summaries of the section's main ideas at different levels of detail.

Quick Overview

This section provides a practical example of a simple program in Python that checks if a number is even or odd.

Standard

The section illustrates how to use the modulo operator to determine whether an input number is even or odd through a straightforward Python program, emphasizing the use of conditional statements for decision-making in programming.

Detailed

Detailed Summary

In this section, we explore an example of a simple program written in Python that checks whether a given number is even or odd. The program utilizes the modulo operator (%), which gives the remainder of a division. When a number is divided by 2, if the remainder is 0, the number is classified as even; otherwise, it's odd.

Key Concepts:

  • Input Handling: The program begins by requesting input from the user.
  • Conditional Statements: It uses an if-else construct to make decisions based on the remainder of the number when divided by 2.
  • Output Display: Finally, it provides output to the user indicating whether the given number is even or odd.

This practical example helps reinforce fundamental programming concepts such as variables, control structures, and input/output operations.

Audio Book

Dive deep into the subject with an immersive audiobook experience.

Input a Number

Chapter 1 of 2

🔒 Unlock Audio Chapter

Sign up and enroll to access the full audio experience

0:00
--:--

Chapter Content

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

Detailed Explanation

In this chunk, we are asking the user to enter a number. The input() function captures the user's input as a string, and the int() function converts that string into an integer so that we can perform numerical operations on it.

Examples & Analogies

Think of this step as asking someone their age. When they tell you '15', you need to convert that spoken word into a number (15) to understand how old they actually are.

Checking Even or Odd

Chapter 2 of 2

🔒 Unlock Audio Chapter

Sign up and enroll to access the full audio experience

0:00
--:--

Chapter Content

if num % 2 == 0:
    print("Even")
else:
    print("Odd")

Detailed Explanation

This chunk contains a conditional statement that checks if the number is even or odd. The % operator calculates the remainder when num is divided by 2. If the remainder is 0, the number is even, and 'Even' is printed. If the remainder is not 0, the number is odd, and 'Odd' is printed instead.

Examples & Analogies

Imagine you have a group of friends, and you want to pair them up for a game. If you have an even number of friends, everyone gets a partner, which is like the number being 'Even'. But if one friend is left out, they don't have a partner, which represents the number being 'Odd'.

Key Concepts

  • Input Handling: The program begins by requesting input from the user.

  • Conditional Statements: It uses an if-else construct to make decisions based on the remainder of the number when divided by 2.

  • Output Display: Finally, it provides output to the user indicating whether the given number is even or odd.

  • This practical example helps reinforce fundamental programming concepts such as variables, control structures, and input/output operations.

Examples & Applications

Using the program, if the input is 4, the output will be 'Even'.

If the input is 7, the output will be 'Odd'.

Memory Aids

Interactive tools to help you remember key concepts

🎵

Rhymes

To find if a number's neat (even) or odd, divide by two with a nod!

📖

Stories

Once there was a number named 'Seven'. It wanted to know if it was cool. So, it went to a wise old teacher named Python, who said, 'If you divide me by 2 and the remainder's not seen, you’re even like a queen!'

🧠

Memory Tools

E.O. (Even - Odd) can help you remember: Even means 0 remainder, Odd means 1 remainder.

🎯

Acronyms

EOP (Even or Odd Program)

E

for Even

O

for Odd

P

for Program helps us recall how to check.

Flash Cards

Glossary

Even Number

An integer that is divisible by 2 with no remainder.

Odd Number

An integer that is not divisible by 2 without a remainder.

Modulo Operator

A mathematical operator that returns the remainder of a division operation.

Conditional Statement

A programming construct that allows execution of certain code based on whether a condition evaluates to true or false.

Input

Data that is received by a program from the user.

Output

The data that is sent from a program to the user.

while True

num = int(input('Enter a number (0 to stop): '))

if num == -1

break

if num % 2 == 0 print('Even')

else: print('Odd')

  • Hint: Think about how to implement a stopping condition.
  • Problem: Modify the program to count how many even and how many odd numbers have been inputted by the user.
  • Solution: // Pseudocode Example:
    // even_count = 0
    // odd_count = 0
    // while True:
    // num = int(input('Enter a number (or -1 to exit): '))
    // if num == -1:
    // break
    // if num % 2 == 0:
    // even_count += 1
    // else:
    // odd_count += 1
    // print('Even count:', even_count, 'Odd count:', odd_count)
  • Hint: Consider how to maintain and update counters for even and odd inputs.

if num == 0

break

Reference links

Supplementary resources to enhance your learning experience.