Hello dear readers of my blog! In programming (in any programming language), it is very important to create the algorithm of the program you want to create in advance, so that it becomes easier to code the program, that is, to write code for the program in any programming language.

This article is also about writing an algorithm for the program, that is, we will consider some stages of creating a suitable algorithm for the program in harmony with examples.

ATTENTION ADVERTISEMENT!A video course on Java programming languages is ready. You can read information about the video course by clicking on the image below:

I would like to remind you that as we begin to learn how to create an algorithm, in the previous article we got a little familiar with data structures and algorithms. Click on this link to go to the article!

Algorithm and its features were mentioned in the previous article, but I would like to make a note here.

## About the algorithm!

An algorithm is a set (collection) of step-by-step instructions used in mathematics and computer science. Computer programmers use algorithms to perform simple tasks and calculations. Even the most complex computer programs are built on the basis of the composition of the source of algorithms.

Several algorithms can be devised to solve a problem, but the best of them, i.e. the one needed by the programmer, can be determined according to the following main qualities.

## The quality of a good algorithm

- Input and output (result) must be correctly defined.
- Each step in the algorithm should be clear and unambiguous.
- The algorithm should be the most effective among many different solutions to solve the problem.
- The algorithm should not involve computer code. Instead, the algorithm should be written in such a way that it can be used in similar programming languages.

## Creating an algorithm!

Let's take an example of a real-life situation for writing an algorithm, and then we will dwell on the appearance of more specific steps. Below is an algorithm for going to the market when buying a pen.

- Getting dressed to go to the market.
- Checking whether there is money in the wallet.
- If there is no money in the wallet, filling it.
- Going to the market.
- Asking for a pen brand you like.
- If the pen is not available, go to step 10, otherwise go to step 7.
- Giving money to the shopkeeper.
- Put the purchased pen securely in your pocket.
- Return home.
- Asking for a pen of another brand.
- Go to step 7.

The above algorithm can be expanded by adding various tasks, for example, a completely separate algorithm can be created for the 4th step.

Now, if we start creating an algorithm, we will go through the following steps.

**1 – Step.** Determine the input of the algorithm! Many algorithms receive data to process. This information can be in the form of any unit of measurement of computer data.

Many programming languages ask you to specify how much input you can accept and what type of data that input is. For example, if you are writing an algorithm to calculate the area of a square, your input might be the width and height of the square.

**2 – Stage.** Identify the variables! Algorithm variables allow the same algorithm to be used for more equations than just one equation.

Again, using the square above as an example, creating two variables as inputs for the width and height of the square will make that one algorithm fit any square.

In other words, you can use this algorithm to determine the face of any square by inputting its width and height.

**3 – Stage.** Draw a pattern of algorithm operations! This changes the value of the input variables. Using the above example again, the algorithm increases the width and height of the input variables, this is a simple example.

In general, the operations of the algorithm can take the form of many steps, depending on the values and number of input variables.

**4 – Step.** Display the results of your algorithm's operations. For example, if the variables width and height of a square take the values 2 and 3, respectively, the algorithm outputs the value 6.

Examples of algorithms in programming:

- Write an algorithm to add two numbers entered by the user!

Step 1: Start

Step 2: Declare the variables number1, number2 and sum!

Step 3: Read the values of number1 and number2!

Step 4: Add the values of number1 and number2 together and set the result equal to the sum variable!

sum <- number1+number2Step 5: sum up (show)!

Step 6: Stop!

2. Write an algorithm to find the largest of three numbers entered by .

Step 1: Start

Step 2: Declare the variables a, b, and c.

Step 3: Read the variables a, b, and c.

Step 4: If a> b

If a > c

show the largest number a.

Otherwise

c is the largest number.

Otherwise

If b > c

b is the largest number.

Otherwise

c is the largest number.Step 5: Stop.

- Write an algorithm to find the factorization of numbers entered by the user.

Step 1: Start

Step 2: Declare the variables n, factorial, and i.

Step 3: Initialize the variables, give them an initial value

factorial ← 1

i ← 1

Step 4: Read the value of n

Step 5: Repeat the steps until i = n

5.1: factorial ← factorial * i

5.2: i ← i + 1

Step 6: Display the factorial

Step 7: Stop!

- Write an algorithm to check whether the number entered by the user is prime or not.

Step 1: Start

Step 2: Declare n, i, flag variables.

Step 3: Initialize the variables, assign a value

flag ←1

i ← 2

Step 4: Read the variable n from the user.

Step 5: Repeat until i <(n / 2).

5.1 If the remainder of n ÷ i is equal to 0

flag ← 0

Go to step 6

5.2 i ← i + 1

Step 6: if flag = 0

Show that n is not a prime number.

Otherwise

Show that n is a prime number!

Step 7: Stop

So, the algorithm is written in the following forms. An algorithm is not some kind of programming code. Algorithms are guidelines that give you specific ideas for writing code.

In this article, I wanted to give information about the algorithm and its writing. If you have any questions about the article, leave them in the comments!

Wishing you health and safety, I bid you farewell.