Basic Programming C# Examples

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:

using System;

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

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

            Console.WriteLine("Well,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());
                }
            }

            int[] rowSum = new int[rows];
            int[] columnSum = new int[columns];

            for (int i = 0; i < rows; i++)
            {
                for (int j = 0; j < columns; j++)
                {
                    rowSum[i] += matrix[i, j];
                    columnSum[j] += matrix[i, j];
                }
            }

            Console.WriteLine("Okay, the sum of rows of the matrix is: ");
            for (int i = 0; i < rows; i++)
            {
                Console.WriteLine("Row {0}: {1}", i + 1, rowSum[i]);
            }

            Console.WriteLine("Okay, the sum of columns of the matrix is: ");
            for (int i = 0; i < columns; i++)
            {
                Console.WriteLine("Column {0}: {1}", i + 1, columnSum[i]);
            }
        }
    }
}

Output:

Hello Codeauri Family,enter the number of rows and columns of the matrix here to find the sum of columns of matrix:
2
2
Well,enter the elements of the matrix:
12
45
77
34
Okay, the sum of rows of the matrix is:
Row 1: 57
Row 2: 111
Okay, the sum of columns of the matrix is:
Column 1: 89
Column 2: 79

Pro-Tips💡

Here are the steps by steps execution of above program:

  1. Using System: This line of code is a directive that tells the compiler to use the System namespace, which contains the Console class used in this code.
  2. Namespace MatrixSum: The code defines a namespace called MatrixSum, which is used to group related classes together.
  3. Class Program: This line of code defines a class named Program, which is the main class of the application.
  4. Main method: The Main method is the entry point of the program, where execution starts.
  5. Console.WriteLine: The code uses the WriteLine method of the Console class to display a prompt asking the user to enter the number of rows and columns of the matrix.
  6. int.Parse: The code uses the Parse method of the int data type to convert the user’s input from a string to an integer.
  7. int[,] matrix: The code creates a two-dimensional integer array named “matrix” with the number of rows and columns specified by the user.
  8. Loop: A nested loop is used to fill the elements of the matrix with values entered by the user.
  9. int[] rowSum and int[] columnSum: The code creates two arrays, “rowSum” and “columnSum”, to store the sum of each row and column of the matrix, respectively.
  10. Nested Loop: Another nested loop is used to calculate the sum of each row and column of the matrix and store it in the “rowSum” and “columnSum” arrays.
  11. Console.WriteLine: The code uses the WriteLine method of the Console class to display the sum of each row and column of the matrix.
  12. End of Main method: The Main method ends and the program terminates.

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 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 Sum of right Diagonals of a Matrix

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

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