C program for Addition of two matrices (2D Arrays)

 #include <stdio.h>

#include <stdlib.h>


int main() {


    int a[10][10], b[10][10], c[10][10];

    int ar, ac, br, bc, i, j;


    printf("\nEnter how many rows and cols of Matrix A: ");

    scanf("%d %d", &ar, &ac);

    

    printf("\nEnter how many rows and cols of Matrix B: ");

    scanf("%d %d", &br, &bc);


    if (ar != br || ac != bc) {

        printf("\nInvalid Addition\n");

        exit(0);

    }


    printf("\nEnter the elements of Matrix A:\n");

    for (i = 0; i < ar; i++) {

        for (j = 0; j < ac; j++) {

            scanf("%d", &a[i][j]);

        }

    }


    printf("\nEnter the elements of Matrix B:\n");

    for (i = 0; i < br; i++) {

        for (j = 0; j < bc; j++) {

            scanf("%d", &b[i][j]);

        }

    }


    // Addition of matrices

    for (i = 0; i < ar; i++) {

        for (j = 0; j < ac; j++) {

            c[i][j] = a[i][j] + b[i][j];

        }

    }


    printf("\nAddition of Matrix A and Matrix B:\n");

    for (i = 0; i < ar; i++) {

        printf("\n");

        for (j = 0; j < ac; j++) {

            printf("%d\t", c[i][j]);

        }

    }


    return 0;

}

Comments

Popular posts from this blog

Single Program Using All Automatic String Functions

C program for subtraction of two matrices (2D Arrays)

C program for Multiplication of Two matrices