# c program to add two matrices

list of programs below:  c program to add two matrices using functions,
c program to add two matrices without functions

Rule: two matrices can only be added if both are in same order.

 Matrix Aa b c d Matrix B1 2 3 4 Resultant Matrix of Addition of two matrices A and B area + 1 , b +2 c + 3 , d + 4

## Write a C program to add two matrices without functions

Write a C program to find addition of two matrices without functions

#include<stdio.h>
void main()
{
int a[5][5],b[5][5],c[5][5],i,j,k,sum=0,m,n,o,p;
//your can specify maximum 5 x 5 matrix with above initialization
printf(“\n To find addition of two matrices”);
printf(“\n Enter the no of rows and columns of first matrix”);
scanf(“%d %d”,&m,&n);
printf(“\n Enter the no of rows and columns of matrix”);
scanf(“%d %d”,&o,&p);
if(m!=o || n!=p)
{
printf(“\n rows and columns of first matrix must be same as rows and columns of second matrix”);
}
else
{
printf(“\n Enter the First matrix->”);
for(i=0;i<m;i++)
for(j=0;j<n;j++)
scanf(“%d”,&a[i][j]);
printf(“\n Enter the Second matrix->”);
for(i=0;i<o;i++)
for(j=0;j<p;j++)
scanf(“%d”,&b[i][j]);
printf(“\nThe First matrix is\n”);
for(i=0;i<m;i++){
printf(“\n”);
for(j=0;j<n;j++){
printf(“%d\t”,a[i][j]);
}
}
printf(“\n The Second matrix is\n”);
for(i=0;i<o;i++){
printf(“\n”);
for(j=0;j<p;j++){
printf(“%d\t”,b[i][j]);
}
}
printf(“\n The sum of two matrices is\n”);
for(i=0;i<m;i++)
{
//row of first matrix
for(j=0;j<p;j++)
{//column of second matrix
{
c[i][j]=a[i][j]+b[i][j];
}
for(i=0;i<m;i++)
{
printf(“\n”);for(j=0;j<p;j++)
{
printf(“%d\t”,c[i][j]);
}
}
}

## Write a C program to find sum of two matrices using functions

Write a C program to add  two matrices using functions / write a c program to find sum of two matrices using functions

#include<stdio.h>
void main()
{
int a[5][5],b[5][5],c[5][5],i,j,k,sum=0,m,n,o,p;
//your can specify maximum 5 x 5 matrix with above initialization
printf(“\n To find addition of two matrices”);
printf(“\n Enter the no of rows and columns of first matrix”);
scanf(“%d %d”,&m,&n);
printf(“\n Enter the no of rows and columns of matrix”);
scanf(“%d %d”,&o,&p);
if(m!=o || n!=p)
{
printf(“\n rows and columns of first matrix must be same as rows and columns of second matrix”);
}
else
{
printf(“\n Enter the First matrix->”);
for(i=0;i<m;i++)
for(j=0;j<n;j++)
scanf(“%d”,&a[i][j]);
printf(“\n Enter the Second matrix->”);
for(i=0;i<o;i++)
for(j=0;j<p;j++)
scanf(“%d”,&b[i][j]);
printf(“\n The First matrix is\n”);
display_matrix(a[5][5], m, n );
printf(“\n The second matrix is\n”);
display_matrix(b[5][5], m, n );
printf(“\n The sum of two matrices is\n”);
}
getch();
}

// function to display matrices

void display_matrix(int a[5][5], int m, int n )
{
int i,j;
for(i=0;i<m;i++)
{
printf(“\n”);
for(j=0;j<n;j++)
{
printf(“%d\t”,a[i][j]);
}
}
}

void Addition_matrix(int a[5][5], int b[5][5],int  m, int n )

{
int i,j;
for(i=0;i<m;i++)
{
printf(“\n”);
for(j=0;j<n;j++)
{
printf(“%d\t”,a[i][j] + b[i][j]);
}
}
}