0
0
RubyProgramBeginner · 2 min read

Ruby Program to Find Largest of Three Numbers

You can find the largest of three numbers in Ruby using max = [a, b, c].max or by comparing them with if statements like if a > b && a > c then max = a.
📋

Examples

Inputa=3, b=7, c=5
Output7
Inputa=10, b=10, c=2
Output10
Inputa=-1, b=-5, c=-3
Output-1
🧠

How to Think About It

To find the largest number among three, compare each number with the others using greater than checks. The number that is greater than both others is the largest.
📐

Algorithm

1
Get the three numbers as input.
2
Compare the first number with the second and third.
3
If the first is greater than both, it is the largest.
4
Otherwise, compare the second with the third.
5
The greater between second and third is the largest.
6
Return or print the largest number.
💻

Code

ruby
puts "Enter three numbers:"
a = gets.to_i
b = gets.to_i
c = gets.to_i

if a > b && a > c
  max = a
elsif b > c
  max = b
else
  max = c
end

puts "The largest number is #{max}"
Output
Enter three numbers: 3 7 5 The largest number is 7
🔍

Dry Run

Let's trace the input a=3, b=7, c=5 through the code

1

Input numbers

a=3, b=7, c=5

2

Check if a > b and a > c

3 > 7 is false, so condition fails

3

Check if b > c

7 > 5 is true, so max = b = 7

4

Print result

The largest number is 7

StepConditionResultmax
1a > b && a > c3 > 7 && 3 > 5 = falsenil
2b > c7 > 5 = true7
3Print max-7
💡

Why This Works

Step 1: Compare first number

We check if the first number is greater than both others using a > b && a > c to quickly find if it's the largest.

Step 2: Compare second and third

If the first is not largest, we compare the second and third numbers with b > c to find which is bigger.

Step 3: Assign and print largest

The largest number is assigned to max and printed using string interpolation for clear output.

🔄

Alternative Approaches

Using Array max method
ruby
puts "Enter three numbers:"
a = gets.to_i
b = gets.to_i
c = gets.to_i
max = [a, b, c].max
puts "The largest number is #{max}"
This method is shorter and uses Ruby's built-in <code>max</code> method for arrays, making code cleaner and easier to read.
Using nested if-else
ruby
puts "Enter three numbers:"
a = gets.to_i
b = gets.to_i
c = gets.to_i

if a >= b
  if a >= c
    max = a
  else
    max = c
  end
else
  if b >= c
    max = b
  else
    max = c
  end
end

puts "The largest number is #{max}"
This approach uses nested conditions to carefully check each comparison, which can be clearer for beginners but longer.

Complexity: O(1) time, O(1) space

Time Complexity

The program does a fixed number of comparisons (at most 3), so it runs in constant time, O(1).

Space Complexity

Only a few variables are used to store inputs and the result, so space complexity is O(1).

Which Approach is Fastest?

All approaches run in constant time, but using the array max method is more concise and idiomatic in Ruby.

ApproachTimeSpaceBest For
If-else comparisonsO(1)O(1)Clear logic for beginners
Array max methodO(1)O(1)Concise and idiomatic Ruby code
Nested if-elseO(1)O(1)Step-by-step comparison clarity
💡
Use Ruby's built-in max method on an array for a simple and clean solution.
⚠️
Beginners often forget to use gets.to_i to convert input to numbers, causing string comparison errors.