mirror of
https://github.com/TheAlgorithms/Ruby
synced 2025-01-13 08:01:03 +01:00
add feature implementation
This commit is contained in:
parent
b1b6f80a0b
commit
1a8dc1debf
1 changed files with 20 additions and 0 deletions
20
maths/add.rb
Normal file
20
maths/add.rb
Normal file
|
@ -0,0 +1,20 @@
|
|||
# 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
|
||||
|
||||
def self_add(*array)
|
||||
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
|
||||
self_add(1)
|
||||
self_add(2, 5, -4)
|
||||
self_add(25, 45)
|
||||
|
||||
# Invalid inputs
|
||||
self_add("1", 2, 3)
|
||||
self_add("a", 1)
|
Loading…
Reference in a new issue