C Program to Calculate Subtraction of Two Matrices (दो Matrices का घटाव ज्ञात करने का C प्रोग्राम)
Matrix subtraction is the process of subtracting the corresponding elements of one matrix from another matrix having the same number of rows and columns.
Matrix Subtraction वह प्रक्रिया है जिसमें समान 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], sub[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]);}}/* Subtraction of two matrices */for (i = 0; i < R; i++){for (j = 0; j < C; j++){sub[i][j] = a[i][j] - b[i][j];}}printf("\nSubtraction of Matrix A and B is:\n");for (i = 0; i < R; i++){for (j = 0; j < C; j++){printf("%d\t", sub[i][j]);}printf("\n");}return 0;}
Output (आउटपुट)
Enter elements of Matrix A:Enter A[0][0] = 10Enter A[0][1] = 20Enter A[1][0] = 30Enter A[1][1] = 40Enter elements of Matrix B:Enter B[0][0] = 1Enter B[0][1] = 2Enter B[1][0] = 3Enter B[1][1] = 4Subtraction of Matrix A and B is:9 1827 36
Explanation (व्याख्या)
The program uses two nested for loops to subtract the corresponding elements of Matrix B from Matrix A.
a[R][C]stores Matrix A.b[R][C]stores Matrix B.sub[R][C]stores the subtraction result.sub[i][j] = a[i][j] - b[i][j];subtracts corresponding elements.
इस प्रोग्राम में दो Nested for लूप का उपयोग किया गया है। प्रत्येक स्थान पर Matrix A के तत्व में से Matrix B का संगत तत्व घटाया जाता है और परिणाम को sub Matrix में संग्रहीत किया जाता है। यह प्रोग्राम C भाषा में 2D Array की सहायता से दो Matrices का घटाव करना सिखाता है।
Comments
Post a Comment