Ruby program to calculate the sum of array elements

Ruby Example: Write a program to calculate the sum of array elements.
Submitted by Nidhi, on January 19, 2022

Problem Solution:

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

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.

Ruby Arrays Programs »




Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.