๐ Day 6/150 – Find Remainder of Division in Python
Today we will learn how to find the remainder of a division in Python.
The remainder is the value left after division, and it is commonly used in:
- Even/Odd checks
- Cyclic operations
- Number-based logic
๐ง Problem Statement
๐ Write a Python program to find the remainder when one number is divided by another.
1️⃣ Using % Operator (Most Common)
The % operator is called the modulus operator, and it gives the remainder.
a = 10 b = 3 remainder = a % b print("Remainder:", remainder)Output
Remainder: 1
✔ Simple and widely used
✔ Best method for beginners
2️⃣ Taking User Input
Make the program dynamic using user input.
✔ Useful in real applications
3️⃣ Using a Function
Functions help in writing reusable code.
def find_remainder(x, y): return x % y print(find_remainder(10, 3))
✔ Clean and reusable
✔ Good programming practice
4️⃣ Using divmod() Function
Python provides a built-in function that returns both quotient and remainder.
a = 10 b = 3 quotient, remainder = divmod(a, b) print("Quotient:", quotient) print("Remainder:", remainder)
Output
Quotient: 3
Remainder: 1
✔ Efficient
✔ Useful when both values are needed
⚠️ Important Note
Division by zero will cause an error:
print(10 % 0) # ❌ Error
Always handle it safely:
if b != 0: print(a % b) else: print("Cannot divide by zero")
๐ฏ Key Takeaways
Today you learned:
- Modulus operator %
- Taking user input
- Using functions
- Using divmod()
- Handling division errors
.png)

0 Comments:
Post a Comment