ADVERTISEMENT
ADVERTISEMENT

C# program to find the sum of each row of the matrix

Here, we are going to learn how to find the sum of each row of the matrix in C#?
Submitted by Nidhi, on November 02, 2020

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

Program:

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

//C# program to find the sum of each row of the matrix.

using System;

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

        int sum = 0;
        int row = 2;
        int col = 2;
        
        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++)
        {
            sum = 0;
            for (j = 0; j < col; j++)
            {
                sum += Matrix[i, j];
            }
            Console.WriteLine("Sum of row[{0}]: {1}", (i + 1), sum);
        }
    }
}

Output:

Enter the elements of matrix: 1
2
3
4

Matrix:
1       2
3       4
Sum of row[1]: 3
Sum of row[2]: 7
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 the matrix and print the matrix on the console screen.

for (i = 0; i < row; i++)
{
    sum = 0;
    for (j = 0; j < col; j++)
    {
        sum += Matrix[i, j];
    }
    Console.WriteLine("Sum of row[{0}]: {1}", (i + 1), sum);
}

Here, we calculated the sum of each row of the matrix and then print the sum of each row on the matrix.

C# Basic Programs »


ADVERTISEMENT



ADVERTISEMENT



Comments and Discussions


ADVERTISEMENT

ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.