0
0
RubyProgramBeginner · 2 min read

Ruby Program to Reverse a Number with Output and Explanation

You can reverse a number in Ruby by repeatedly extracting its last digit using % 10 and building the reversed number by multiplying the result by 10 and adding the digit, like this: while num > 0; digit = num % 10; reversed = reversed * 10 + digit; num /= 10; end.
📋

Examples

Input12345
Output54321
Input1000
Output1
Input7
Output7
🧠

How to Think About It

To reverse a number, think of peeling off the last digit one by one using the remainder operator %. Then, build a new number by shifting the current reversed number left (multiply by 10) and adding the peeled digit. Repeat until the original number is fully processed.
📐

Algorithm

1
Get the input number.
2
Initialize a variable to store the reversed number as 0.
3
While the input number is greater than 0:
4
Extract the last digit using modulus 10.
5
Multiply the reversed number by 10 and add the extracted digit.
6
Remove the last digit from the input number by dividing by 10 using integer division.
7
Return the reversed number.
💻

Code

ruby
puts "Enter a number:"
num = gets.to_i
reversed = 0
while num > 0
  digit = num % 10
  reversed = reversed * 10 + digit
  num /= 10
end
puts "Reversed number: #{reversed}"
Output
Enter a number: 12345 Reversed number: 54321
🔍

Dry Run

Let's trace the number 123 through the code to see how it reverses.

1

Initial values

num = 123, reversed = 0

2

First iteration

digit = 123 % 10 = 3; reversed = 0 * 10 + 3 = 3; num = 123 / 10 = 12

3

Second iteration

digit = 12 % 10 = 2; reversed = 3 * 10 + 2 = 32; num = 12 / 10 = 1

4

Third iteration

digit = 1 % 10 = 1; reversed = 32 * 10 + 1 = 321; num = 1 / 10 = 0

5

Loop ends

num is now 0, loop stops; reversed number is 321

numdigitreversed
12333
12232
11321
💡

Why This Works

Step 1: Extract last digit

Using num % 10 gets the last digit of the number.

Step 2: Build reversed number

Multiply the current reversed number by 10 to shift digits left, then add the extracted digit.

Step 3: Remove last digit

Divide the original number by 10 using integer division to remove the last digit.

🔄

Alternative Approaches

String conversion
ruby
puts "Enter a number:"
num = gets.chomp
reversed = num.reverse
puts "Reversed number: #{reversed.to_i}"
This method is simpler and uses Ruby's string reverse, but it converts numbers to strings and back, which may be less efficient for large numbers.
Recursive method
ruby
def reverse_num(num, rev=0)
  return rev if num == 0
  reverse_num(num / 10, rev * 10 + num % 10)
end
puts "Enter a number:"
num = gets.to_i
puts "Reversed number: #{reverse_num(num)}"
Uses recursion to reverse the number, which is elegant but may cause stack overflow for very large numbers.

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

Time Complexity

The loop runs once for each digit in the number, so time grows linearly with the number of digits, O(d).

Space Complexity

Only a few variables are used regardless of input size, so space complexity is O(1).

Which Approach is Fastest?

The arithmetic method is fastest and uses constant space. String conversion is simpler but slower and uses extra space.

ApproachTimeSpaceBest For
Arithmetic loopO(d)O(1)Performance and memory efficiency
String conversionO(d)O(d)Simplicity and quick coding
Recursive methodO(d)O(d)Elegant code but limited by recursion depth
💡
Use integer division and modulus to peel digits one by one when reversing a number without converting to string.
⚠️
Beginners often forget to update the original number inside the loop, causing an infinite loop.