Selection and Iteration Structures

565 viewsG11-01. Programming
0

  1. Write a program in pseudo code to categorize a person’s BMI (Body Mass Index) as Underweight (<18.5), Normal (18.5-24.9), or Overweight (≥25).

  2. Write a switch/case pseudo code to display a message based on a day number (1=Monday, 2=Tuesday, …, 7=Sunday).

  3. What is the difference between a while loop and a do-while loop?

  4. Optimize a loop to calculate the factorial of a number.

  5. Write pseudo code to sum digits of a number until a single digit is obtained.

Ruwan Suraweera Changed status to publish
0

Answers and Descriptions

  1. Answer:

INPUT bmi
IF bmi < 18.5 THEN
    OUTPUT "Underweight"
ELSE IF bmi < 25 THEN
    OUTPUT "Normal"
ELSE
    OUTPUT "Overweight"
ENDIF

Description: Nested If-Else statements handle multiple BMI ranges, testing complex selection logic for real-world applications.

  1. Answer:

INPUT day
SWITCH day
    CASE 1: OUTPUT "Monday"
    CASE 2: OUTPUT "Tuesday"
    CASE 3: OUTPUT "Wednesday"
    CASE 4: OUTPUT "Thursday"
    CASE 5: OUTPUT "Friday"
    CASE 6: OUTPUT "Saturday"
    CASE 7: OUTPUT "Sunday"
    DEFAULT: OUTPUT "Invalid day"
ENDSWITCH

Description: Switch/case maps a single variable to multiple outcomes, ideal for enumerated values like days, enhancing code clarity.

  1. Answer: A while loop checks the condition before executing, while a do-while loop executes at least once before checking the condition.
    Description: While loops may skip execution if the condition is false, whereas do-while loops guarantee one execution, useful for menu-driven programs.

  2. Answer:

INPUT n
factorial = 1
FOR i = 1 TO n
    factorial = factorial * i
ENDFOR
OUTPUT factorial

Description: This loop uses a single variable to accumulate the product, demonstrating efficient iteration for factorial computation.

  1. Answer:

INPUT number
WHILE number > 9
    sum = 0
    WHILE number > 0
        sum = sum + (number MOD 10)
        number = number DIV 10
    ENDWHILE
    number = sum
ENDWHILE
OUTPUT number

Description: This while loop handles unknown iterations, summing digits until a single digit remains, testing advanced iteration logic.

Ruwan Suraweera Changed status to publish
🔴 Lesson List
SIDE BUTTON ON
Sign In Register
×

👋 Welcome Back!

🚀
Ready to Learn?Pick up where you left off.
🔑
Forgot Password?Click the link below to reset.
📢
New FeaturesCheck out the new AI Voice tool.

Sign In

👤
🔒
or continue with
Google Facebook

Already have an account? Register Now