Write an algorithm that performs the following: Ask a user to enter a number. If the number is between 5 and 15, write the word GREEN. If the number is between 15 and 25, write the word BLUE. If the number is between 25 and 35, write the word ORANGE. If it is any other number, write that ALL COLOURS ARE BEAUTIFUL.
To solve this problem, we design an algorithm that classifies a user-provided number into specific colour categories based on its value. The algorithm relies on conditional branching to determine which output to display. We will name this algorithm Colour_Range_Algo.
The process begins with the input phase. The algorithm prompts the user to enter a value. We use the INPUT command to store this value in a variable named Number. This variable is crucial as it is evaluated in the subsequent steps.
The core logic involves checking the range of the number. We use an IF statement to check if the number is greater than 5 and less than 15. If this condition evaluates to true, the algorithm outputs "GREEN". If false, the flow moves to the ELSE IF block.
In the next step, the algorithm checks if the number is between 15 and 25. If true, it prints "BLUE". If not, it checks the third condition: whether the number is between 25 and 35. If this condition holds true, the output is "ORANGE". These checks are performed sequentially using relational operators.
If the number does not fall into any of the above ranges (for example, it is less than 5 or greater than 35), the ELSE statement executes. This default case prints "ALL COLOURS ARE BEAUTIFUL". This structure ensures that every possible input receives a defined output.
Explanation
The answer constructs the algorithm as requested in Question 9 of the textbook context. It follows the structure of similar algorithms provided in the context (such as Question 17 'Classify_Numbers_Algo' and Question 18 'Accept_1to100_Algo'), using standard INPUT, IF, ELSE IF, and ELSE statements. The logic strictly adheres to the ranges specified in the question prompt.
Solution Steps
Step 1: Define the algorithm name and take a number as INPUT from the user.
Step 2: Use an IF statement to check if the number is between 5 and 15; if true, print 'GREEN'.
Step 3: Use ELSE IF to check if the number is between 15 and 25; if true, print 'BLUE'.
Step 4: Use another ELSE IF to check if the number is between 25 and 35; if true, print 'ORANGE'.
Step 5: Use an ELSE block to handle all other numbers and print 'ALL COLOURS ARE BEAUTIFUL'.