Scala program to calculate the sum of array elements

Here, we are going to learn how to calculate the sum of array elements in Scala programming language?
Submitted by Nidhi, on May 07, 2021

Problem Solution:

Here, we will create an array of integer elements, and then we will calculate the sum of array elements and print the result on the console screen.

Program/Source Code:

The source code to calculate the sum of array elements is given below. The given program is compiled and executed on the ubuntu 18.04 operating system successfully.

// Scala program to calculate the 
// sum of array elements

object Sample {  
    def main(args: Array[String]) {  
        var IntArray = Array(10,20,30,40,50)
        var count:Int=0
        var sum:Int=0
            sum =sum + IntArray(count)
        printf("Sum of array elements is: %d\n",sum)


Sum of array elements is: 150


In the above program, we used an object-oriented approach to create the program. We created an object Sample, and we defined main() function. The main() function is the entry point for the program.

In the main() function, we created a array IntArr and two integer variables count, sum. Then we added the value of array elements to the sum variable. After accessing and adding all elements to the sum variable. We printed the value of the sum variable on the console screen.

Scala Array Programs »


Comments and Discussions




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

© some rights reserved.