MULTIPLICATION OF TWO MATRICES
#include<stdio.h>
int main() {
int a[3][3],b[3][3],c[3][3],i,j,k,sum,m,n,p,q;
printf("Enter the Rows and Columns of the Matrix: ");
scanf("%d %d",&m,&n);
printf("Enter first Matrix:\n");
for(i=0;i<m;i++) {
for(j=0;j<n;j++) {
scanf("%d",&a[i][j]);
printf("Enter the rows and columns of the second matrix:\n");
scanf("%d %d",&p,&q);
printf("\nenter the second matrix:\n");
for(i=0;i<p;i++) {
for(j=0;j<q;j++) {
scanf("%d",&b[i][j]);
printf("\nFirst matrix is:\n");
for(i=0;i<m;i++) {
for(j=0;j<n;j++) {
printf("%d\t",a[i][j]);
printf("\n");
printf("second matrix is:\n");
for(i=0;i<p;i++) {
for(j=0;j<q;j++) {
printf("%d\t",b[i][j]);
printf("\n");
if(n!=p)
printf("Multiplication is not possible");
else
for(i=0;i<m;i++)
for(j=0;j<q;j++)
sum=0;
for(k=0;k<m;k++)
sum=sum+(a[i][k]*b[k][j]);
c[i][j]=sum;
printf("multiplication is:\n");
for(i=0;i<m;i++)
for(j=0;j<q;j++)
printf("%d\t",c[i][j]);
printf("\n");
}
Output:
Enter the Rows and Columns of the Matrix: 2
Enter first Matrix:
Enter the rows and columns of the second matrix:
enter the second matrix:
First matrix is:
1 1
1 1
second matrix is:
1 1
1 1
multiplication is:
2 2
2 2
ADDITION OF TWWO MATRICES
#include<stdio.h>
void main() {
int a[2][2],b[2][2],i,j;
for(i=0;i<2;i++) {
for(j=0;j<2;j++) {
printf("Enter the element: ");
scanf("%d",&a[i][j]);
printf("Next Matrix\n");
for(i=0;i<2;i++) {
for(j=0;j<2;j++) {
printf("Enter the element: ");
scanf("%d",&b[i][j]);
for(i=0;i<2;i++) {
for(j=0;j<2;j++) {
printf("%d\t", a[i][j]+b[i][j]);
printf("\n");
Output:
Enter the element: 1
Enter the element: 2
Enter the element: 3
Enter the element: 4
Next Matrix
Enter the element: 5
Enter the element: 6
Enter the element: 7
Enter the element: 8
6 8
10 12