Python Practice › If-Else Programs › Program 55
Is the last digit divisible by 4?
Write a Python program to check whether the last digit of a number is divisible by 4.
Input
58, 37
Output
58 -> last digit 8 is divisible by 4 37 -> last digit 7 is not divisible by 4
Approach
- Get the last digit with
n % 10, then test% 4.
Solution
for n in [58, 37]: last = n % 10 if last % 4 == 0: print(n, "-> last digit", last, "is divisible by 4") else: print(n, "-> last digit", last, "is not divisible by 4")Output
58 -> last digit 8 is divisible by 4 37 -> last digit 7 is not divisible by 4
Line by line
Line 2
The remainder after dividing by 10 is the last digit.
Learn the concept
New to this topic? Read the lesson, then come back.