C Program to Calculate Addition of Two Matrices (दो Matrices का जोड़ ज्ञात करने का C प्रोग्राम)
Matrix addition is the process of adding the corresponding elements of two matrices having the same number of rows and columns.
Matrix Addition वह प्रक्रिया है जिसमें समान Rows और Columns वाली दो Matrices के संगत तत्वों को जोड़ा जाता है। इस प्रोग्राम में हम दो 2×2 Matrices का जोड़ ज्ञात करेंगे।
C Program
#include <stdio.h>#define R 2#define C 2int main(){int a[R][C], b[R][C], add[R][C];int i, j;printf("Enter elements of Matrix A:\n");for (i = 0; i < R; i++){for (j = 0; j < C; j++){printf("Enter A[%d][%d] = ", i, j);scanf("%d", &a[i][j]);}}printf("\nEnter elements of Matrix B:\n");for (i = 0; i < R; i++){for (j = 0; j < C; j++){printf("Enter B[%d][%d] = ", i, j);scanf("%d", &b[i][j]);}}/* Addition of two matrices */for (i = 0; i < R; i++){for (j = 0; j < C; j++){add[i][j] = a[i][j] + b[i][j];}}printf("\nAddition of Matrix A and B is:\n");for (i = 0; i < R; i++){for (j = 0; j < C; j++){printf("%d\t", add[i][j]);}printf("\n");}return 0;}
Output (आउटपुट)
Enter elements of Matrix A:Enter A[0][0] = 1Enter A[0][1] = 2Enter A[1][0] = 3Enter A[1][1] = 4Enter elements of Matrix B:Enter B[0][0] = 5Enter B[0][1] = 6Enter B[1][0] = 7Enter B[1][1] = 8Addition of Matrix A and B is:6 810 12
Explanation (व्याख्या)
The program uses two nested for loops to read and add the corresponding elements of both matrices.
a[R][C]stores Matrix A.b[R][C]stores Matrix B.add[R][C]stores the result of addition.add[i][j] = a[i][j] + b[i][j];adds corresponding elements.
प्रोग्राम में दो Nested for लूप का उपयोग किया गया है। पहला लूप Rows के लिए और दूसरा Columns के लिए चलता है। प्रत्येक स्थान पर Matrix A और Matrix B के तत्वों को जोड़कर परिणाम Matrix में संग्रहीत किया जाता है। यह प्रोग्राम C भाषा में 2D Array की सहायता से दो Matrices का जोड़ ज्ञात करना सिखाता है।
Comments
Post a Comment