C program to Find the Sum of two Matrices

C program to Find the Sum of two Matrices

Problem Statement: Given two multi-dimensional arrays, also called as matrices, find their Sum.

// C program to find the sum of two matrices of order

#include <stdio.h>
int main()
{
    int a[3][3], b[3][3], result[3][3];

    // Taking input using nested for loop
    printf("Enter elements of 1st matrix\n");
    for (int i = 0; i < 3; ++i)
        for (int j = 0; j < 3; ++j)
        {
            scanf("%d", &a[i][j]);
        }

    // Taking input using nested for loop
    printf("Enter elements of 2nd matrix\n");
    for (int i = 0; i < 3; ++i)
        for (int j = 0; j < 3; ++j)
        {
            scanf("%d", &b[i][j]);
        }

    // adding corresponding elements of two matrices
    for (int i = 0; i < 3; ++i)
        for (int j = 0; j < 3; ++j)
        {
            result[i][j] = a[i][j] + b[i][j];
        }

    // Displaying the sum
    printf("Sum Of Matrix:\n");

    for (int i = 0; i < 3; ++i)
    {
        for (int j = 0; j < 3; ++j)
        {
            printf("%d\t", result[i][j]);
        }
        printf("\n");
    }
    return 0;
}
$ ./a.out
Enter elements of 1st matrix
1 2 3
4 5 6
7 8 9
Enter elements of 2nd matrix
1 1 1
1 1 1
1 1 1
Sum Of Matrix:
2 3 4
5 6 7
8 9 10
$

If you like the post C program to Find the Sum of two Matrices, please share your feedback!


also see

C Programming language
Go Programming language
Linked List Array
Simplification Queue
DBMS Reasoning
Aptitude HTML
Previous articleDoubly Linked List
Next articleHTML Layout

LEAVE A REPLY

Please enter your comment!
Please enter your name here