C Programming
- Print integer
- Add & subtract
- Odd & even
- Multiply & divide
- Check vowels
- Letter grade program
- Leap year program
- Sum of digits
- Prime numbers
- Sum of numbers
- Find factorial
- Fibonacci series
- Fibonacci triangle
- Number triangle
- Alphabet triangle
- Armstrong numbers
- Palindrome numbers
- Swap number
- Reverse number
- Decimal to binary
- Assembly code in C
- Matrix multiplication
- HCF & LCM
- nCr & nPr
- Print pattern
- Floyd’s triangle
- Pascal triangle
- Count digits
- Strong numbers
- Perfect numbers
- Sum of natural number
- Binary to decimal
- Decimal to octal
- Octal to decimal
- Binary to octal
- Octal to binary
- Add using pointer
- Bubble sort
- Insertion sort
- Selection sort
- Quick sort
- Binary search
- Linear search
- Largest element of array
- Smallest element of array
- Reverse Array
- Variable size & limit
- ASCII value of character
- Sum of array elements
- Number of element in array
- Merge two array
- Insert element in array
- Delete element from array
- Add two matrix
- Transpose matrix
- Print string
- Reverse string
- Delete vowels
- Sort string
- Remove spaces
- Swap string
- Random numbers
- Print date & time
- Print IP address
- Shut down computer
Addition of two matrix in C
In this article we will make some C programs to add two matrix or subtract two matrix using C programming language. We will take two matrix as input from the user and them add them and print another matrix which will be the sum of first two matrix. Consider a user gives two matrix A and B as like bellow;
Matrix A is,
3 4 5
6 7 8
2 3 4
And matrix B is,
1 2 3
4 5 6
7 8 9
So, the sum of two matrix A + B will be,
4 6 8
10 12 14
9 11 13
We can write the code in C for this purpose of add two matrix in different way. You can also use function or recursion for this purpose. Let’s see a program bellow which will determine the sum to two matrix A and B.
We recommend you to learn our two dimensional array chapter to get better understanding here.
Note : The row and column of first matrix and the row and column of second matrix must have to be equal to add them. Otherwise we can not able to add them here.
C program to add two matrix
// c add two matrix program
#include <stdio.h>
int main(){
int first_arr[20][20], second_arr[20][20], result_arr[20][20], row, col, x, y;
printf("Enter how many row and column of each matrix : ");
scanf("%d %d", &row, &col);
printf("\nEnter the elements for first matrix here : \n");
for(x = 0; x < row; x++){
for(y = 0; y < col; y++){
scanf("%d", &first_arr[x][y]);
}
}
printf("\nEnter the elements for second matrix here : \n");
for(x = 0; x < row; x++){
for(y = 0; y < col; y++){
scanf("%d", &second_arr[x][y]);
}
}
printf("\nSum of two matrix is as follows : \n");
for(x = 0; x < row; x++) {
for(y = 0 ; y < col; y++) {
result_arr[x][y] = first_arr[x][y] + second_arr[x][y];
printf("%d\t", result_arr[x][y]);
}
printf("\n");
}
return 0;
}
Output of this add matrix program:

Similarly you can subtract a matrix from other. Everything will be same for subtract a matrix from another. See the program for subtracting a matrix from other.
C program to subtract two matrix
// c program to subtract two matrix
#include <stdio.h>
int main(){
int first_arr[20][20], second_arr[20][20], result_arr[20][20], row, col, x, y;
printf("Enter how many row and column of each matrix : ");
scanf("%d %d", &row, &col);
printf("\nEnter the elements for first matrix here : \n");
for(x = 0; x < row; x++){
for(y = 0; y < col; y++){
scanf("%d", &first_arr[x][y]);
}
}
printf("\nEnter the elements for second matrix here : \n");
for(x = 0; x < row; x++){
for(y = 0; y < col; y++){
scanf("%d", &second_arr[x][y]);
}
}
printf("\nResult matrix after subtracting is : \n");
for(x = 0; x < row; x++) {
for(y = 0 ; y < col; y++) {
result_arr[x][y] = first_arr[x][y] - second_arr[x][y];
printf("%d\t", result_arr[x][y]);
}
printf("\n");
}
return 0;
}
Output of subtract matrix program:
Enter how many row and column of each matrix : 3 3
Enter the elements for first matrix here :
9 8 7
8 7 6
7 6 5
Enter the elements for second matrix here :
1 2 3
2 3 4
3 4 5
Result matrix after subtracting is :
8 6 4
6 4 2
4 2 0
Previous page: Delete element from array
Next page: Transpose matrix
Recommended for you: