Algorithm Examples # 2
1. Algorithm to Check if a Number is Even or Odd:
1. Start
2. Input a number 'num'.
3. If num % 2 equals 0, then:
1. Print "The number is even."
4. Else:
1. Print "The number is odd."
5. End
Explanation:
1. The algorithm starts.
2. It takes an input, which is the number 'num' you want to check.
3. It uses the modulo operator (%) to check the remainder when 'num' is
divided by 2. If the remainder is 0, it means the number is even;
otherwise, it's odd.
4. Depending on the result of the modulo operation, it prints the
appropriate message.
5. The algorithm ends.
2. Algorithm to Calculate Student Grade:
1. Start
2. Input student's score 'score'.
3. If score is greater than or equal to 90, then:
1. Set 'grade' to 'A+'.
4. Else if score is greater than or equal to 80, then:
1. Set 'grade' to 'A'.
5. Else if score is greater than or equal to 70, then:
1. Set 'grade' to 'B'.
6. Else if score is greater than or equal to 60, then:
1. Set 'grade' to 'C'.
7. Else if score is greater than or equal to 50, then:
1. Set 'grade' to 'D'.
8. Else:
1. Set 'grade' to 'F'.
9. Print "Student's grade is " followed by the 'grade'.
10. End
Explanation:
1. The algorithm starts.
2. It takes an input, which is the student's score.
3. It uses conditional statements to determine the grade based on the
score.
4. If the score is 90 or above, the student receives an 'A+' grade.
5. If the score is between 80 and 89, the student receives an 'A' grade,
and so on.
6. If the score doesn't fall into any of the above categories, the student
receives an 'F' grade.
7. It then prints the calculated grade.
8. The algorithm ends.
3. Algorithm to Calculate the Factorial of a Number:
1. Start
2. Input a positive integer 'n'.
3. Set 'factorial' to 1.
4. Set 'i' to 1.
5. While 'i' is less than or equal to 'n', do the following:
1. Set 'factorial' to 'factorial' multiplied by 'i'.
2. Increment 'i' by 1.
6. Print "The factorial of" concatenated with 'n' concatenated with " is "
concatenated with 'factorial'.
7. End
Explanation:
1. The algorithm starts.
2. It takes an input, which is a positive integer 'n' for which you want to
calculate the factorial.
3. It initializes 'factorial' to 1 to store the factorial value.
4. It initializes 'i' to 1, which will be used as a counter.
5. It enters a loop that runs while 'i' is less than or equal to 'n'.
a. Inside the loop, it calculates the 'factorial' by multiplying the
current 'factorial' value with 'i'.
b. It increments 'i' by 1 to move to the next number.
6. After the loop, it prints the factorial of the specified 'n'.
7. The algorithm ends.
4. Algorithm to Find the Sum of Even Numbers:
1. Start
2. Input a positive integer 'limit'.
3. Set 'sum' to 0.
4. For 'num' from 1 to 'limit', do the following:
1. If 'num' is even, then:
1. Add 'num' to 'sum'.
5. Print "The sum of even numbers from 1 to" concatenated with 'limit'
concatenated with " is " concatenated with 'sum'.
6. End
Explanation:
1. The algorithm starts.
2. It takes an input, which is a positive integer 'limit' defining the range of
numbers to consider.
3. It initializes 'sum' to 0 to store the sum of even numbers.
4. It enters a loop that iterates from 1 to 'limit'. a. Inside the loop, it
checks if 'num' is even (divisible by 2). b. If 'num' is even, it adds 'num'
to the 'sum'.
5. After the loop, it prints the sum of even numbers from 1 to the
specified 'limit'.
6. The algorithm ends.
5. Algorithm to Find the Greatest Number
1. Start
2. Input three numbers: 'num1', 'num2', and 'num3'.
3. Initialize 'greatest' to 0 (a default value).
4. Check if 'num1' is greater than 'num2':
1. If true, then:
- Check if 'num1' is greater than 'num3':
- If true, set 'greatest' to 'num1'.
- Otherwise, set 'greatest' to 'num3'.
2. If 'num1' is not greater than 'num2', check if 'num2' is greater than
'num3':
- If true, set 'greatest' to 'num2'.
- Otherwise, set 'greatest' to 'num3'.
5. Print "The greatest number among" concatenated with 'num1',
'num2', and 'num3' concatenated with " is " concatenated with
'greatest'.
6. End
Explanation:
1. The algorithm starts.
2. It takes inputs for three numbers: 'num1', 'num2', and 'num3'.
3. 'greatest' is initialized to 0 as a default value.
4. It begins a series of nested if-else statements to compare the
numbers:
First, it checks if 'num1' is greater than 'num2'. If true, it further
checks if 'num1' is greater than 'num3'. If both conditions are
met, 'greatest' is set to 'num1'. Otherwise, 'greatest' is set to
'num3'.
If 'num1' is not greater than 'num2', it checks if 'num2' is greater
than 'num3'. If true, 'greatest' is set to 'num2'.
If none of the above conditions are met, 'greatest' is already set
to 'num3'.
5. The algorithm prints the result, indicating the greatest number among
the three input numbers.
6. The algorithm ends.
6. Algorithm to Find HCF (GCD) of Two Numbers:
1. Start with the two input numbers, num1 and num2.
2. Initialize two variables, a and b
3. Set a to be the larger of the two numbers (num1 or num2).
4. Set b to be the smaller of the two numbers.
5. While b is not zero, do the following:
a. Calculate the remainder r when dividing a by b (i.e., r = a %
b).
b. Set a to b and b to r.
6. When b becomes zero, the current value of a is the HCF (GCD) of
num1 and `num2.