From 1a8dc1debfad7e10719736dafd32edfbc291a393 Mon Sep 17 00:00:00 2001 From: Sahil Afrid Farookhi Date: Sun, 11 Apr 2021 00:36:28 +0530 Subject: [PATCH] add feature implementation --- maths/add.rb | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 maths/add.rb diff --git a/maths/add.rb b/maths/add.rb new file mode 100644 index 0000000..7351c32 --- /dev/null +++ b/maths/add.rb @@ -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)