C program for addition of two matrices using arrays.

"Array in C"

/*C program for addition of two matrices using arrays*/
#include<stdio.h>
#include<conio.h>
void main()
{
  int a[3][3],b[3][3],c[3][3],i,j;
  printf("Enter the First matrix->");
  for(i=0;i<3;i++)
for(j=0;j<3;j++)
 scanf("%d",&a[i][j]);
  printf("\nEnter the Second matrix->");
  for(i=0;i<3;i++)
for(j=0;j<3;j++)
 scanf("%d",&b[i][j]);
  printf("\nThe First matrix is\n");
  for(i=0;i<3;i++){
printf("\n");
for(j=0;j<3;j++)
 printf("%d\t",a[i][j]);
  }
  printf("\nThe Second matrix is\n");
  for(i=0;i<3;i++){
printf("\n");
for(j=0;j<3;j++)
printf("%d\t",b[i][j]);
}
for(i=0;i<3;i++)
for(j=0;j<3;j++)
c[i][j]=a[i][j]+b[i][j];
printf("\nThe Addition of two matrix is\n");
for(i=0;i<3;i++){
printf("\n");
for(j=0;j<3;j++)
printf("%d\t",c[i][j]);
}
getch();
}

Expected Output:
Enter the First matrix->
3
3
3
3
3
3
3
3
3
The First matrix is
3   3   3
3   3   3
3   3   3
 Enter the Second matrix->
5
5
5
5
5
5
5
5
5
The Second matrix is
5   5   5
5   5   5
5   5   5

The Addition of two matrix is
8   8   8   
8   8   8 
8   8   8

OR

#include <stdio.h>
#include <conio.h>
void main()
{
int m, n, c, d, first[10][10], second[10][10], sum[10][10];

printf("Enter the number of rows and columns of matrix\n");
scanf("%d%d", &m, &n);
printf("Enter the elements of first matrix\n");

for ( c = 0 ; c < m ; c++ )
for ( d = 0 ; d < n ; d++ )
scanf("%d", &first[c][d]);

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

for ( c = 0 ; c < m ; c++ )
for ( d = 0 ; d < n ; d++ )
scanf("%d", &second[c][d]);

for ( c = 0 ; c < m ; c++ )
for ( d = 0 ; d < n ; d++ )
sum[c][d] = first[c][d] + second[c][d];

printf("Sum of entered matrices:-\n");

for ( c = 0 ; c < m ; c++ )
{
for ( d = 0 ; d < n ; d++ )
printf("%d\t", sum[c][d]);

printf("\n");
}

getch();
}
Expected Output:
Enter the number of rows and columns of matrix
2
2
Enter the elements of first matrix
4
4
4
4
Enter the elements of second matrix
5
5
5
5
Sum of entered matrices:-
9    9  
9    9

Post a Comment

Previous Post Next Post