Ruby conditional statements – Find output programs (set 3)

This section contains the Ruby conditional statements find output programs (set 3) with their output and explanations.
Submitted by Nidhi, on February 11, 2022

Program 1:

num1 = 10;
num2 = 20;
num3 = 30;
large = 0;

if (num1 > num2)
  if (num1 > num3)
    large = num1;
  else
    large = num3;
end
elsif(num2 > num1)
if (num2 > num3)
  large = num2;
else
  large = num3;
end
else
  large = num3;
end

print("Largest value is : ", large);

Output:

Largest value is : 30

Explanation:

In the above program, we created num1, num2, num3, large that are initialized with 10, 20, 30, 0 respectively. Then we found the largest number among 3 numbers using nested if and printed the largest number.


Program 2:

num1 = -10;

if (num1 < 0) == true
  print("Number is NEGATIVE.");
else
  print("Number is POSITIVE.");
end

Output:

Number is NEGATIVE.

Explanation:

In the above program, we created a variable num1, which is initialized with -10. Then we checked num is POSITIVE or NEGATIVE using the if statement and printed the appropriate message.

Ruby Find Output Programs »





Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.