C

Addition of Two Matrix in C

In this tutorial, we are going to see how to write a C program to add two matrix. Meaning to calculate the sum of two matrices and then display it. First, the user will be asked to enter the order of the matrix (number of rows and columns), then two matrices. For example, if a user enters an order of 2×2, this means two rows and two columns. For example:
 


Matrices are frequently used in programming to represent graphical data structures, to solve equations etc.
100-multiple-choice-questions-in-c-programming100 Multiple Choice Questions In C Programming – Part 1This collection of 100 Multiple Choice Questions and Answers (MCQs) In C Programming : Quizzes & Practice Tests with Answer focuses on “C Programming”.  …Read More
Addition of Two Matrix in C
#include<stdio.h>
 
int main()
{
  int matx1[10][10], matx2[10][10], matx3[10][10];
  int i,j,row,col;
  
  printf("How many rows and columns?\n");
  scanf("%d%d",&row,&col);
  
  printf("\nEnter the first matrix:\n");
  for(i=0; i < row; ++i)
    for(j = 0; j < col; ++j)
      scanf("%d",&matx1[i][j]);
      
  printf("\nEnter the second matrix:\n");
  for(i = 0; i < row; ++i)
    for(j = 0; j < col; ++j)
      scanf("%d",&matx2[i][j]);
  
  printf("\nMatrix after addition:\n");
  for(i = 0; i < row; ++i)
  {
    for(j=0; j < col; ++j)
    {
      matx3[i][j] = matx1[i][j] + matx2[i][j];
      printf("%d ",matx3[i][j]);
    }
    printf("\n");
  }
 
  return 0;
}

Output:

How many rows and columns?
2 2

Enter the first matrix:
1 2   
1 3

Enter the second matrix:
4 1
5 3

Matrix after addition:
5 3 
6 6

 

mcqMCQPractice competitive and technical Multiple Choice Questions and Answers (MCQs) with simple and logical explanations to prepare for tests and interviews.Read More

Leave a Reply

Your email address will not be published. Required fields are marked *