##### Categories # Algorithm

## How to write an algorithm

``Step 1  Identify  input``
``An algorithm may need some data (input) to process and produce the outcome. ``
``Step 2 Define the variables``
``Variable are placeholder that holds values required to process. Define variables that hold the data for later use.``
``Step 3 Outline the steps: ``
``Mention the step (s) using the sequence of steps, branching and loops constructors``
``Step 4 Output the results:``
````Display or  return the desired outcome.`` ​​​​​​​`
```

​​​​​​​​​​​​​​Example 1:  Algorithm add two numbers

`` ``Step 1: Start``
`` Step 2: Declare variables number1, number2 and sum. ``
`` Step 3: Read values number1 and number2.``
`` Step 4: Add number1 and number2 and assign the result to sum``
```` Step 5: Display sum`
```

## Example 2:  Algorithm finds if a number is divisible by 7

`` Step 1: Start``
`` Step 2: Declare variables number1``
`` Step 3: Read value number1 ``
`` Step 4: Check number1 mod 7  is equal to 0 go to step 5 otherwise go    to step 6``
`` Step 5: Display number1 is divisible by 7``
`` Step 6: Stop``

## Example 3:  Algorithm  sum of n natural numbers

`` Step 1: Start``
`` Step 2: Declare variables stop, sum =0, start = 1``
`` Step 3: Ask user to input the stop value``
`` Step 4: stop = stop value``
`` Step 5: If start is equal to stop  go to step 8``
`` Step 6: Add start to sum``
`` Step 7: Add start to 1``
`` Step 8: Print sum`` 