×

Ruby Tutorial

Ruby Basics

Ruby Control Statements

Ruby Methods

Ruby Classes and Methods

Ruby Arrays

Ruby Sets

Ruby Strings

Ruby Classes & Objects

Ruby Hash

Ruby Tools

Ruby Functions

Ruby Built-in Functions

Misc.

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


Comments and Discussions!

Load comments ↻


Advertisement
Advertisement
Advertisement

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