Welcome To Uday Satya Blog

01 - 05 PROGRAMS


/* ****************************************************
 1 . TO READ & ENTER ELEMENTS INTO THE MATRIX   **************************************************** */
#include<*stdio.h>
#include<*conio.h>
void main()
{
int a[3][3],i,j;
clrscr();
printf("\n enter the elements into the matrix : ");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("\n the matrix is : ");
printf("\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf("\t %d",a[i][j]);
}
printf("\n");
}
getch();
}

output

 enter the elements into the matrix : 1 1 1 1 1 1 1 1 1

 the matrix is :
         1       1       1
         1       1       1
         1       1       1

/* *****************************************
                2 . TO ADD TWO MATRICES
    ***************************************** */
#include<*stdio.h>
#include<*conio.h>
void main()
{
int a[3][3],b[3][3],c[3][3],i,j;
clrscr();
printf("\n enter the elements into the matrix 'A' : ");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("\n enter the elements into the matrix 'B' : ");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
scanf("%d",&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("\n the sum of two matrices 'A' and 'B' is : ");
printf("\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf("\t %d",c[i][j]);
}
printf("\n");
}
getch();
}

output

 enter the elements into the matrix 'A' : 1 1 1 1 1 1 1 1 1

 enter the elements into the matrix 'B' : 1 1 1 1 1 1 1 1 1

 the sum of two matrices 'A' and 'B' is :
         2       2       2
         2       2       2
         2       2       2

/* *********************************************
                3 . TO SUBSTRACT TWO MATRICES.
    ******************************************** */
#include<*stdio.h>
#include<*conio.h>
void main()
{
int a[3][3],b[3][3],c[3][3],i,j;
clrscr();
printf("\n enter the elements into the matrix 'A' : ");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("\n enter the elements into the matrix 'B' : ");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
scanf("%d",&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("\n the substraction of two matrices 'A' and 'B' is : ");
printf("\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf("\t %d",c[i][j]);
}
printf("\n");
}
getch();
}

 output

 enter the elements into the matrix 'A' : 1 1 1 1 1 1 1 1 1
 enter the elements into the matrix 'B' : 1 1 1 1 1 1 1 1 1
 the substraction of two matrices 'A' and 'B' is :
         0       0       0
         0       0       0
         0       0       0

/* *********************************************
                4 . TO MULTIPLY TWO MATRICES.
    ******************************************** */
#include<*stdio.h>
#include<*conio.h>
void main()
{
int a[3][3],b[3][3],c[3][3],i,j;
clrscr();
printf("\n enter the elements into the matrix 'A' : ");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("\n enter the elements into the matrix 'B' : ");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
scanf("%d",&b[i][j]);
}
}
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
c[i][j]=0;
c[i][j]=c[i][j]+(a[i][j]*b[j][i]);
}
}
printf("\n the multiplication of two matrices 'A' and 'B' is : ");
printf("\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf("\t %d",c[i][j]);
}
printf("\n");
}
getch();
}

 output

 enter the elements into the matrix 'A' : 1 1 1 1 1 1 1 1 1

 enter the elements into the matrix 'B' : 1 1 1 1 1 1 1 1 1

 the multiplication of two matrices 'A' and 'B' is :
         1       1       1
         1       1       1
         1       1       1

/* ***************************************************
   5 . TO TAKE DIMENSIONS OF MATRIX & ADD THEM
    ************************************************ */
#include<*stdio.h>
#include<*conio.h>
void main()
{
int a[3][3],b[3][3],c[3][3],i,j,m,n,p,q;
clrscr();
printf("\n enter the dimensions of matrix 'A' : ");
scanf("%d %d",&m,&n);
printf("\n enter the dimensions of matrix 'B' : ");
scanf("%d %d",&p,&q);
if((m==p)&&(n==q))
{
printf("\n matrix addition is possible");
printf("\n");
printf("\n enter the elements into the matrix 'A' : ");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("\n enter the elements into the matrix 'B' : ");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
scanf("%d",&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("\n the sum of two matrices 'A' and 'B' is : ");
printf("\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf("\t %d",c[i][j]);
}
printf("\n");
}
}
else
{
printf("\n matrix addition is not possible");
}
getch();
}

outputs

enter the dimensions of matrix 'A' : 3 3

 enter the dimensions of matrix 'B' : 3 3

 matrix addition is possible

 enter the elements into the matrix 'A' : 1 1 1 1 1 1 1 1 1

 enter the elements into the matrix 'B' : 1 1 1 1 1 1 1 1 1

 the sum of two matrices 'A' and 'B' is :
                             2       2       2
                             2       2       2
                             2       2       2

 enter the dimensions of matrix 'A' : 2 3

 enter the dimensions of matrix 'B' : 3 2

 matrix addition is not possible

0 comments:

Post a Comment