Write an algorithm that accepts four numbers as input and find the largest and smallest of them.
Question
Write an algorithm that accepts four numbers as input and find the largest and smallest of them.
✨ Free AI Tools for You
Powered by Chatterbot AI
Answer
Here is the algorithm to find the largest and smallest of four numbers:
Pseudocode:
INPUT num1, num2, num3, num4
SET largest = num1
SET smallest = num1
IF num2 > largest THEN
SET largest = num2
IF num3 > largest THEN
SET largest = num3
IF num4 > largest THEN
SET largest = num4
IF num2 < smallest THEN
SET smallest = num2
IF num3 < smallest THEN
SET smallest = num3
IF num4 < smallest THEN
SET smallest = num4
PRINT "Largest number is ", largest
PRINT "Smallest number is ", smallest
Explanation:
Input: Read four numbers (num1, num2, num3, num4).
Initialize:
Set 'largest' variable to the first number (num1).
Set 'smallest' variable to the first number (num1).
Finding the Largest:
Compare num2 with 'largest' and update 'largest' if num2 is greater.
Compare num3 with 'largest' and update 'largest' if num3 is greater.
Compare num4 with 'largest' and update 'largest' if num4 is greater.
Finding the Smallest:
Compare num2 with 'smallest' and update 'smallest' if num2 is smaller.
Compare num3 with 'smallest' and update 'smallest' if num3 is smaller.
Compare num4 with 'smallest' and update 'smallest' if num4 is smaller.
Output: Print the 'largest' and 'smallest' values.
India's 1st AI Doubt Solver for CBSE, JEE, and NEET
Ask a Question for Freeand then it's just ₹212 a month