Ruby Program to Check Palindrome Number
You can check a palindrome number in Ruby by converting the number to a string and comparing it with its reverse using
num.to_s == num.to_s.reverse.Examples
Input121
Output121 is a palindrome number.
Input123
Output123 is not a palindrome number.
Input0
Output0 is a palindrome number.
How to Think About It
To check if a number is a palindrome, think of it like reading a word forwards and backwards. Convert the number to a string, then compare the string with its reverse. If both are the same, the number is a palindrome.
Algorithm
1
Get the input number.2
Convert the number to a string.3
Reverse the string.4
Compare the original string with the reversed string.5
If they are equal, the number is a palindrome; otherwise, it is not.6
Print the result.Code
ruby
puts "Enter a number:" num = gets.chomp if num == num.reverse puts "#{num} is a palindrome number." else puts "#{num} is not a palindrome number." end
Output
Enter a number:
121
121 is a palindrome number.
Dry Run
Let's trace the input 121 through the code
1
Input number
num = "121"
2
Reverse string
num.reverse = "121"
3
Compare strings
"121" == "121" is true
4
Print result
Output: "121 is a palindrome number."
| num | num.reverse | Comparison Result |
|---|---|---|
| 121 | 121 | true |
Why This Works
Step 1: Convert number to string
We use gets.chomp to read input as a string so we can easily reverse it.
Step 2: Reverse the string
Using num.reverse creates a reversed copy of the input string.
Step 3: Compare original and reversed
If the original string equals the reversed string, the number reads the same forwards and backwards, so it is a palindrome.
Alternative Approaches
Using integer reversal
ruby
puts "Enter a number:" num = gets.to_i original = num reversed = 0 while num > 0 reversed = reversed * 10 + num % 10 num /= 10 end if original == reversed puts "#{original} is a palindrome number." else puts "#{original} is not a palindrome number." end
This method works with integers directly without converting to string but is more complex.
Using recursion to check palindrome
ruby
def palindrome?(str) return true if str.length <= 1 return false if str[0] != str[-1] palindrome?(str[1..-2]) end puts "Enter a number:" num = gets.chomp if palindrome?(num) puts "#{num} is a palindrome number." else puts "#{num} is not a palindrome number." end
This recursive method checks characters from outside in but is less efficient for large inputs.
Complexity: O(n) time, O(n) space
Time Complexity
Reversing the string takes O(n) time where n is the number of digits, and comparison also takes O(n).
Space Complexity
The reversed string requires O(n) extra space to store the reversed copy.
Which Approach is Fastest?
The string reversal method is simple and fast for typical inputs, while integer reversal avoids extra string space but uses more steps.
| Approach | Time | Space | Best For |
|---|---|---|---|
| String reversal | O(n) | O(n) | Simplicity and readability |
| Integer reversal | O(n) | O(1) | Memory efficiency with numeric operations |
| Recursive check | O(n) | O(n) | Learning recursion, less practical |
Convert the number to a string and compare it with its reverse for a quick palindrome check.
Forgetting to convert the number to a string before reversing causes errors or wrong results.