TheAlgorithms-Ruby/maths/add.rb

21 lines
419 B
Ruby
Raw Normal View History

2021-04-10 21:06:28 +02:00
# A ruby program to add numbers
# Addition or sum of numbers means adding each and every element of the inputs
# Sum or addition of 1 and 3 is 1 + 3 = 4
2021-04-15 01:29:37 +02:00
def add(*array)
2021-04-10 21:06:28 +02:00
sum = 0
array.each { |a| sum+=a }
puts "The sum of following elements #{array} is #{sum}"
rescue
puts "Error: Please provide number only!"
end
# Valid inputs
2021-04-15 01:29:37 +02:00
add(1)
add(2, 5, -4)
add(25, 45)
2021-04-10 21:06:28 +02:00
# Invalid inputs
2021-04-15 01:29:37 +02:00
add("1", 2, 3)
add("a", 1)