SimoCode

ArmstrongNumbers in Ruby

Feb 23rd, 2018
154
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 0.60 KB | None | 0 0
  1. def armstrongNumber n
  2.   a = n.to_i
  3.   n = n.to_s
  4.   digits = n.length
  5.   array = Array.new
  6.   n.each_char {|c| array.push(c.to_i)}
  7.   array_cubo = array.map do |valore|
  8.     (valore.to_i)**digits
  9.   end
  10.   sum = 0
  11.   array_cubo.each { |a| sum+=a }
  12.   if sum == a
  13.     puts "#{sum} is an Armstrong number."
  14.   end
  15. end
  16.  
  17. puts 'Please insert an integer'
  18. num = gets.chomp.to_i
  19. armstrongNumber num
  20.  
  21. puts " "
  22. puts 'Display all Armstrong numbers in a range.'
  23. puts 'Insert the last number of the range'
  24. last = gets.chomp.to_i
  25. first = 0
  26. while first <= last
  27.   armstrongNumber first
  28.   first = first + 1
  29. end
Advertisement
Add Comment
Please, Sign In to add comment