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.
Program/Source Code:
The source code to calculate the sum of array elements is given below. The given program is compiled and executed successfully.
Output:
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