Q:

Ruby program to calculate the sum of array elements

belongs to collection: Ruby Arrays Programs

0

In this program, we will create an array of integer elements. Then we will calculate the sum of array elements.

All Answers

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

Program/Source Code:

The source code to calculate the sum of array elements is given below. The given program is compiled and executed successfully.

# Ruby program to calculate the 
# sum of array elements

arr = [12,45,23,39,38];
count = 0;
sum = 0;

while count < arr.size()
    sum = sum + arr[count];
    count=count + 1;
end

print "Sum of array elements is: ",sum,"\n";

Output:

Sum of array elements is: 157

Explanation:

In the above program, we created an array of integer elements. Then we calculated the sum of array elements and printed the result.

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

total answers (1)

Ruby Arrays Programs

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
Ruby program to reverse an array without using lib... >>
<< Ruby program to find the EVEN numbers from the arr...