Write a program for matrix multiplication in C language.

Tested on: Turbo C++ Version 4.0Time Complexity: O (m × n × p)Space Complexity: O (m × n + n × p + m × p)Open Source: MIT
1
2
    #include <stdio.h>
3
    #include <conio.h>
4
    int main()
5
    {
6
        int a[10][10], b[10][10], c[10][10];
7
        int m, n, p, i, j, k;
8
        clrscr();
9
        printf("Enter the order of 1st and 2nd matrix as m,n,p\n");
10
        scanf("%d%d%d", &m, &n, &p);
11
    
12
        printf("\nEnter the elements of first matrix\n");
13
        /* scanning first matrix*/
14
        for (i = 0; i < m; i++)
15
        {
16
            printf("Enter %d row elements\n", i + 1);
17
            for (j = 0; j < n; j++)
18
            {
19
                scanf("%d", &a[i][j]);
20
            }
21
        }
22
    
23
        /*scanning second matrix*/
24
        printf("\nEnter the elements of second Matrix\n");
25
        for (i = 0; i < n; i++)
26
        {
27
            printf("Enter %d row elements\n", i + 1);
28
            for (j = 0; j < p; j++)
29
            {
30
                scanf("%d", &b[i][j]);
31
            }
32
        }
33
        printf("\nAfter Multiplication of Matricies\n");
34
        /*multiplication process */
35
        for (i = 0; i < m; i++)
36
        {
37
            for (j = 0; j < p; j++)
38
            {
39
                c[i][j] = 0;
40
                for (k = 0; k < n; k++)
41
                    c[i][j] += a[i][k] * b[k][j];
42
                printf("%d\t", c[i][j]);
43
            }
44
            printf("\n");
45
        }
46
    
47
        return 0;
48
    }
49
50
        

Output

1
2
    Enter the order of 1st and 2nd matrix as m,n,p
3
    2
4
    2
5
    2
6
    Enter the elements of first matrix
7
    Enter 1 row elements
8
    1
9
    2
10
    Enter 2 row elements
11
    3
12
    4
13
    Enter the elements of second Matrix
14
    Enter 1 row elements
15
    1
16
    4
17
    Enter 2 row elements
18
    6
19
    7
20
    After Multiplication of Matricies
21
    13	18	
22
    27	40	
23
24
   

Rate the Code

Reviews are published after a quick safety check (usually within 24 hours).

Reviews are public. Developers can see basic device specifications to respond. Learn more

How to run matrix multiplication program in C-Language?

To run matrix multiplication program in C-Language, follow the below mentioned steps:

  1. Copy the Code: Press the copy button in the code block to copy the complete code.
  2. Open Turbo C++ Version 4: Open the Turbo C++ (Version 4) software on your system.
  3. Paste the code: Paste the copied code into Turbo C++’s editor.
  4. Compile the Code: Compile the program by pressing the Alt+F9 keys. This shows you the errors and warnings, if any.
  5. Run the code: Press the Ctrl+F9 keys to run the program if the program is successfully compiled.
  6. Enter the Input: Provide the required inputs or your desired inputs, if applicable.
  7. Receive the Result: Get the result of the program on the screen based on the provided inputs.

More about a matrix multiplication program in the C language

Aim: To multiply two matrices, which will be provided to the user as inputs, where the first matrix has mxn order and the second has nxp, and after multiplication, the resultant matrix order will be mxp.

Concepts used in this program

  • 2D Array: to store the elements of the matrices.
  • Nested For Loops: To traverse the matrix elements
  • Matrix Multiplication: to multiply the first matrix rows and the second matrix columns.
  • Accumulator c[i][j]: To store the final elements of the resultant matrix.

Matrix multiplication is possible only if the number of columns in the first matrix is equal to the number of rows in the second matrix.

Real-life applications

  • Computer graphics and 3D games: To rotate, scale, or translate objects in 3D graphics, matrix transformation and matrix multiplication play an important role.
  • Artificial Intelligence and Machine Learning: To calculate the inputs and weights the matrix multiplication is used at the big scale.
  • Scientific & Engineering Software: To simulate physics, engineering calculations, and to solve numerical analysis methods, matrix multiplication is used.
License: This program's source code is open-sourced under the MIT License, while the rest of the program's text content and overall dhimanp platform and brand content remain proprietary under dhimanp.

Submit your modified code

Your submissions are published after a quick safety check (usually within 7 days).

Submissions are public. Developers can see basic device specifications to respond. Learn more

Disclaimer

The information that you shared with us will be publicly visible, and everyone can see your information. And we (dhimanp) are not responsible if anyone misuses this information. We can remove any submitted code at any time for better transparency.

Privacy Policy

If you share your data with us by submitting your code for credit, then we may show your full name, school or college name, and city name on our website.

We are not using this data to track you and are not selling this data to anyone.

If you want to remove your name from our website, you can contact us, and we will remove your name from our website. In the removal process, full credit, including your full name, school or college, city, and code, will be removed from our website.