Showing posts with label Ruby program to check the armstrong number. Show all posts
Showing posts with label Ruby program to check the armstrong number. Show all posts

Ruby program to check the armstrong number



Write a Ruby program to check the armstrong number

Example of armstrong number:
Input - 153
Output - 1^3 + 5^3 + 3^3 = 153, so it is Armstrong no.


class ArmstrongNumber
  def checkArmstrong
    print "Enter the number: "
    input_num = gets.chomp.to_i
    num = input_num
    check = 0 
    while (num > 0) do 
      rem = num % 10 
      num = num / 10 
      check = check + (rem * rem * rem)
    end
    if check == input_num
      print "#{input_num} is an armstrong number"
    else
      print "#{input_num} is not an armstrong number"
    end 
  end
end

p = ArmstrongNumber.new 
p.checkArmstrong

Output:

Enter the number:  371
371 is an armstrong number