Write a Ruby program to compute the sum of elements in a given array.
nums = [12, 34, 23, 56] print "Original array:\n" print nums print "\nSum of the values of the above array:\n" print nums.inject(0){|sum,x| sum + x }
Output:
Original array: [12, 34, 23, 56] Sum of the values of the above array: 125
total answers (1)
start bookmarking useful questions and collections and save it into your own study-lists, login now to start creating your own collections.
Output:
need an explanation for this answer? contact us directly to get an explanation for this answer