C PROGRAM TO PERFORM ADDITION OF TWO MATRICES
C Program to Perform Matrix Addition
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
#include<stdio.h> #include<conio.h> void main() { //using 2D-ARRAYS int RowSize,ColSize; int Matrix1[5][5]; int Matrix2[5][5]; int ResultMatrix[5][5]; int i, j; printf("\n >>> PROGRAM To PRINT ADDITION OF TWO MATRICES <<<\n"); printf("\n Enter the Size of a Matrix(For Example:3 3) : "); scanf("%d %d",&RowSize,&ColSize); if (RowSize > 5 || ColSize > 5) //limiting the size of matrix { printf(" The Size Of Matrix should Be in Less Than 5 (limiting size of array)"); printf("\n\n\t Press Enter key to exit...."); getch(); return; } else { //Initializing all the elements to zero for (i = 0; i < RowSize; i++) { for (j = 0; j < ColSize; j++) { Matrix1[i][j] = 0; Matrix2[i][j] = 0; } } //Reading elements of Matrix1 printf("\n Enter the elements of Matrix1(%d * %d) \n", RowSize, ColSize); for (i = 0; i < RowSize; i++) { for (j = 0; j < ColSize; j++) { printf(" Matrix1[%d][%d] : ", i, j); scanf("%d",&Matrix1[i][j]); } } //Reading elements of Matrix2 printf("\n Enter the elements of Matrix2(%d * %d) \n", RowSize, ColSize); for (i = 0; i < RowSize; i++) { for (j = 0; j < ColSize; j++) { printf(" Matrix2[%d][%d] : ", i, j); scanf("%d",&Matrix2[i][j]); } } //calculating ResultMatrix, by Adding Matrix1 And Matrix2 for (i = 0; i < RowSize; i++) { for (j = 0; j < ColSize; j++) { ResultMatrix[i][j] = Matrix1[i][j] + Matrix2[i][j]; } } //Printing Result Matrix printf("\n\n\t*** Result Matrix ***\n\n\t"); for (i = 0; i < RowSize; i++) { for (j = 0; j < ColSize; j++) { if (ResultMatrix[i][j] < 10) { //prinnting number as 01,02,etc,. printf(" 0%d",ResultMatrix[i][j] ); } else { printf("%d",ResultMatrix[i][j]); } if (j == ColSize - 1) { printf("\n\t"); } } } } printf("\n\n\t Press Enter key to exit...."); getch(); } |
Output 1:
output 2 :
This comment has been removed by a blog administrator.