C program for addition of 2 matrices using arrays concept. This source code is all about matrices and their calculation. This is based on 2×2 matrix addition. So that, you have to enter totally 8 inputs for the 2 matrices. And the output will produce an another matrix. Source code of this C tutorial is given below,
ADDITION OF TWO MATRICES IN C
This C program is used to adding the two matrix methods. An addition operation will be in a row and column wise for the matrix program. It is also a simple code like mathematical matrix. For example,
First Matrix value:-
1 2
3 4
Second matrix value:-
5 6
2 1
Then output of the Matrix addition is:-
09 08
23 22
[Read: Software Company List] & [Aptitude online portal]
MATRIX ADDITION IN C:
Q: Write a C program to calculate the addition of two matrices using Arrays.
//Header files #include<stdio.h> #define MAX 5 void main() { //Program variables int ma1[MAX][MAX],ma2[MAX][MAX],ma3[MAX][MAX]; int i,j; int a,b,a1,b1; printf("Enter the row and column of a matrix"); //Display function scanf("%d%d",&a,&b); //Getting input function printf("Enter the value of first matrix"); for(j=0;j<=(b-1);b++)//Looping statement { for(i=0;i<=(a-1);i++)//Nested for loop { scanf("%d",&matrix1[i][j]); } } printf("enter the size of second matrix"); scanf("%d%d",&a1,&b1); if(b!=a1)//Conditional statement { printf("Size of second matrix is not compatible"); exit(1); } else { printf("Enter value of second matrix"); for(j=0;j<=(b1-1);b++) { for(i=0;i<=(a1-1);i++) { scanf("%d",&ma1[i][j]); } } } for(j=0;j<=(b-1);b++) { for(i=0;i<=(a-1);i++) { ma3[i][j]=ma1[i][j]+ma2[i][j]; printf("Addition of matrix is=%d ",ma3[i][j]); } } }