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.

advertisement
advertisement

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

Sanfoundry Global Education & Learning Series – 1000 C# Programs.

If you wish to look at all C# Programming examples, go to 1000 C# Programs.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.