Home »
Ruby Tutorial »
Ruby Programs
Ruby program to calculate the sum of array elements
Last Updated : December 15, 2025
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 »
Advertisement
Advertisement