C C++ program to find sum of all elements of matrix

Coding in C

#include<stdio.h>
int main()
{
    int m[3][3],i,j,sum=0;
    printf("Enter elements of Matrix:\n");
    for(i=0;i<3;i++)
    {
        for(j=0;j<3;j++)
        {
            scanf("%d",&m[i][j]);
        }
    }
    for(i=0;i<3;i++)
    {
        for(j=0;j<3;j++)
        {
            sum=sum+m[i][j];
        }
    }
    printf("Sum of all elements = %d",sum);
    return 0;
}

Coding in C++

#include<iostream>
using namespace std;
int main()
{
    int m[3][3],i,j,sum=0;
    cout<<"Enter elements of Matrix:\n";
    for(i=0;i<3;i++)
    {
        for(j=0;j<3;j++)
        {
            cin>>m[i][j];
        }
    }
    for(i=0;i<3;i++)
    {
        for(j=0;j<3;j++)
        {
            sum=sum+m[i][j];
        }
    }
    cout<<"Sum of all elements = "<<sum;
    return 0;
}

Output

C C++ program to find sum of all elements of matrix

Published by

admin

I am a computer teacher, programmer and web developer