Saturday, February 6, 2016

Matrix Addition using C program

#include<stdio.h>
int main()
{
int a[3][3],b[3][3],c[3][3],i, j;
 //Loops for Reading A matrix
for ( i = 0; i < 3; i++ ) //related to row
{
for ( j = 0; j < 3; j++ ) //related to column
{
printf("Matrix A[%d][%d]:",i,j);
scanf("%d", &a[i][j]) ;
}
}
 //Loops for Reading B matrix
for ( i = 0; i < 3; i++ ) //related to row
{
for ( j = 0; j < 3; j++ ) //related to column
{
printf("Matrix B[%d][%d]:",i,j);
scanf("%d", &b[i][j]) ;
}
}
printf("Matrix A is as under: \n") ;
/* Print Matrix A, B */
for ( i = 0; i < 3; i++ ) //related to row
{
for ( j = 0; j < 3; j++ ) //related to column
{
printf(" %d\t ",a[i][j] );
}
printf("\n");
}
printf("Matrix B is as under: \n") ;
for ( i = 0; i < 3; i++ ) //related to row
{
for ( j = 0; j < 3; j++ ) //related to column
{
printf(" %d\t ",b[i][j] );
}
printf("\n");
}
printf("Matrix Addition is as under: \n");
for ( i = 0; i < 3; i++ ) //related to row
{
for ( j = 0; j < 3; j++ ) //related to column
{
printf(" %d\t ",a[i][j] + b[i][j] );
}
printf("\n");
}
 return 0;
}


Output of program

Matrix A[0][0]:1
Matrix A[0][1]:1
Matrix A[0][2]:1
Matrix A[1][0]:2
Matrix A[1][1]:2
Matrix A[1][2]:2
Matrix A[2][0]:3
Matrix A[2][1]:3
Matrix A[2][2]:3
Matrix B[0][0]:4
Matrix B[0][1]:4
Matrix B[0][2]:4
Matrix B[1][0]:5
Matrix B[1][1]:5
Matrix B[1][2]:5
Matrix B[2][0]:6
Matrix B[2][1]:6
Matrix B[2][2]:6
Matrix A is as under:
 1        1       1
 2        2       2
 3        3       3
Matrix B is as under:
 4        4       4
 5        5       5
 6        6       6
Matrix Addition is as under:
 5        5       5
 7        7       7
 9        9       9

No comments:

Post a Comment