This is a continuation from [part (1/2)] https://coderlegion.com/1568/write-a-pseudocode-and-python-code-to-find-the-sum-of-numbers-divisible-by-4-part1
A number is known to be divisible by four if the remainder of the integer division between that number and four is zero. For example, is the number 20 divisible by 4? The answer is yes because 20 divided by 4 is 5 and the remainder is zero. Another example, is the number 10 divisible by 4? The answer is no, because the remainder is 2.
There are many methods to test if a number is divisible by 4.
Divide the number by four using long division. If the remainder is zero, then that number is divisible by four.
Is the number 7772 divisible by 4?
Let us try the long division.

Since the remainder is zero, the number 7772 is divisible by 4.
Use the modulo operator %
of Python to calculate the remainder. If the remainder is 0, that number is divisible by 4.
Is the number 96 divisible by 4?
filename: mymodulo.py
code
remainder = 96 % 4
print(f'The remainder is {remainder}.')
if remainder == 0:
print(f'The number 96 is divisible by 4.')
else:
print(f'The number 96 is not divisible by 4.')
run result

In line 1, we use the modulo operator %
to get the remainder.
Python %
operator has its equivalent function via the operator module. Have a look at the following example.
filename: mymodulofunc.py
code
import operator
a = 750
b = 4
remainder = operator.mod(a, b)
print(f'The remainder is {remainder}.')
run result

In line 1, we import the operator module so that we can use the function mod()
.
a. Multiple of 4
A number is divisible by 4 if the last two digits of the given number is a multiple of four. But what is multiple of four? A multiple of four is the product or result after multiplying four with any whole number. Check these examples.
Is 12 a multiple of 4? Yes it is because 4 times 3 is 12.
Is 14 a multiple of 4? No because:
4 x 0 = 0
4 x 1 = 4
4 x 2 = 8
4 x 3 = 12
4 x 4 = 16
We have seen that the number 14 is not there. So 14 is not a multiple of 4.
Try this example and check the last two digits in a given number if it is a multiple of 4, and if so, that number is also divisible by 4.
Is the number 937828 divisible by four?
To answer that question we need the following:
- Get the last 2 digits of that number which is 28.
- Is 28 a multiple of 4? Yes because 4 times 7 is 28.
- Since 28 is a multiple of 4, that means the number 937828 is divisible by 4.
Now let us verify by modulo operator %
if indeed 937828 is divisible by 4.
code
remainder = 937828 % 4
print(f'937828 % 4 has a remainder of {remainder}')
run result

The verification showed that the remainder is 0 and so the number 937828 is divisible by 4.
b. Divisibility by 4 again
We take the last 2 digits of a given number. If these digits are divisible by four then the given number is divisible by four. The advantage of this method is we reduce the number into two digits only.
If we are given a big integer number of 2456984582154816
, just take the last 2 digits which is 16
. Since 16
is divisible by 4 because 16/4
is 4
with zero remainder, that big number is divisible by 4.
Programmatically we can extract the last two digits of an integer number by the following function. The number must be two or more digits.
code
def get_last_two_digits(number: int):
return number % 100
n = 25485698786212
last_two_digit = get_last_two_digits(n)
print(last_two_digit)
run result

The last two digits are 12
which is correct.
You can use the built-in function divmod to calculate the remainder. If the remainder is 0, that number is divisible by 4. We have already discussed the divmod in the previous section. Now we will use it to detect if a number is divisible by four.
Is the number 8454 divisible by 4?
To solve that, we will use the divmod()
to see if the remainder is zero between 8454 as the first argument and 4 as the second argument.
code
arg1 = 8454
arg2 = 4
quotient, remainder = divmod(arg1, arg2)
print(f'remainder is {remainder}')
if remainder == 0:
print(f'The number {arg1} is divisible by 4.')
else:
print(f'The number {arg1} is not divisible by 4.')
run result

Since the remainder is not zero, the number 8454 is not divisible by 4.
Let us now create a pseudocode to sum the number if that number is divisible by four. Remember, a number is divisible by four if the remainder is zero after applying a modulo operation between that number and four. See section E. Divisibility by four rules.
- Initialize a variable called
divisible_by_four_total
to zero for storing the sum.
- Create a
while
loop to prompt the user to input a number. Break the loop if a user enters the word end
.
a. Ask the user to enter a number or the word end
to end asking the user.
b. If the user enters the word end
go to step 3.
c. If the user enters a number, check if the number is divisible by 4.
1) If the number is divisible by four add it to the divisible_by_four_total
.
2) If the number is not divisible by four, print a message to console "The number is not divisible by four."
3) Go to step 2.a.
- Print to console the value in
divisible_by_four_total
.
This is our code based on the pseudocode.
filename: divisiblebyfour.py
code
def divisible_by_four(n: int):
"""Determines if n is divisible by four.
Args:
n: An input number.
Returns:
True if n is divisible by 4 otherwise false
"""
return True if int(n) % 4 == 0 else False
# step 1
divisible_by_four_total = 0
# step 2
while True:
# step 2.a
user_input = input("Input a number or the word end to stop asking? ")
# step 2.b
if user_input == 'end':
break
# step 2.c
if divisible_by_four(user_input):
# step 2.c.i
divisible_by_four_total += int(user_input)
else:
# step 2.c.ii
print('The number is not divisible by four.')
# step 2.c.iii (go to start of while loop above)
# step 3
print(f'The sum of all divisible by 4 given numbers is {divisible_by_four_total}.')
sample run result

In the program we use the modulo operator to determine if the given number is divisible by four or not. If the remainder of n % 4
is zero, the n
is divisible by four. This can be seen in the function divisible_by_four()
.
How about validating the total?
total = 56 + 888 + 48 + 512 or 1504. So the program total is correct. Note the numbers 56, 888, 48, 512 are all divisible by 4.
We calculate the sum of numbers provided each of these numbers is divisible by four. We employ the remainder technique to ascertain the divisibility of a number by four. If the remainder is zero, it indicates that the number is divisible by four. The modulo operator provides the fastest and simpler way to obtain this remainder.
The pseudocode allows us to see ahead, what tasks are present, how they are executed and what is its expected output. Pseudocode is very important in the implementation of the actual code. It guides the developers to not waste time doing things that are not in the pseudocode, thereby saving resources and avoiding conflicts. It generates a kind of focus that accomplishes tasks more quickly as these tasks are known in advance.