Dremendo Tag Line

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

Two Dimensional Array - Question 2

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

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

Program

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

using namespace std;

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

    cout<<"Enter 9 numbers\n";
    for(r=0; r<3; r++)
    {
        for(c=0; c<3; c++)
        {
            cin>>a[r][c];
            if(a[r][c]>=10 && a[r][c]<=99)
            {
                s1=s1+a[r][c];
            }
            else if(a[r][c]>=100 && a[r][c]<=999)
            {
                s2=s2+a[r][c];
            }
        }
    }
    cout<<"Sum of 2 digit positive numbers = "<<s1<<endl;
    cout<<"Sum of 3 digit positive numbers = "<<s2;
    return 0;
}

Output

Enter 9 numbers
12
4
69
124
36
221
96
5
7
Sum of 2 digit positive numbers = 213
Sum of 3 digit positive numbers = 345
video-poster