0% found this document useful (0 votes)
16 views

Program To Multiply Two Matrices

This C++ program multiplies two matrices. It prompts the user to enter the rows and columns of matrices A and B. If the number of columns of A matches the rows of B, the program accepts the elements, displays the matrices, and calculates their product C by iterating through A and B with a nested for loop. Otherwise, it outputs that the matrices cannot be multiplied.
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views

Program To Multiply Two Matrices

This C++ program multiplies two matrices. It prompts the user to enter the rows and columns of matrices A and B. If the number of columns of A matches the rows of B, the program accepts the elements, displays the matrices, and calculates their product C by iterating through A and B with a nested for loop. Otherwise, it outputs that the matrices cannot be multiplied.
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Program to multiply two Matrices:#include<iostream.

h>
#include<conio.h>
#include<process.h>
int main()
{
int A[10][10],B[10][10],C[10][10],m,n,p,q,k,i,j;
cout<<"\nEnter the rows & columns of matrix A:";
cin>>m>>n;
cout<<"\nEnter the rows & columns of matrix B:";
cin>>p>>q;
if(n==p)
{
cout<<"\nEnter the elements of matrix A:\n";
for(i=0;i<m;i++)
for(j=0;j<n;j++)
cin>>A[i][j];
cout<<"\nEnter the elements of matrix B:\n";
for(i=0;i<p;i++)
for(j=0;j<q;j++)
cin>>B[i][j];
cout<<"\nMatrix A is:";
for(i=0;i<m;i++)
{
cout<<"\n";
for(j=0;j<n;j++)
cout<<A[i][j]<<" ";
}
cout<<"\nMatrix B is:";
for(i=0;i<p;i++)
{
cout<<"\n";
for(j=0;j<q;j++)
cout<<B[i][j]<<" ";
}
cout<<"\nProduct of two matrices:";
for(i=0;i<m;i++)

{
cout<<"\n";
for(j=0;j<q;j++)
{
C[i][j]=0;
for(k=0;k<n;k++)
C[i][j]=C[i][j]+A[i][k]*B[k][j];
cout<<C[i][j]<<" ";
}
}
}
else
cout<<"\nMatrices cannot be multiplied.";
getch();
}

You might also like