Algorithm:
- Start
- Initialize a variable
numwith the value 1. - Repeat the following steps until
numis greater than 5:- Print the value of
num. - Increment
numby 1.
- Print the value of
- End
Example in Pseudocode:
BEGIN
num ← 1
WHILE num ≤ 5 DO
PRINT num
num ← num + 1
END WHILE
END
The algorithm to print numbers from 1 to 5 involves iteration and incrementing a variable until a condition is met. Here’s a detailed explanation of each step:
1. Start
- This marks the beginning of the process.
2. Initialize a variable
- We create a variable called
numand set its initial value to1.- This is the starting point of our sequence.
3. Repeat the steps while a condition is true
- Use a loop to repeat a block of code. The loop runs as long as the condition
num ≤ 5is true.- Example: If
num = 1, the condition1 ≤ 5is true, so the loop will execute. - The loop stops when
numbecomes greater than 5.
- Example: If
4. Print the value of num
- Inside the loop, we print the current value of
num.- Example: When
num = 1, it prints1.
- Example: When
5. Increment the value of num
- After printing, we increase the value of
numby 1.- This ensures that the loop moves forward and doesn’t run infinitely.
- Example: After printing
1,numbecomes2.
6. End
- When
numbecomes greater than 5 (i.e., the conditionnum ≤ 5is false), the loop stops, and the program ends.
Example Execution:
| Step | Value of num |
Condition (num ≤ 5) |
Action |
|---|---|---|---|
| 1 | 1 | True | Print 1 |
| 2 | 2 | True | Print 2 |
| 3 | 3 | True | Print 3 |
| 4 | 4 | True | Print 4 |
| 5 | 5 | True | Print 5 |
| 6 | 6 | False | Stop the loop. |
Key Concepts:
- Looping: Repeating a block of code while a condition is true.
- Incrementing: Gradually increasing the variable’s value to eventually stop the loop.
- Condition Checking: Ensuring the loop stops at the right time.

Pingback: Algorithm Vs Program - srifolks