ADVERTISEMENT
ADVERTISEMENT

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 »


ADVERTISEMENT
ADVERTISEMENT


Comments and Discussions!



ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.