← Back

Home › Python programs (Hindi)

Even Odd Program Python में — code, output और dry run

Read this in English

Problem statement

यह program number को पढ़कर बताता है कि वह even है या odd। Zero even होता है, और negative numbers पर भी यही rule लागू होता है।

Approach 1: simple loop

Test the remainder of division by 2. A number that divides evenly by 2 leaves remainder 0 — that is the definition of even, and % gives you the remainder directly.

n = 7
if n % 2 == 0:
    print("Even")
else:
    print("Odd")
# Output: Odd

The condition reads like the maths: "if the remainder of n divided by 2 equals 0". Everything else is printing the answer.

Approach 2: reusable function

Wrap the test in a function that returns a boolean. The program then reads as a question and an answer, and the same function works for any list of numbers.

def is_even(n):
    return n % 2 == 0

print(is_even(10))   # True
print(is_even(7))    # False
print(is_even(0))    # True
print(is_even(-3))   # False

Returning True/False instead of printing inside the function keeps the decision reusable — the caller decides what to do with it.

Approach 3: Pythonic alternative

Python lets you pick a value with a conditional expression, which is handy when the output itself must be a word. The bitwise test n & 1 is the compact alternative — it checks the last binary digit.

for n in [4, 7, 0, -3]:
    print(n, "Even" if n % 2 == 0 else "Odd")
# Output:
# 4 Even
# 7 Odd
# 0 Even
# -3 Odd

Note that Python's % behaves politely with negatives: -3 % 2 is 1, so -3 is correctly reported as odd — no special case needed.

Dry run

मान लीजिए input 7 है। 7 % 2 का मतलब: 7 को 2 से भाग देने पर शेष क्या बचता है? 2 तीन बार जाता है (6), शेष 1। अब 1 == 0 झूठा है, इसलिए else चलेगा और Odd print होगा। 0 के लिए 0 % 2 ही 0 है — इसलिए Even। हर input को इसी तरह trace करें: पहले remainder, फिर comparison, फिर branch।

Common errors

Practice on PyDebug

Concept समझने के बाद इन free problems को browser में solve करें: