Matrix Multiplication in C Last Updated : 01 Aug, 2023 Comments Improve Suggest changes Like Article Like Report A matrix is a collection of numbers organized in rows and columns, represented by a two-dimensional array in C. Matrices can either be square or rectangular. In this article, we will learn the multiplication of two matrices in the C programming language. ExampleInput: mat1[][] = {{1, 2}, {3, 4}} mat2[][] = {{5, 6}, {7, 8}} Multiplication of two matrices: {{1*5 + 2*7 1*6 + 2*8}, {3*5 + 4*7 3*6 + 4*8}} Output: {{19, 22}, {43, 50}}Recommended: Please solve it on "PRACTICE" first, before moving on to the solution. Multiplication of two matrices is done by multiplying corresponding elements from the rows of the first matrix with the corresponding elements from the columns of the second matrix and then adding these products. Note: The number of columns in the first matrix must be equal to the number of rows in the second matrix. C Program to Multiply Two Matrices We use 2D Arrays and pointers in C to multiply matrices. Please refer to the following post as a prerequisite for the code. How to pass a 2D array as a parameter in C? C // C program to multiply two matrices #include <stdio.h> #include <stdlib.h> // matrix dimensions so that we dont have to pass them as // parametersmat1[R1][C1] and mat2[R2][C2] #define R1 2 // number of rows in Matrix-1 #define C1 2 // number of columns in Matrix-1 #define R2 2 // number of rows in Matrix-2 #define C2 3 // number of columns in Matrix-2 void multiplyMatrix(int m1[][C1], int m2[][C2]) { int result[R1][C2]; printf("Resultant Matrix is:\n"); for (int i = 0; i < R1; i++) { for (int j = 0; j < C2; j++) { result[i][j] = 0; for (int k = 0; k < R2; k++) { result[i][j] += m1[i][k] * m2[k][j]; } printf("%d\t", result[i][j]); } printf("\n"); } } // Driver code int main() { // R1 = 4, C1 = 4 and R2 = 4, C2 = 4 (Update these // values in MACROs) int m1[R1][C1] = { { 1, 1 }, { 2, 2 } }; int m2[R2][C2] = { { 1, 1, 1 }, { 2, 2, 2 } }; // if coloumn of m1 not equal to rows of m2 if (C1 != R2) { printf("The number of columns in Matrix-1 must be " "equal to the number of rows in " "Matrix-2\n"); printf("Please update MACROs value according to " "your array dimension in " "#define section\n"); exit(EXIT_FAILURE); } // Function call multiplyMatrix(m1, m2); return 0; } OutputResultant Matrix is: 3 3 3 6 6 6 Complexity Analysis Time complexity: O(n3). It can be optimized using Strassen’s Matrix MultiplicationAuxiliary Space: O(m1 * n2) For more information, refer to the article - Program to multiply two matrices Comment More infoAdvertise with us Next Article Matrix Multiplication in C kartik Follow Improve Article Tags : Mathematical Matrix C Programs C Language School Programming DSA Paytm C Array Programs +4 More Practice Tags : PaytmMathematicalMatrix Similar Reads DSA Tutorial - Learn Data Structures and Algorithms DSA (Data Structures and Algorithms) is the study of organizing data efficiently using data structures like arrays, stacks, and trees, paired with step-by-step procedures (or algorithms) to solve problems effectively. Data structures manage how data is stored and accessed, while algorithms focus on 7 min read C Programming Language Tutorial C is a general-purpose mid-level programming language developed by Dennis M. Ritchie at Bell Laboratories in 1972. It was initially used for the development of UNIX operating system, but it later became popular for a wide range of applications. Today, C remains one of the top three most widely used 5 min read Quick Sort QuickSort is a sorting algorithm based on the Divide and Conquer that picks an element as a pivot and partitions the given array around the picked pivot by placing the pivot in its correct position in the sorted array. It works on the principle of divide and conquer, breaking down the problem into s 12 min read Merge Sort - Data Structure and Algorithms Tutorials Merge sort is a popular sorting algorithm known for its efficiency and stability. It follows the divide-and-conquer approach. It works by recursively dividing the input array into two halves, recursively sorting the two halves and finally merging them back together to obtain the sorted array. Merge 14 min read Bubble Sort Algorithm Bubble Sort is the simplest sorting algorithm that works by repeatedly swapping the adjacent elements if they are in the wrong order. This algorithm is not suitable for large data sets as its average and worst-case time complexity are quite high.We sort the array using multiple passes. After the fir 8 min read Data Structures Tutorial Data structures are the fundamental building blocks of computer programming. They define how data is organized, stored, and manipulated within a program. Understanding data structures is very important for developing efficient and effective algorithms. What is Data Structure?A data structure is a st 2 min read Breadth First Search or BFS for a Graph Given a undirected graph represented by an adjacency list adj, where each adj[i] represents the list of vertices connected to vertex i. Perform a Breadth First Search (BFS) traversal starting from vertex 0, visiting vertices from left to right according to the adjacency list, and return a list conta 15+ min read Binary Search Algorithm - Iterative and Recursive Implementation Binary Search Algorithm is a searching algorithm used in a sorted array by repeatedly dividing the search interval in half. The idea of binary search is to use the information that the array is sorted and reduce the time complexity to O(log N). Binary Search AlgorithmConditions to apply Binary Searc 15 min read Insertion Sort Algorithm Insertion sort is a simple sorting algorithm that works by iteratively inserting each element of an unsorted list into its correct position in a sorted portion of the list. It is like sorting playing cards in your hands. You split the cards into two groups: the sorted cards and the unsorted cards. T 9 min read Dijkstra's Algorithm to find Shortest Paths from a Source to all Given a weighted undirected graph represented as an edge list and a source vertex src, find the shortest path distances from the source vertex to all other vertices in the graph. The graph contains V vertices, numbered from 0 to V - 1.Note: The given graph does not contain any negative edge. Example 12 min read Like