Program to determine the product of two matrices

#include <stdio.h>
main()
{
 /*Program to determine the product of two matrices*/
 int m=0, n=0, p=0, A[100][100], B[100][100], C[100][100], i=0, j=0, k=0, temp=0;
 printf("\nEnter number of rows for matrix A: ");
 scanf("%d", &m);
 printf("\nEnter number of columns for matrix A: ");
 scanf("%d", &n);
 printf("\nEnter number of columns for matrix B: ");
 scanf("%d", &p);
 /*Read the elements for matrix A*/
 for(i=0; i<m; i++)/*row counter*/
 {
  for(j=0; j<n; j++)/*column counter*/
  {
   printf("\nEnter element A[%d][%d]: ",(i+1),(j+1));
   scanf("%d",&A[i][j]);
  }
 }
 /*Read the elements for matrix B*/
 for(i=0; i<n; i++)/*row counter*/
 {
  for(j=0; j<p; j++)/*column counter*/
  {
   printf("\nEnter element B[%d][%d]: ",(i+1),(j+1));
   scanf("%d", &B[i][j]);
  }
 }
 /*Compute the product matrix C*/
 for(i=0; i<m; i++)/*row counter*/
 {
  for(j=0; j<p; j++)/*column counter*/
  {
   temp = 0;
   for(k=0; k<n; k++)
   {
    temp = temp + ((A[i][k])*(B[k][j]));
   }
   C[i][j] = temp;
  }
 }
 /*Display matrix A*/
 printf("\nMatrix A = \n");
 for(i=0; i<m; i++)/*row counter*/
 {
  for(j=0; j<n; j++)/*column counter*/
  {
   printf("%4d", A[i][j]);
  }
  printf("\n");
 }
 /*Display matrix B*/
 printf("Matrix B = \n");
 for(i=0; i<n; i++)/*row counter*/
 {
  for(j=0; j<p; j++)/*column counter*/
  {
   printf("%4d", B[i][j]);
  }
  printf("\n");
 }
 /*Display matrix C*/
 printf("Matrix C = \n");
 for(i=0; i<m; i++)/*row counter*/
 {
  for(j=0; j<p; j++)/*column counter*/
  {
   printf("%4d", C[i][j]);
  }
  printf("\n");
 }
}
Advertisements
This entry was posted in Information Technology. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s