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:
- 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.
- Namespace MatrixSum: The code defines a namespace called MatrixSum, which is used to group related classes together.
- Class Program: This line of code defines a class named Program, which is the main class of the application.
- Main method: The Main method is the entry point of the program, where execution starts.
- 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.
- 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.
- int[,] matrix: The code creates a two-dimensional integer array named “matrix” with the number of rows and columns specified by the user.
- Loop: A nested loop is used to fill the elements of the matrix with values entered by the user.
- 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.
- 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.
- Console.WriteLine: The code uses the WriteLine method of the Console class to display the sum of each row and column of the matrix.
- 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.