Skip to main content

Product of two Same order Matrices in C

 

This code calculates the product of two matrices entered by the user. It uses the standard libraries `stdio.h`, `stdlib.h`, and `string.h`. After clearing the screen, it prompts the user to enter the number of rows and columns for the matrices. It then declares three matrices: `a`, `b`, and `product`. The code proceeds to prompt the user to enter the elements of the first and second matrices. Next, it computes the product of the matrices using nested loops and stores the result in the `product` matrix. Finally, it prints the resulting matrix.


Source Code :


MatrixProductSameOrder.C

#include<stdio.h>
#include<stdlib.h>
#include<string.h>

int main(){

    int row,columns;
    // this line will clear the screen using stdlib.h file
    system("cls");

    // getting number of rows and columns from user
    printf("Enter the number of Rows and Columns of the matrices : ");
    scanf("%d %d", &row,&columns);
    printf("\n");

    // sorry i forget to declare matrices
    // you should declare matrix after
// geting row and columns
    int a[row][columns],b[row][columns],product[row][columns];

    // now to get elements of matrix from user
    printf("-> Enter elements of matrix 1 of order %d X %d\n", row,columns);
    printf("\n");

    // starting loop
    // matrix 1
    for (int i = 0; i < row; i++)
    {
        for (int j = 0; j < columns; j++)
        {
            printf("Enter the value of %d %d element : ", i+1, j+1);
            scanf("%d", &a[i][j]);
        }
       
    }
    printf("\n");
    printf("-> Enter elements of matrix 2 of order %d X %d\n", row,columns);
    printf("\n");

    // starting loop
    // matrix 2
    for (int i = 0; i < row; i++)
    {
        for (int j = 0; j < columns; j++)
        {
            printf("Enter the value of %d %d element : ", i+1, j+1);
            scanf("%d", &b[i][j]);
        }
       
    }
    // now to make logic to find product of these two matrix
    for (int i = 0; i < row; i++)
    {
        for (int j = 0; j < columns; j++)
        {
            product[i][j]=0;
            for (int k = 0; k < columns; k++)
            {
                product[i][j] += a[i][k] * b[k][j];
            }
           
        }
       
    }

    // To print the resultant matrix
    printf("\n");
    printf("Product of the matrices is : \n");

    for (int i = 0; i < row; i++)
    {
        for (int j = 0; j < columns; j++)
        {
            printf("%d\t", product[i][j]);
        }
            printf("\n");
       
    }
   

    return 0;
}


Follow for more code and there source codes

THANK YOU


Comments

Popular posts from this blog

Lexicographical Order in C language

  This code is a C program that sorts a given number of strings in lexicographical order. It begins by asking the user to enter the number of strings. Then, it prompts the user to input each string one by one. After obtaining all the strings, it uses nested loops and the `strcmp` function to compare and sort the strings in lexicographical order. Finally, it prints the sorted strings to the console. The code utilizes the standard libraries `stdio.h`, `stdlib.h`, and `string.h` for input/output operations, memory allocation, and string manipulation, respectively. Watch full  Video on YouTube Now - LEXICOGRAPHICAL ORDER Source code of this program : Lexicographic.C #include <stdio.h> #include <stdlib.h> #include <string.h>   int main (){        int number ;        char string [ 10 ][ 100 ], s [ 100 ];        printf ( "Enter number of strings : " );        scanf ( " %d " ...

Top 5 Best Text Editors for Programmers

In the world of programming, choosing the right code editor is crucial for developers seeking a seamless and efficient coding experience. With numerous options available, it can be overwhelming to decide which code editor is best suited for your needs. This blog aims to explore and provide in-depth insights into the top code editors available today, enabling you to make an informed choice and elevate your programming skills. 1. Visual Studio Code (VS Code): Visual Studio Code, developed by Microsoft, is a highly versatile and widely used code editor. It has gained immense popularity among developers due to its powerful features and extensive support for a wide range of programming languages. VS Code offers a user-friendly interface with a customizable layout that allows developers to personalize their coding environment. It provides essential features such as syntax highlighting, code completion, and linting, which help catch errors and improve code quality. The editor also includes bu...