Add two matrix in C programs is given by the user, And also given the range by the users. It is a simple methods of additions of two matrix.
C programming code
#include<stdio.h>
#include<conio.h>
void main()
{
int a[3][3], b[3][3], sum[3][3], i, j, n;
clrscr();
printf("\n Enter the number of same rows and columns of matrix ");
scanf("%d",&n);
printf("\n Enter the elements of first matrix\n");
for ( i = 0 ; i < n ; i++ )
for ( j = 0 ; j < n ; j++ )
scanf("%d",&a[i][j]);
printf("\n Enter the elements of second matrix\n");
for ( i = 0 ; i < n ; i++ )
for ( j = 0 ; j < n ; j++ )
scanf("%d",&b[i][j]);
for ( i = 0 ; i < n ; i++ )
for ( j = 0 ; j < n ; j++ )
sum[i][j] = a[i][j]+ b[i][j];
printf("\n Sum of entered matrices:-\n");
for ( i = 0 ; i < n ; i++ )
{
for ( j = 0 ; j < n ; j++ )
printf("%5d",sum[i][i]);
printf("\n");
}
getch();
}
Output:-

C programming code
#include<stdio.h>
#include<conio.h>
void main()
{
int a[3][3], b[3][3], sum[3][3], i, j, n;
clrscr();
printf("\n Enter the number of same rows and columns of matrix ");
scanf("%d",&n);
printf("\n Enter the elements of first matrix\n");
for ( i = 0 ; i < n ; i++ )
for ( j = 0 ; j < n ; j++ )
scanf("%d",&a[i][j]);
printf("\n Enter the elements of second matrix\n");
for ( i = 0 ; i < n ; i++ )
for ( j = 0 ; j < n ; j++ )
scanf("%d",&b[i][j]);
for ( i = 0 ; i < n ; i++ )
for ( j = 0 ; j < n ; j++ )
sum[i][j] = a[i][j]+ b[i][j];
printf("\n Sum of entered matrices:-\n");
for ( i = 0 ; i < n ; i++ )
{
for ( j = 0 ; j < n ; j++ )
printf("%5d",sum[i][i]);
printf("\n");
}
getch();
}
Output:-

No comments:
Post a Comment