C# Program to Find Sum of Diagonal Elements of Matrix
Posted by Superadmin on August 15 2022 06:29:43

C# Program to Find Sum of Diagonal Elements of Matrix

 

 

This is a C# Program to find the sum of the values on diagonal of the matrix.

Problem Description

This C# Program Finds the Sum of the Values on Diagonal of the Matrix.

Problem Solution

Here the number of rows and columns are first obtained and the sum is calculated by adding the diagonal elements.

Program/Source Code

Here is source code of the C# Program to Find the Sum of the Values on Diagonal of the Matrix. The C# program is successfully compiled and executed with Microsoft Visual Studio. The program output is also shown below.

/*
 * C# Program to Find the Sum of the Values on 
 * Diagonal of the Matrix
 */
using System;
using System.Collections.Generic;
using System.Text;
class mat
    {
        int i, j, m, n;
        int[,] a = new int[20, 20];
        public void get()
        {
            Console.WriteLine("Enter Row Value");
            m = int.Parse(Console.ReadLine());
            Console.WriteLine("Enter Column Value");
            n = int.Parse(Console.ReadLine());
            Console.WriteLine("Enter Elements one by one");
            for (i = 1; i <= m; i++)
            {
                for (j = 1; j <= n; j++)
                {
                    a[i, j] = int.Parse(Console.ReadLine());
                }
            }
            Console.WriteLine("Given Matrix");
            for (i = 1; i <= m; i++)
            {
                for (j = 1; j <= n; j++)
                {
                    Console.Write("\t{0}", a[i, j]);
                }
                Console.WriteLine();
            }
        }
        public void diag()
        {
            int d;
            d = 0;
            if (m == n)
            {
                for (i = 1; i <= m; i++)
                {
 
                    for (j = 1; j <= n; j++)
                    {
                        if (i == j)
                        {
                            d = d + a[i, j];
                        }
 
                    }
                }
                Console.WriteLine("Diagonal Sum= {0}", d);
            }
            else
            {
                Console.WriteLine("Can't Perform Diagonal Sum");
            }
        }
    class matsum
    {
        static void Main(string[] args)
        {
            mat ma = new mat();
            ma.get();
            ma.diag();
            Console.Read();
        }
    }
}
Program Explanation

This C# program, we are reading the order of the matrix using ‘m’ and ‘n’ variable. If else condition statement is used to check the order of the value of row and column matrix of an array are equal. If the condition is true, then using for loop we are entering the coefficient element values for an array.

 

Otherwise, if the condition is false then execute the else statement and print the given order is not square matrix. Another for loop is used to add the main diagonal of matrix as well as the opposite diagonal of the matrix.

 

Initialize the value of ‘i’ variable as 0 and check the condition that the value of ‘i’ variable is less than the value of ‘m’ variable. If the condition is true then execute the iteration of the loop. The ‘sum’ variable is used to compute the summation of the value of ‘sum’ variable with int[][] coefficient elements in the array. Print the sum of the values on diagonal of the matrix.

Runtime Test Cases
 
Enter Row Value : 2
Enter Column Value : 2
Enter Elements One by One : 
2
2
2
2
Given Matrix :
  2   2
  2   2
Diagonal Sum :4