Write C++ Program to Add Two Matrices


I have used CodeBlocks compiler for debugging purpose. But you can use any C++ programming language compiler as per your availability.


techstudy.org

HP 15 Core i3 7th gen Laptop(4GB, 1TB HDD, Windows 10) | Rs. 31,490

Introduction

I have used CodeBlocks compiler for debugging purpose. But you can use any C++ programming language compiler as per your availability.


  1. #include <iostream>
  2. using namespace std;
  3.  
  4. int main()
  5. {
  6. int r, c, a[100][100], b[100][100], sum[100][100], i, j;
  7.  
  8. cout<<"Enter number of rows (between 1 and 100): ";
  9. cin>>r;
  10. cout<<"Enter number of columns (between 1 and 100): ";
  11. cin>>c;
  12.  
  13. cout<<"\nEnter elements of First matrix:\n";
  14.  
  15. for(i=0; i<r; ++i)
  16. for(j=0; j<c; ++j)
  17. {
  18. cout<<"Enter element a"<<i+1<<j+1<<": ";
  19. cin>>a[i][j];
  20. }
  21.  
  22. cout<<"Enter elements of Second matrix:\n";
  23. for(i=0; i<r; ++i)
  24. for(j=0; j<c; ++j)
  25. {
  26. cout<<"Enter element a"<<i+1<<j+1<<": ";
  27. cin>>b[i][j];
  28. }
  29.  
  30. // Adding Two matrices
  31.  
  32. for(i=0;i<r;++i)
  33. for(j=0;j<c;++j)
  34. {
  35. sum[i][j]=a[i][j]+b[i][j];
  36. }
  37.  
  38. // Displaying the result
  39. cout<<"\nSum of two matrix is: \n\n";
  40.  
  41. for(i=0;i<r;++i)
  42. for(j=0;j<c;++j)
  43. {
  44.  
  45. cout<<"\t"<<sum[i][j];
  46.  
  47. if(j==c-1)
  48. {
  49. cout<<"\n\n";
  50. }
  51. }
  52.  
  53. return 0;
  54. }
  55.  

Result

C++ language techstudy.org

     





Related Topics




Popular Posts