C Program to find the Multiplication of two n x n Matrices


 

// Program by Akash Tripathi (@proakash256) 
#include <stdio.h>
int main()
{
    int r;
    printf("Enter the number of Rows and Columns: ");
    scanf("%d" , &r);
    int ar1[r][r];
    int ar2[r][r];
    int ar3[r][r];
    printf("\nEnter the elements (row wise) of the First Matrix:\n");
    for(int i = 0i < ri = i + 1)
        for(int j = 0j < rj = j + 1)
            scanf("%d" , &ar1[i][j]);
    printf("\nEnter the elements (row wise) of the Second Matrix:\n");
    for(int i = 0i < ri = i + 1)
        for(int j = 0j < rj = j + 1)
            scanf("%d" , &ar2[i][j]);
    printf("\nThe First Matrix is :\n\n");
    for(int i = 0i < ri = i + 1)
    {
        for(int j = 0j < rj = j + 1)
            printf("%d\t" , ar1[i][j]);
        printf("\n");
    }
    printf("\nThe Second Matrix is :\n\n");
    for(int i = 0i < ri = i + 1)
    {
        for(int j = 0j < rj = j + 1)
            printf("%d\t" , ar2[i][j]);
        printf("\n");
    }
    int sum = 0 , i = 0 , j = 0;
    for(i = 0i < ri = i + 1)
    {
        for(j = 0j < rj = j + 1)
        {
            for(int k = 0k < rk = k + 1)
                sum = sum + (ar1[i][k] * ar2[k][j]);
            ar3[i][j] = sum;
            sum = 0;
        }
    }
    printf("\nThe Multiplication of both the matrices is :\n\n");
    for(int i = 0i < ri = i + 1)
    {
        for(int j = 0j < rj = j + 1)
            printf("%d\t" , ar3[i][j]);
        printf("\n");
    }
    printf("\n");
    return 0;
}

Comments