Dremendo Tag Line

Input numbers in 2d array and find the sum of each column in C++

Two Dimensional Array - Question 6

In this question, we will see how to input numbers in a 3X3 integer matrix (2d array) and find the sum of each column separately in C++ programming. To know more about two dimensional array click on the two dimensional array lesson.

Q6) Write a program in C++ to input numbers in a 3X3 integer matrix (2d array) and find the sum of each column separately.

Program

#include <iostream>
#include <conio.h>

using namespace std;

int main()
{
    int a[3][3], r,c,cs;

    cout<<"Enter 9 numbers\n";
    for(r=0; r<3; r++)
    {
        for(c=0; c<3; c++)
        {
            cin>>a[r][c];
        }
    }

    for(r=0; r<3; r++)
    {
        cs=0;
        for(c=0; c<3; c++)
        {
            cout<<a[r][c]<<" ";
            cs=cs+a[c][r];
        }
        cout<<"  Column Sum = "<<cs<<endl;
    }
    return 0;
}

Output

Enter 9 numbers
18
12
72
10
15
45
38
5
64
18 12 72   Column Sum = 66
10 15 45   Column Sum = 32
38 5 64   Column Sum = 181
video-poster