Scala program to calculate the sum of matrix elements

Today we are going to discuss this important topic via this article Scala program to calculate the sum of matrix elements We hope you like this article.

Scala program to calculate the sum of matrix elements

Scala program to calculate the sum of matrix elements

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

object Sample {
  def main(args: Array[String]) {
    var TwoDArr = Array.ofDim[Int](2, 2)
    var i: Int = 0
    var j: Int = 0

    var sum: Int = 0

    printf("Enter elements of MATRIX:\n")
    i = 0;
    while (i < 2) {
      j = 0;
      while (j < 2) {
        printf("ELEMENT(%d)(%d): ", i, j);
        TwoDArr(i)(j) = scala.io.StdIn.readInt();
        sum = sum + TwoDArr(i)(j);
        j = j + 1;
      }
      i = i + 1;
    }

    printf("MATRIX:\n")
    i = 0;
    while (i < 2) {
      j = 0;
      while (j < 2) {
        printf("%d ", TwoDArr(i)(j));
        j = j + 1;
      }
      i = i + 1;
      println();
    }

    printf("Sum of matrix elements is: %d\n", sum);
  }
}
Enter elements of MATRIX:
ELEMENT(0)(0): 10
ELEMENT(0)(1): 20
ELEMENT(1)(0): 30
ELEMENT(1)(1): 40
MATRIX:
10 20 
30 40
Sum of matrix elements is: 100

Final Words

Scala program to calculate the sum of matrix elements We learned about this very clearly through this article. And if you liked this article please share it with your friend.

Hi, I'm Ranjith a full-time Blogger, YouTuber, Affiliate Marketer, & founder of Coding Deekshi. Here, I post about programming to help developers.

Share on:

Leave a Comment