Write a pseudocode and python code to find the sum of numbers divisible by 4 - Part1

Write a pseudocode and python code to find the sum of numbers divisible by 4 - Part1

posted 5 min read

Pseudocode illustrates the procedure in natural language on how to achieve a goal. Pseudocode is important because it will allow us to see an overview of all tasks, recognize step by step how tasks are executed and the intent of each task. It is not perfect as predicting the future is not that easy but it serves as a guide and gives direction in achieving the target.

In this article, our goal is to sum all the given numbers provided that each number is divisible by four. A number is divisible by four if the remainder is zero after dividing that number by four.

Mathematics is broad, by learning its different concepts, such as greatest common factor; multiplication; number divisibility; sum; multiples; factors; and others, it allows us to understand more of it which will prepare us to solve mathematical problems from our work and even in our day to day living.

The input numbers can be from a user whom we will prompt. First, we need to build a pseudocode, and second we will create a Python code based on the pseudocode.

A. What is the problem

The problem is to sum all the given numbers that are divisible by four. But we need to show the pseudocode and its equivalent python code. In the subsequent sections, we will discuss pseudocode, divisibility by four rules and remainder.

We will follow a scenario where the program takes the input number, checks if it is divisible by four and if so, adds the number to the total. Note that we only deal with whole numbers such as 5, 10, etc. but not float numbers such as 8.5, 12.5 and others with decimal points.

B. What is pseudocode

Pseudocode is a written instruction or procedure that uses human language in order to achieve an objective. Each line is a task that is actionable. These tasks are numbered where the first task has to be finished before the second and so on. A task may include a subtask to break down a complicated task.

Python has keywords such as if, else, while, True, False, or, continue, and break that are also used in the English language. We can use them to generate pseudocode.

1. Advantages of having a pseudocode

  • Pseudocode can be understood by everybody not only the programmers but also the non-programmers.
  • People collaborate effectively in achieving the project goals.
  • Issues that may arise are only minimal as pseudocode passes a thorough review.
  • It is easier to resolve issues as everyone knows about the project's plan.
  • Pseudocode can assist finance people in providing a good estimate of the costs and profits.

Pseudocode can be updated to reflect what is on the actual implementation. Future similar projects may benefit from this updated pseudocode.

2. Example of pseudocode and python code

Our goal is to multiply two numbers and print the product.

pseudocode

1. Asks the user to input the first number, remember it by storing the same into the variable x.
2. Asks the user to input the second number, remember it by storing the same into the variable y.
3. Multiply x and y to get the product.
4. Print the product.

filename: sample_multiply.py

code

x = int(input("Enter the first number? "))
y = int(input("Enter the second number? "))
product = x * y
print(product)

You can copy paste the code above and try to run it on your favorite IDE, I use visual studio code.

run result

We get two numbers, multiply them to get the product and print the result. Our pseudocode has 4 tasks while our code has 4 statements. Each statement in the code corresponds to each task in pseudocode. Statements in the code are executed in sequence from line 1 down to line 4.

C. What is a Sum

Sum or total is the result when we add two or more numbers. If we add 1 and 1 the sum is 2. If we add 10, 20 and 30, the sum is 60.

total_1 = 1 + 1
total_2 = 10 + 20 + 30

D. What is a Remainder

A remainder is a number that is left after performing a division without applying decimal calculation. It is also a division process where the quotient or result is a whole number meaning the remainder is zero. Being able to calculate the remainder is crucial in determining if a number is divisible by four or not.

Here are some examples.

Our basic formula is:

quotient = dividend / divisor

Formula with remainder and without decimal point.

quotient = (dividend / divisor) + (remainder / divisor)

1. The hard way

Here we will use the long division process.

a. Calculate the remainder if the dividend is 12 and the divisor is 4

      (Explanation)
   3
 ---
4)12
  12  (3 x 4 = 12)
 ---
   0  (12 - 12 = 0, stop here since 0 is less than 4)  <-- remainder

The remainder is zero.

So 12 / 4 = 3 + 0/4 or 3 remainder 0.

b. Calculate the remainder if the dividend is 20 and the divisor is 8

      (Explanation)
   2
 --- 
8)20
  16  (2 x 8 = 16)
 ---
   4  (20 - 16 = 4, stop here since 4 is less than 8)  <-- remainder

So 20 / 8 = 2 + 4/8 or 2 remainder 4.

In this example the remainder is 4.

c. Calculate the remainder if the dividend is 5 and the divisor is 8

      (Explanation)
  0
 --- 
8)5
  0  (0 x 8 = 0)
 ---
  5  (5 - 0 = 5, stop here since 5 is less than 8)

The remainder is 5.

2. The easy way

Python has two easy ways to get the remainder.

  1. Use the modulo operator %
  2. Use the divmod built-in function.

a. Use modulo operator %

Find the remainder if the first number is 874 and the second number is 4?

code

remainder = 874 % 4
print(f'The remainder is {remainder}.')

output

The remainder is 2.

b. Use divmod built-in function

The divmod() takes two parameters. The first is the dividend and the second is the divisor. It returns a tuple of two numbers, the first is the quotient from integer division and the second number is the remainder.

divmod(a, b)

Given a=100 and b=3 find the quotient and the remainder.

filename: mydivmod.py

code

a = 100
b = 3
quotient, remainder = divmod(a, b)
print(f'a: {a}, b: {b}, quotient: {quotient}')
print(f'a: {a}, b: {b}, remainder: {remainder}')

run result

You may continue reading (part 2/2).

If you read this far, tweet to the author to show them you care. Tweet a Thanks

Terrific post, Ferdy

More Posts

Efficient Approach to Calculate the Sum of Numbers Divisible by 4 in Python (Part 2: Full Code Implementation)

Ferdy - Mar 17

How to fix the Taberror: inconsistent use of tabs and spaces

prince yadav - Sep 12, 2023

How to Fix the TypeError: cannot use a string pattern on a bytes-like object Error in Python

Cornel Chirchir - Oct 29, 2023

How to read a file and search specific word locations in Python

Brando - Nov 8, 2023

Python on the web - High cost of synchronous uWSGI

wewake - Apr 30, 2024
chevron_left