Is the last digit divisible by 4?

EasyIf-Else Programs · 55 of 64

Write a Python program to check whether the last digit of a number is divisible by 4.

Advertisement
Input
58, 37
Output
58 -> last digit 8 is divisible by 4
37 -> last digit 7 is not divisible by 4

Approach

  1. Get the last digit with n % 10, then test % 4.

Solution

Python
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.

More if-else programs

← All 64 If-Else Programs

Leave a Comment