Skip to main content

Matrix Multiplication in c language

first matrix:     second matrix:    answer matrix is:

2   3                    3     4                    30         29

3   2                    8     7                    27         26


below is code in c language:

#include <stdio.h>
#include <string.h>
#include <math.h>
#include <ctype.h>
#include <stdlib.h>
int main()
{
    int row1row2col1col2sum;
    printf("Enter the row and column for matrix 1\n");
    scanf("%d %d", &row1, &col1);
    printf("Enter the row and column for matrix 2\n");
    scanf("%d %d", &row2, &col2);
    int mat1[row1][col1];
    int mat2[row2][col2];
    int ansmat[row1][col2];
    printf("Enter the first matrix\n");
    for (int i = 0i < row1i++)
    {
        for (int j = 0j < col1j++)
        {
            scanf("%d", &mat1[i][j]);
        }
    }
    printf("Enter the second matrix\n");
    for (int i = 0i < row2i++)
    {
        for (int j = 0j < col2j++)
        {
            scanf("%d", &mat2[i][j]);
        }
    }

    if (col1 == row2)
    {

        for (int i = 0i < row1i++)
        {
            for (int j = 0j < col2j++)
            {
                sum = 0;
                for (int k = 0k < row2k++)
                {
                    sum = sum + (mat1[i][k] * mat2[k][j]);
                }
                ansmat[i][j] = sum;
            }
        }
        printf("The answer matrix is below\n");
        for (int i = 0i < row1i++)
        {
            for (int j = 0j < col2j++)
            {
                printf("%d\t"ansmat[i][j]);
            }
            printf("\n");
        }
    }
    else
    {
        printf("Multiplication is not possible\n");
    }

    return 0;
}

Comments

Popular posts from this blog

C Program to Calculate Difference Between Two Time Periods

  Calculate Difference Between Two Time Periods # include <stdio.h> struct TIME { int seconds; int minutes; int hours; }; void differenceBetweenTimePeriod (struct TIME t1, struct TIME t2, struct TIME *diff) ; int main () { struct TIME startTime , stopTime , diff ; printf ( "Enter the start time. \n" ); printf ( "Enter hours, minutes and seconds: " ); scanf ( "%d %d %d" , &startTime.hours, &startTime.minutes, &startTime.seconds); printf ( "Enter the stop time. \n" ); printf ( "Enter hours, minutes and seconds: " ); scanf ( "%d %d %d" , &stopTime.hours, &stopTime.minutes, &stopTime.seconds); // Difference between start and stop time differenceBetweenTimePeriod(startTime, stopTime, &diff); printf ( "\nTime Difference: %d:%d:%d - ...

What is Cloud computing?

Cloud computing refers to the delivery of computing resources and services over the internet, allowing users to access and use applications, storage, and processing power without the need for local infrastructure or hardware. It involves the use of remote servers, typically hosted in data centers, to store, manage, and process data instead of relying on a local computer or server. In cloud computing, users can access a wide range of services and resources on-demand, which are usually provided by a cloud service provider (CSP). These services can include infrastructure resources (such as virtual machines, storage, and networking), platforms for developing and deploying applications, and software applications that can be accessed and used via the internet. Cloud computing offers several advantages over traditional on-premises computing models. It allows for scalability, where users can easily scale up or down their resource usage based on their needs, without having to invest in and mana...

C Program to Sort Elements in Lexicographical Order (Dictionary Order)

  Sort strings in the dictionary order # include <stdio.h> # include <string.h> int main () { char str[ 5 ][ 50 ], temp[ 50 ]; printf ( "Enter 5 words: " ); // Getting strings input for ( int i = 0 ; i < 5 ; ++i) { fgets(str[i], sizeof (str[i]), stdin ); } // storing strings in the lexicographical order for ( int i = 0 ; i < 5 ; ++i) { for ( int j = i + 1 ; j < 5 ; ++j) { // swapping strings if they are not in the lexicographical order if ( strcmp (str[i], str[j]) > 0 ) { strcpy (temp, str[i]); strcpy (str[i], str[j]); strcpy (str[j], temp); } } } printf ( "\nIn the lexicographical order: \n" ); for ( int i = 0 ; i < 5 ; ++i) { fputs (str[i], stdout ); } return 0 ; } Output Enter 5 words: R programming JavaScript Java C programming C++ programming In the lexicographical order: C programming C+...