Program 1:
case 20
when 10
puts "TEN";
when 20
puts "TWENTY";
when 30
puts "THIRTY";
when 40
puts "FORTY";
when 50
puts "FIFTY";
else
puts "Unknown number";
end
Program 2:
case (40-10)
when 10
puts "TEN";
when 20
puts "TWENTY";
when 30
puts "THIRTY";
when 40
puts "FORTY";
when 50
puts "FIFTY";
else
puts "Unknown number";
end
Program 3:
case 40
when 80-10
puts "SEVENTY";
when 80-20
puts "SIXTY";
when 80-30
puts "FIFTY";
when 80-40
puts "FORTY";
when 80-50
puts "THIRTY";
else
puts "Unknown number";
end
Program 4
case 40
else
puts "Unknown number";
when 10
puts "TEN";
when 20
puts "TWENTY";
when 30
puts "THIRTY";
when 40
puts "FORTY";
when 50
puts "FIFTY";
end
Program 5:
case 43
when 11..20
puts "Number is within the range of 11 to 20";
when 21..30
puts "Number is within the range of 21 to 30";
when 31..40
puts "Number is within the range of 31 to 40";
when 41..50
puts "Number is within the range of 41 to 50";
else
puts "Unknown number";
end
Answer Program 1:
Output:
Explanation:
In the above program, we created case blocks. Here, the case 20 matched based on the given value and printed the "TWENTY".
Answer Program 2:
Output:
Explanation:
In the above program, we created case blocks. Here case 30 matched based on the given value and printed the " THIRTY".
Answer Program 3:
Output:
Explanation:
In the above program, we created case blocks. Here, the case 40 matched based on the given value and printed the "FORTY".
Answer Program 4:
Output:
Explanation:
The above program will generate errors because we can use "else" at the end of the "case" statement only. The correct program is given below:
Answer Program 5:
Output:
Explanation:
In the above program, we created case blocks. Here, the case 41..50 matched the given number with range, and print the appropriate message.
need an explanation for this answer? contact us directly to get an explanation for this answer