Thursday, 21 January 2021

Write a Program in C++ to find product of 2 matrices.

 //Program to find product of 2 matrices

#include<iostream>

using namespace std;

main()

{

  int A[10][10],B[10][10],C[10][10],i,j,k,n;

  cout<<"Enter the size of the Matrix\n";

  cin>>n;

  cout<<"Enter the elements of Matrix A\n";

  for(i=0;i<n;i++)

  {

    for(j=0;j<n;j++)

    cin>>A[i][j];

  }

  cout<<"Enter the elements of Matrix B\n";

  for(i=0;i<n;i++)

  {

    for(j=0;j<n;j++)

    cin>>B[i][j];

  }

  for(i=0;i<n;i++)

   for(j=0;j<n;j++)

  {

     C[i][j]=0;

     for(k=0;k<n;k++)

     C[i][j]=C[i][j]+A[i][k]*B[k][j];

  }

  cout<<"The product of the 2 matrices :\n";

  for(i=0;i<n;i++)

  {

    for(j=0;j<n;j++)

    cout<<C[i][j]<<" ";

    cout<<endl;

  }

}

No comments:

Post a Comment

Java Program to count the total number of characters in a string

 //Java Program to count the total number of characters in a string import java.util.*; public class CountChars {     public static void mai...