PYnative

Python Programming

  • Learn Python
    • Python Tutorials
    • Python Basics
    • Python Interview Q&As
  • Exercises
    • Python Exercises
    • C Programming Exercises
    • C++ Exercises
  • Quizzes
  • Code Editor
    • Online Python Code Editor
    • Online C Compiler
    • Online C++ Compiler
Home » Python » Programs and Examples » Python Programs to Check if a Number is Odd or Even

Python Programs to Check if a Number is Odd or Even

Updated on: March 31, 2025 | Leave a Comment

One of the most fundamental concepts in programming involves determining whether a number is even or odd. In this article, we’ll walk you through how to write a simple Python program to achieve this. This is a great starting point for understanding basic arithmetic operations and conditional logic in Python.

  • An Even Number is an integer that is divisible by 2 without leaving a remainder. For Example: 0, 2, 4, 6, 8, 10, etc.
  • An Odd Number is an integer that is not divisible by 2, leaving a remainder of 1. For Example: 1, 3, 5, 7, 9, etc.

This article discusses the most common methods, each with an explanation and example.

Table of contents

  • 1. Using the Modulo Operator (%)
  • 2. Using Integer Division operator ( // )

1. Using the Modulo Operator (%)

Using the modulo operator is a straightforward method for checking whether a number is even or odd in Python. The modulo operator (%) in Python is used to compute the remainder of the division between two numbers. When a number is divided by 2, the remainder is 0, so it’s an even number; otherwise, it is odd.

Steps:

  1. Get Input:
    • Use input() function to get the number that you want to check.
  2. Use the Modulo Operator:
    • Divide the number by 2 and find the remainder using the modulo operator (%).
    • The modulo operator returns the remainder of a division.
  3. Check the Remainder:
    • If the remainder is 0, the number is even.
    • If the remainder is 1, the number is odd.
  4. Output the Result:
    • Print or return a message indicating whether the number is even or odd.

Code Example

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

# Check if the number is even or odd using the modulo operator
if number % 2 == 0:
    print(f"{number} is even.")
else:
    print(f"{number} is odd.")Code language: Python (python)

2. Using Integer Division operator (//)

Integer Division operator (//) divides two numbers and returns the quotient by discarding the decimal part (i.e., the result is always an integer or a whole number if both inputs are integers).

For Example, (10 // 3) Output: 3 (10 ÷ 3 = 3.33; decimal part discarded).

This method uses integer division to determine whether a number is even or odd. The result of integer division is multiplied by 2 and compared with the original number.

Example

  • For number = 4: (number // 2) * 2 = (4 // 2) * 2 = 4. Since 4 equals the original number, it is even.
  • For number = 5: (number // 2) * 2 = (5 // 2) * 2 = 4. Since 4 does not equal the original number, it is odd.

Code Example

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

# Check if the number is even or odd using integer division
if (number // 2) * 2 == number:
    print(f"{number} is even.")
else:
    print(f"{number} is odd.")Code language: Python (python)

Explanation

  • Input: The program takes an integer input from the user.
  • Integer division (//): The expression number // 2 performs integer division, discarding any remainder.
  • Multiplication and comparison: The result of number // 2 is multiplied by 2. If the result equals the original number, it is even; otherwise, it is odd.
  • Output: The program prints whether the number is even or odd.

Filed Under: Programs and Examples, Python, Python Basics

Did you find this page helpful? Let others know about it. Sharing helps me continue to create free Python resources.

TweetF  sharein  shareP  Pin

About Vishal

Image

I’m Vishal Hule, the Founder of PYnative.com. As a Python developer, I enjoy assisting students, developers, and learners. Follow me on Twitter.

Related Tutorial Topics:

Programs and Examples Python Python Basics

All Coding Exercises:

C Exercises
C++ Exercises
Python Exercises

Python Exercises and Quizzes

Free coding exercises and quizzes cover Python basics, data structure, data analytics, and more.

  • 15+ Topic-specific Exercises and Quizzes
  • Each Exercise contains 25+ questions
  • Each Quiz contains 25 MCQ
Exercises
Quizzes

Leave a Reply Cancel reply

your email address will NOT be published. all comments are moderated according to our comment policy.

Use <pre> tag for posting code. E.g. <pre> Your entire code </pre>

In: Programs and Examples Python Python Basics
TweetF  sharein  shareP  Pin

 Explore Python

  • Python Tutorials
  • Python Exercises
  • Python Quizzes
  • Python Interview Q&A
  • Python Programs

  Python Tutorials

  • Get Started with Python
  • Python Statements
  • Python Comments
  • Python Keywords
  • Python Variables
  • Python Operators
  • Python Data Types
  • Python Casting
  • Python Control Flow statements
  • Python For Loop
  • Python While Loop
  • Python Break and Continue
  • Python Nested Loops
  • Python Input and Output
  • Python range function
  • Check user input is String or Number
  • Accept List as a input from user
  • Python Numbers
  • Python Lists
  • Python Tuples
  • Python Sets
  • Python Dictionaries
  • Python Functions
  • Python Modules
  • Python isinstance()
  • Python OOP
  • Python Inheritance
  • Python Exceptions
  • Python Exercise for Beginners
  • Python Quiz for Beginners

All Python Topics

  • Python Basics
  • Python Exercises
  • Python Quizzes
  • Python File Handling
  • Python Date and Time
  • Python OOP
  • Python Random
  • Python Regex
  • Python Pandas
  • Python Databases
  • Python MySQL
  • Python PostgreSQL
  • Python SQLite
  • Python JSON

About PYnative

PYnative.com is for Python lovers. Here, You can get Tutorials, Exercises, and Quizzes to practice and improve your Python skills.

Follow Us

To get New Python Tutorials, Exercises, and Quizzes

  • Twitter
  • Facebook
  • Sitemap

Explore Python

  • Learn Python
  • Python Basics
  • Python Databases
  • Python Exercises
  • Python Quizzes
  • Online Python Code Editor
  • Python Tricks

Coding Exercises

  • C Exercises
  • C++ Exercises
  • Python Exercises

Legal Stuff

  • About Us
  • Contact Us

We use cookies to improve your experience. While using PYnative, you agree to have read and accepted our:

  • Terms Of Use
  • Privacy Policy
  • Cookie Policy

Copyright © 2018–2026 pynative.com

Advertisement
Advertisement