Write a program in C to add two matrix

#include <stdio.h>

#include <conio.h>


int main() {

    int rows=0, cols=0;


    printf("Enter the rows of matrix: ");

    scanf("%d", &rows);

    printf("Enter the columns of matrix: ");

    scanf("%d", &cols);


    int a[rows][cols], b[rows][cols];


    printf("Enter the values of the first matrix:\n");


    for(int i=0; i<rows; i++) {

        for(int j=0; j<cols; j++) {

            printf("a[%d][%d]: ", i, j);

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

        }

    }


    printf("Enter the values of the second matrix:\n");


    for(int i=0; i<rows; i++) {

        for(int j=0; j<cols; j++) {

            printf("b[%d][%d]: ", i, j);

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

        }

    }


    printf("\n");


    for(int i=0; i<rows; i++) {

        for(int j=0; j<cols; j++) {

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

        }

        printf("|\t");

        for(int j=0; j<cols; j++) {

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

        }


        printf("\n");

    }


    printf("\n");


    printf("Addition of the two matrix is:\n");


    for(int i=0; i<rows; i++) {

        for(int j=0; j<cols; j++) {

            printf("%d\t", a[i][j]+b[i][j]);

        }

        printf("\n");

    }


    getch();

    return 0;

}


Comments