Advertisement
Programmin-in-Python

C Program for Matrix Addition

Jan 13th, 2022
593
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.87 KB | None | 0 0
  1. #include <stdio.h>
  2.  
  3. int main(){
  4.     int row,col;
  5.     printf("Enter the Number of Rows : ");
  6.     scanf("%d",&row);
  7.     printf("Enter the Number of Columns : ");
  8.     scanf("%d",&col);
  9.  
  10.     int a[row][col], b[row][col], c[row][col];
  11.  
  12.     //Storing Values
  13.     printf("Enter the elements of A :-\n");
  14.  
  15.     for(int i=0; i<row; i++){
  16.         for(int j=0; j<col; j++){
  17.             printf("A[%d][%d]=",i+1,j+1);
  18.             scanf("%d",&a[i][j]);
  19.         }
  20.     }
  21.     printf("\nEnter the elements of B :-\n");
  22.  
  23.     for(int i=0; i<row; i++){
  24.         for(int j=0; j<col; j++){
  25.             printf("B[%d][%d]=",i+1,j+1);
  26.             scanf("%d",&b[i][j]);
  27.         }
  28.     }
  29.  
  30.     //Matrix Addition
  31.     for(int i=0; i<row; i++){
  32.         for(int j=0; j<col; j++){
  33.             c[i][j] = (a[i][j]+b[i][j]);
  34.         }
  35.     }
  36.  
  37.     //Displaying Result
  38.     printf("\nResult of Addition:-\n");
  39.  
  40.     for(int i=0; i<row; i++){
  41.         for(int j=0; j<col; j++){
  42.             printf("%d ",c[i][j]);
  43.         }printf("\n");
  44.     }
  45.     return 0;
  46. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement