C program to print product of 2 matrices (Program 23)

/*
Made by Anmol main
Student :B.tech CSE 
Punjabi university Patiala
Program No. : 23
*/
//number of rows of 1 matrix and number of colums of 2 matrix
#include <stdio.h>
#define n 3
int main()
{
    int i, j, k = 0;
    int sum = 0;
    int rows, colums;
    int brows, bcolums;

    int product[n][n];
    int text[n][n];
    int text2[n][n];

    printf("Enter number of rows of matrix A: ");
    scanf("%d", &rows);
    printf("Enter number of colums of matrix A: ");
    scanf("%d", &colums);

    //getting input of 2 matrices
    for (i = 0; i < rows; i++)
    {
        for (j = 0; j < colums; j++)
        {
            printf("A %d%d : ", i + 1, j + 1);
            scanf("%d", &text[i][j]);
        }
    }
    printf("\n\n");
    printf("Enter number of rows of matrix B: ");
    scanf("%d", &brows);
    printf("Enter number of colums of matrix B: ");
    scanf("%d", &bcolums);

    for (i = 0; i < brows; i++)
    {
        for (j = 0; j < bcolums; j++)
        {
            printf("A %d%d : ", i + 1, j + 1);
            scanf("%d", &text2[i][j]);
        }
    }
    //printing 2 martrices
    printf("\nMatrix B :");
    for (i = 0; i < rows; i++)
    {
        printf("\n");
        for (j = 0; j < colums; j++)
        {
            printf("%d ", text[i][j]);
        }
    }
    printf("\n\nMatrix B :");
    for (i = 0; i < brows; i++)
    {
        printf("\n");
        for (j = 0; j < bcolums; j++)
        {
            printf("%d ", text2[i][j]);
        }
    }
    //resulatant matrix
    printf("\n\nResultant Matrix :");
    for (i = 0; i < rows; i++)
    {
        for (j = 0; j < bcolums; j++)
        {
            for (k = 0; k < brows; k++)
            {
                sum += text[i][k] * text2[k][j];
            }
            product[i][j] = sum;
            sum = 0;
        }
    }
    for (i = 0; i < rows; i++)
    {
        printf("\n");
        for (j = 0; j < bcolums; j++)
        {
            printf("%d    ", product[i][j]);
        }
    }

    return 0;
}

Comments

Popular posts from this blog

Glowing-Log-in-window-in-Pure-html-css #30

Mouse Hover Effect in Html CSS and Javascript #27

Recipie book in c language with text animiations - Source code ( Program - 18 )