C Program to add two matrices using pointers.

 #include<stdio.h>

main()

{

int a[5][5],b[5][5],c[5][5],i,j,m,n;

printf("\n Enter the Dimension of the Matrix:\n");

scanf("%d%d",&m,&n);

printf("\nEnter the Elements of the first matix:");

for(i=0;i<m;i++)

{

for(j=0;j<n;j++)

{

scanf("%d",(*(a+i)+j));

}

}

printf("\nEnter the elements of the second matrix:\n");

for(i=0;i<m;i++)

{

for(j=0;j<n;j++)

{

scanf("%d",(*(b+i)+j));

}

}

for(i=0;i<m;i++)

{

for(j=0;j<n;j++)

{

*(*(c+i)+j)=*(*(a+i)+j)+ *(*(b+i)+j);

}

}

printf("\nResultantMatrix:\n");

for(i=0;i<m;i++)

{

for(j=0;j<n;j++)

{

printf("%5d",*(*(c+i)+j));

}

printf("\n");

}

getch();

}






Output:



  1.  Enter the Dimension of the Matrix:

2  2


Enter the Elements of the first matix:

1   2   3   4


Enter the elements of the second matrix:

9   8   7  6

ResultantMatrix:

   10   10

   10   10



  1.  Enter the Dimension of the Matrix:

3  2


Enter the Elements of the first matix:

4  2  6  3  2  1


Enter the elements of the second matrix:

7  8  9  1  3  3


ResultantMatrix:

   11   10

   15    4

    5    4


Post a Comment

Previous Post Next Post