Ruby program to check Palindrome Number



Write a Ruby program to check Palindrome Number

Palindrome Number: If the number is equal to it's reversed number, then the given number is a palindrome number. For example: 121 is a palindrome number while 122 is not.

class PalindromeNumber
  def checkPalindrom
     print "Enter the number: "
     input_num = gets.chomp.to_i
     num = input_num
     new_num = 0 
     while num > 0 do
       rem = num % 10 
       num = num / 10 
       new_num = (new_num * 10) + rem 
     end
     if input_num == new_num
       print "#{input_num} is a palindrome number"
     else 
       print "#{input_num} is not a palindrome number"
     end 
  end
end

p = PalindromeNumber.new 
p.checkPalindrom

Output:

Enter the number:  1221
1221 is a palindrome number


No comments:

Post a Comment