Write a Ruby program to calculate generic root.
Generic Root: It sum of digits of a number unit we don't get a single digit.
Example:
Generic root of 4563: 4+5+6+3 = 18 since 18 is two digit numbers so 1 + 8 = 9
So, generic root of 4563 = 9
Generic Root: It sum of digits of a number unit we don't get a single digit.
Example:
Generic root of 4563: 4+5+6+3 = 18 since 18 is two digit numbers so 1 + 8 = 9
So, generic root of 4563 = 9
class GenericRoot
def getGenericRoot
sum = 0
rem = 0
print "Enter the number: "
num = gets.chomp.to_i
while num >= 10 do
sum = 0
while num != 0 do
rem = num % 10
num = num / 10
sum += rem
end
(sum >= 10) ? (num = sum) : break
end
print "Generic root of given number is :" + sum.to_s
end
end
p = GenericRoot.new
p.getGenericRoot
Output:
Enter the number: 123456
Generic root of given number is :3
|
No comments:
Post a Comment