Write a Ruby program to reverse a number
class ReverseNumber
def checkReverse
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
print "Reverse of #{input_num} is #{new_num}"
end
end
p = ReverseNumber.new
p.checkReverse
----------------------------------------------------------------------------
class ReverseNumber
def doReverse
print "Enter the number: "
num = gets.chomp
rev = num.reverse.to_i
print "Reverse of number is #{rev}"
end
end
p = ReverseNumber.new
p.doReverse
Output:
class ReverseNumber
def checkReverse
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
print "Reverse of #{input_num} is #{new_num}"
end
end
p = ReverseNumber.new
p.checkReverse
----------------------------------------------------------------------------
class ReverseNumber
def doReverse
print "Enter the number: "
num = gets.chomp
rev = num.reverse.to_i
print "Reverse of number is #{rev}"
end
end
p = ReverseNumber.new
p.doReverse
Output:
Enter the number: 852
Reverse of number is 258
|
No comments:
Post a Comment