Let's take a look at several algorithm examples, while solving various problems.

The procedure is:

- Enter the two numbers in the variables N and M.
- Sum them and save the result in the variable sum.
- Output the result.

Obviously, this is a very simple example. If you are serious about learning algorithms, have a look at the Introduction to algorithms book.

A number is even if it can be divided by 2 without remainder. Such numbers are 2, 4, 6, 8.. and so on. The numbers that leave a remainder are called odd. They are 1, 3, 5, 7.. and so on. In programming we find the remainder of a division with the operator %. Also we use the double equals “==” to compare values for equality. You can read more about operators in the math operators lesson.

Summing two numbers was easy – the
calculation was one block from the flow chart. But how about 50? Do
you have to write 50 blocks to solve this task? Happily – no!

You can automate this process by
repeatedly incrementing the value of a variable and checking it every
time if it exceeds the last value – 50. Then sum that number every
step and... there you go! This construction is called a ** loop**. Learn more
about loops in the lesson from the beginners programming
tutorial.

A very common algorithm example from mathematics is the long division. Rather than a programming algorithm, this is a sequence that you can follow to perform the long division. For this example we will divide 52 by 3.

- Take the most significant digit from the divided number( for 52 this is 5) and divide it by the divider.
- Write the result as as a first digit of the end result.(5 / 3 = 1, so we write 1)
- Multiply the remainder by the weight of the divided digit. ( 5/3 = 1 and a remainder 2. 5 has a weight of 10 in 52, so we have 2 * 10).
- Sum the result from 3. with the next most significant digit and continue with this number from step 1. Repeat the steps until no more digits remain from the divided number.

The result from
3. is 20. The next digit in 52 is 2. So.. 20 + 2 = 22

1. 22 / 3
= 7

2. 17. Since this is the last digit 17 is the final answer.
If you continue the division you will find the fractional part.

Let's go a step higher and continue with some more complex algorithm examples.

Given is the array prices with 100 elements(prices[100]). The problem consists of two parts

- Find the highest price in the array
- Reduce that price

For part 1 we iterate through the whole array, starting with index 0. We compare the first value with the next prices and when a greater price is found, we remember the new value in the variable “max” and its location in “maxIndex”.

Once we compared all elements of the array we have to reduce the max
price with 10%. This is the same as multiplying it by 0.9, so that is
what you see in the algorithm.

The last note here – we use short version of the multiply-assign
operator:

* prices[maxIndex] *= 0.9*

is the same
as* prices[maxIndex] = prices[maxIndex] * 0.9*

The last of the algorithm examples will be more branched. As you will see, we will need to do several consecutive examinations and this will spread our flow chart a bit.

See also:

- Introduction to algorithms - an excellent book for algorithms in programming
- Algorithm definition – definition and explanation
- Algorithms – more advanced algorithms, used in real programming
- Flow chart and Flow chart symbols
- Algorithm complexity and analysis of algorithms

- Home
- Algorithms
- Algorithm Examples