Q:

Write a Ruby program to compute the sum of elements in a given array

0

 Write a Ruby program to compute the sum of elements in a given array.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

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

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Similar questions


need a help?


find thousands of online teachers now