Basic Programming C# Examples

C# Program to Find Sum of right Diagonals of a Matrix

The Program in C# Program to Find Sum of right Diagonals of a Matrix is given below:

using System;

namespace RightDiagonalSum
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello Codeauri Family, enter the number of rows and columns in the matrix: ");
            int rows = int.Parse(Console.ReadLine());
            int columns = int.Parse(Console.ReadLine());

            int[,] matrix = new int[rows, columns];
            int sum = 0;

            Console.WriteLine("Enter the elements of the matrix: ");
            for (int i = 0; i < rows; i++)
            {
                for (int j = 0; j < columns; j++)
                {
                    matrix[i, j] = int.Parse(Console.ReadLine());
                }
            }

            for (int i = 0; i < rows; i++)
            {
                sum += matrix[i, i];
            }

            Console.WriteLine("Okay, the sum of right diagonals is: " + sum);
        }
    }
}

Output:

Hello Codeauri Family, enter the number of rows and columns in the matrix:
3
3
Enter the elements of the matrix: 56
78
45
65
33
45
23
12
56
Okay, the sum of right diagonals is: 145

Pro-Tips💡

Here are the steps by steps execution of above program:

  1. We start by importing the System namespace.
  2. Then, we create a class Program and a Main method.
  3. Next, we ask the user to enter the number of rows and columns in the matrix.
  4. We then declare two variables, rows and columns to store the number of rows and columns entered by the user.
  5. We create a 2-dimensional array matrix to store the elements of the matrix, and a variable sum to store the sum of right diagonals.
  6. We then ask the user to enter the elements of the matrix.
  7. We use a for loop to traverse the matrix, and for each element, we add the value of the current element (i, i) to the sum if the current row number is equal to the current column number.
  8. Finally, we print the sum of right diagonals.

Learn C-Sharp ↗

C-sharp covers every topic to learn about C-Sharp thoroughly.

Learn C Programming ↗

C-Programming covers every topic to learn about C-Sharp thoroughly.

Learn C++ Programming↗

C++ covers every topic to learn about C-Sharp thoroughly.

Codeauri is Code Learning Hub and Community for every Coder to learn Coding by navigating Structurally from Basic Programming to Front-End Development, Back-End Development to Database, and many more.

Related Posts

Programming Languages( Types, Pros and Cons)-Codeauri

What are the types or levels of Programming Languages? The types or levels of programming languages are divided into two types: Types Of Programming Languages: 1.Machine-level Language :1st…

C# Program to Find Sum of Rows & Columns of a Matrix

The Program in C# Program to Find Sum of Rows & Columns of a Matrix is given below: Output: Hello Codeauri Family,enter the number of rows and columns…

C# Program to Calculate Determinant of Given Matrix

The Program in C# Program to Calculate Determinant of Given Matrix is given below: Output: Hello Codeauri Family, enter the number of rows and columns of the matrix…

C# Program to Find Transpose of Given Matrix

The Program in C# Program to Find Transpose of Given Matrix is given below: Output: Hello Codeauri Family, enter the number of rows and columns in the matrix:22Enter…

C# Program for Multiplication of two square Matrices

The Program in C# Program for Multiplication of two square Matrices is given below: Output: Hello Codeauri Family, enter the number of rows/columns in the matrices:2Enter the elements…

C# Program to Delete Element at Desired position From Array

The Program in C# Program to Delete Element at Desired position From Array is given below: Output: Hello Codeauri Family, enter the number of elements in the array:4Enter…

Leave a Reply

Your email address will not be published. Required fields are marked *

Your Journey into Code Begins Now: Discover the Wonders of Basic Programming

X