C# Program to Find the Sum of Each Column of the Matrix

This is a C# Program to find Sum of the elements of each column of the given matrix.

Problem Description

This C# Program Finds Sum of the Elements of each Column of the Given Matrix.

Problem Solution

Here the sum of each column of the matrix is obtained by adding the elements of that corresponding column.

Program/Source Code

Here is source code of the C# Program to Find Sum of the Elements of each Column of the Given Matrix. The C# program is successfully compiled and executed with Microsoft Visual Studio. The program output is also shown below.

/*
 * C# Program to Find Sum of the Elements of each Column 
 * of the Given Matrix
 */
using System;
using System.Collections.Generic;
using System.Text;
namespace matrix_col
{
    class mat
    {
        int i, j, m, n;
        int[,] a = new int[20, 20];
        int[,] c = new int[20, 20];
        public void getmatrix()
        {
            Console.WriteLine("Enter the Number of Rows : ");
            m = int.Parse(Console.ReadLine());
            Console.WriteLine("Enter the Number of Columns : ");
            n = int.Parse(Console.ReadLine());
            Console.WriteLine("Enter the Elements");
            for (i = 1; i <= m; i++)
            {
                for (j = 1; j <= n; j++)
                {
                    a[i, j] = int.Parse(Console.ReadLine());
                }
            }
            Console.WriteLine("Given Matrix");
            for (i = 1; i <= m; i++)
            {
                for (j = 1; j <= n; j++)
                {
                    Console.Write("\t{0}", a[i, j]);
                }
                Console.WriteLine();
            }
        }
 
        public void col()
        {
            int c;
            for (i = 1; i <= n; i++)
            {
                c = 0;
                for (j = 1; j <= m; j++)
                {
                    c = c + a[j, i];
                }
                Console.WriteLine("{0} Column Sum : {1}", i, c);
            }
        }
    }
    class matsum
    {
        static void Main(string[] args)
        {
            mat ma = new mat();
            ma.getmatrix();
            ma.col();
            Console.ReadLine();
        }
    }
}
Program Explanation

This C# program, we are creating an object variable ‘ma’ to the mat class. The getmatrix() method we are reading the number of rows and columns of the matrix using ‘m’ and ‘n’ variables respectively.

advertisement
advertisement

Using for loop we are entering the coefficient element values for the array variable a[i,j]. Using for loop in col() method compute the sum of the elements of each column. Print the sum of the elements of each column of the given matrix.

Runtime Test Cases
 
Enter the Number of Rows : 2
Enter the Number of Columns : 2
Enter the Elements : 
1
2
3
4
Given Matrix :
        1      2
        3      4
1 Column Sum : 4
2 Column Sum : 6

Sanfoundry Global Education & Learning Series – 1000 C# Programs.

If you wish to look at all C# Programming examples, go to 1000 C# Programs.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.