Dremendo Tag Line

Input numbers in 2d array and find sum of all numbers in C++

Two Dimensional Array - Question 1

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

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

Program

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

using namespace std;

int main()
{
    int a[3][3], r,c,s=0;

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

Output

Enter 9 numbers
12
8
13
4
34
17
15
32
60
Sum of all the numbers = 195
video-poster