Q:

Ruby conditional statements – Find output programs (set 3)

0

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);

Program 2:

num1 = -10;

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

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Answer Program 1:

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.

Answer Program 2:

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.

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now