Matrix Addition Subtraction in C Programming Language

2x2, 3x3, 4x4 upto 10x10 Matrix Addition, Subtraction Code for C Programming Language


Here this code uses two dimension array. Matrix operations like addition, multiplication subtraction is the best example for two dimensional array. This program will perform matrix addition and subtraction for the given matrix input. Here m1 and m2 array is used to store the matrix values. and r1,c2,r2,c2 is the row, column value count for matrix one and two respectively. Just copy and compile the below code in your c compiler.

void main(){
int m1[10][10],i,j,k,m2[10][10],add[10][10],sub[10][10],r1,c1,r2,c2;
printf("Enter row and column size of first matrix MAX 10\n");
scanf("%d%d",&r1,&c1);
printf("Enter row and column size of second matrix MAX 10\n");
scanf("%d%d",&r2,&c2);
if(r2==c1){
printf("Enter Matrix Element for First Matrix in Row column wise \n");
printf("Row wise\n");
for(i=0;i<r1;i++){
for(j=0;j<c1;j++)
scanf("%d",&m1[i][j]);
}
printf("Your Matrix one Inputs are as follow\n");
for(i=0;i<r1;i++){
for(j=0;j<c1;j++)
printf("%d\t",m1[i][j]);
printf("\n");
}
printf("Enter Matrix Element for Second Matrix in Row column wise\n");
for(i=0;i<r2;i++){
for(j=0;j<c2;j++)
scanf("%d",&m2[i][j]);
}
printf("Your Matrix two Inputs are as follow\n");
for(i=0;i<r2;i++)
{
for(j=0;j<c2;j++)
printf("%d\t",m2[i][j]);
printf("\n");
}
if(r1==r2&&c1==c2)
{
printf("Now we add and subtract both the above matrix \n");
printf("The result of the addition is as follows;\n");
for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++){
add[i][j]=m1[i][j]+m2[i][j];
sub[i][j]=m1[i][j]-m2[i][j];
printf("%d\t",add[i][j]);
}
printf("\n");
}
printf("The result of the subtraction is as follows;\n");
for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++){
printf("%d\t",sub[i][j]);
}
printf("\n");
}
}
else{
printf("Addition, Subtraction cannot be done as rows or columns are not equal\n");
}
}
}

You can use this online resource to verify your result
1. Matrix Addition Calculator
2. Matrix Subtraction Calculator


Related Topic Matrix Multiplication in C Programming Language
The Better Mobile Open Source Platform
Android Application Development SDK Dalvik Virtual Machine
JSP Variable Declaration Statement
JSP Scriptlets Tag for Variable Expressions and Statements Declaration

nScraps.com 2011   Privacy Policy  Terms of Service  Feedback