C# | Sum of the left diagonal of the matrix

Here, we are going to learn how to find the sum of the left diagonal of the matrix in C#?
Submitted by Nidhi, on November 02, 2020 [Last updated : March 19, 2023]

Here, we will read a matrix from the user and then find the sum of the left diagonal of the matrix and then print the matrix and sum of left diagonal elements on the console screen.

C# program to find the sum of the left diagonal of the matrix

The source code to find the sum of the left diagonal of the matrix is given below. The given program is compiled and executed successfully on Microsoft Visual Studio.

//C# | Sum of the left diagonal of the matrix.

using System;

class MatrixDemo
{
    public static void Main(string[] args)
    {
        int i   = 0;
        int j   = 0;

        int sumLeftDiagonal = 0;
        int row = 3;
        int col = 3;
        
        int[,] Matrix = new int[row, col];
        
        Console.Write("Enter the elements of matrix: ");
        for (i = 0; i < row; i++)
        {
            for (j = 0; j < col; j++)
            {
                Matrix[i, j] = int.Parse(Console.ReadLine());
            }
        }

        Console.WriteLine("\nMatrix: ");
        for (i = 0; i < row; i++)
        {
            for (j = 0; j < col; j++)
            {
                Console.Write(Matrix[i, j] + "\t");
            }
            Console.WriteLine();
        }

        for (i = 0; i < row; i++)
        {
            for (j = 0; j < col; j++)
            {
                if((i+j)==2)
                    sumLeftDiagonal += Matrix[j, i];
            }
        }

        Console.WriteLine("Sum of left diagonal is: "+sumLeftDiagonal);
    }
}

Output

Enter the elements of matrix: 1
2
3
4
5
6
7
8
9

Matrix:
1       2       3
4       5       6
7       8       9
Sum of left diagonal is: 15
Press any key to continue . . .

Explanation

In the above program, we created a class MatrixDemo that contains a Main() method. The Main() method is the entry point for the program, Here we created a 2-D array to represent a matrix.

Console.Write("Enter the elements of matrix: ");
for (i = 0; i < row; i++)
{
    for (j = 0; j < col; j++)
    {
        Matrix[i, j] = int.Parse(Console.ReadLine());
    }
}
Console.WriteLine("\nMatrix: ");
for (i = 0; i < row; i++)
{
    for (j = 0; j < col; j++)
    {
        Console.Write(Matrix[i, j] + "\t");
    }
    Console.WriteLine();
}

In the above code, we read the elements of matrix and print the matrix on the console screen.

for (i = 0; i < row; i++)
        {
            for (j = 0; j < col; j++)
            {
                if((i+j)==2)
                    sumLeftDiagonal += Matrix[j, i];
            }
        }

Console.WriteLine("Sum of left diagonal is: "+sumLeftDiagonal);

Here, we calculated the sum of the left diagonal of the matrix and then print that on the console screen.

C# Basic Programs »

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.