C Online Compiler
Example: C program to add two Matrices using Multi-dimensional Arrays
C
C++
C#
Java
Python
PHP
main.c
STDIN
Run
// C program to add two Matrices using Multi-dimensional Arrays #include <stdio.h> // It's main function of the program int main() { int rows, columns; printf("Enter No. of Row & Column:\n"); scanf("%d %d", &rows, &columns); int matrix_1[rows][columns]; int matrix_2[rows][columns]; int matrix_sum[rows][columns]; // Step-1 Enter the values of first matrix printf("\nEnter the values of the first matrix - matrix_1\n"); for (int i = 0; i < rows; i++) { for (int j = 0; j < columns; j++) { printf("Enter value - M1[%d][%d]: ", i, j); scanf("%d", &matrix_1[i][j]); } } // Step-1 Enter the values of second matrix printf("\nEnter the values of the second matrix - matrix_1\n"); for (int i = 0; i < rows; i++) { for (int j = 0; j < columns; j++) { printf("Enter value - M2[%d][%d]: ", i, j); scanf("%d", &matrix_2[i][j]); } } // Step-3 Add the matrices M1 & M2 and store these values in another matrices `matrix_sum` for (int i = 0; i < rows; i++) { for (int j = 0; j < columns; j++) { matrix_sum[i][j] = matrix_1[i][j] + matrix_2[i][j]; } } // Step-4 Final output of the program to print the final matrices printf("\nSUM of two matrices matrix_1 & matrix_2\n"); for (int i = 0; i < rows; i++) { for (int j = 0; j < columns; j++) { printf("%d\t", matrix_sum[i][j]); } printf("\n"); } return 0; }
3 4 12 4 6 7 20 3 6 80 45 25 60 10 10 20 30 90 70 50 45 80 14 23 45 92
Output
Clear
ADVERTISEMENTS