# ๐ What are algorithms

## To get a computer to do anything, you must write a program for it. To write a computer program, you need to tell the computer step by step exactly what you want it to do. The computer then executes the program, following the steps mechanically until it reaches the end.

To get a computer to do anything, you must write a program for it. To write a computer program, you need to tell the computer step by step exactly what you want it to do. The computer then executes the program, following the steps mechanically until it reaches the end.

When you tell the computer what to do, you must choose the method by which the task should be done, and this is called an algorithm.

## What Are Algorithms?

An algorithm is a set of sequential mathematical and logical steps needed to solve a problem. The term "algorithm" is named after the Muslim mathematician **Abu Ja'far Muhammad ibn Musa Al-Khwarizmi**, who invented it in the 9th century AD.

## How Do We Understand Algorithms?

To understand how an algorithm works, letโs imagine you have a friend who has just arrived at the airport and wants to travel from the airport to your home. There are four algorithms you could give your friend to reach your home! Letโs start:

### Taxi Algorithm

- Stop a taxi.
- Get in the taxi.
- Give the driver the address.

### Call Me Algorithm

- When the plane lands, call me on my cell phone.
- Meet me outside the airport arrival hall.

### Car Rental Algorithm

- Go to the car rental area.
- Rent a car.
- Follow the GPS to my house.

### Bus Algorithm

- Exit the airport arrival hall.
- Take bus number 70.
- Transfer to bus number 14 on the main road.
- Get off at Al-Zuhur Street.
- Walk left and pass two houses to find my home.

## How Do We Use Algorithms in Computing?

Each of these algorithms will get your friend to your home, whether by taxi, calling you, renting a car, or even taking the bus. This helps us understand that an algorithm is a way of providing multiple solutions to a problem in a sequential and organized manner to make problem-solving easier.

We use these algorithms in computer programming. In the following example, hereโs a code written in the popular C++ language:

```
N = 2
while (N < 1000000)
{
if (N == 2)
print N
N = N + 1
}
```

In this code, there is a simple algorithm that starts by defining the variable `N`

, and it tells us that this variable is set to the numeric value 2. Then, the algorithm moves us to another stage, which is the loop stage, specifically a `while loop`

. This loop repeats as long as the `while`

condition is true. What is inside the `while loop`

is what will be repeated.

Inside the `while loop`

, there is an `if`

condition that says if `N == 2`

, perform the following operation:

- Print the value of
`N`

(which is 2 in this case). - Add 1 to
`N`

(so it becomes 3 in this case). - Repeat the operation indefinitely!

What we expect from this operation is that the number 3 will be printed because on the next repetition, `N`

will no longer be equal to 2, so the condition will be false, and the repetition will stop.

This way, we have illustrated how an algorithm works in a simple manner.