Saturday 5 October 2013

Matrix Subtraction using C

#include<stdio.h>
#include<conio.h>
main()
{
int i,j,a[10][10],b[10][10],c[10][10],m1,n1,m2,n2;
printf("\nEnter the number of Rows of Mat1 : ");
scanf ("%d",&m1);

printf("\nEnter the number of Columns of Mat1 : ");
scanf ("%d",&n1);

for( i = 0 ; i < m1 ; i++ )
    for( j = 0 ; j < n1 ; j++ )
    {
    printf("\nEnter the Element a[%d][%d] : ",i,j);
    scanf("%d",&a[i][j]);
    }

// ------------------------------------------

printf("\nEnter the number of Rows of Mat2 : ");
scanf ("%d",&m2);

printf("\nEnter the number of Columns of Mat2 : ");
scanf ("%d",&n2);

if ( m1 != m2 || n1 != n2 )
 {
 printf("\nOrder of two matrices is not same ");
 exit(0);
 }

for( i = 0 ; i < m2 ; i++ )
    for( j = 0 ; j < n2 ; j++ )
    {
    printf("\nEnter the Element b[%d][%d] : ",i,j);
    scanf("%d",&b[i][j]);
    }

for( i = 0 ; i < m1 ; i++ )
    for( j = 0 ; j < n1 ; j++ )
    {
    c[i][j] = a[i][j] - b[i][j] ;
    }

printf("\n Matrix after Subtraction : \n");

for( i = 0 ; i < m1 ; i++ )
 {
    for( j = 0 ; j < n1 ; j++ )
    {
    printf("%d\t",c[i][j]);
    }
  printf("\n");
 }
}

No comments:

Post a Comment