C Program to Subtract Two Matrices

Introduction

Subtracting two matrices involves subtracting the corresponding elements of the two matrices to produce a new matrix. This guide will show you how to write a C program that subtracts one matrix from another.

Problem Statement

Create a C program that:

  • Takes two matrices as input from the user.
  • Subtracts the corresponding elements of the second matrix from the first matrix.
  • Displays the resulting matrix.

Example:

  • Input:
    • Matrix 1:
      5 6 7
      8 9 10
      11 12 13
      
    • Matrix 2:
      1 2 3
      4 5 6
      7 8 9
      
  • Output: Resultant Matrix:
    4 4 4
    4 4 4
    4 4 4
    

Solution Steps

  1. Include the Standard Input-Output Library: Use #include <stdio.h> for standard input-output functions.
  2. Write the Main Function: Define the main function, which is the entry point of every C program.
  3. Declare Variables: Declare variables to store the two input matrices, the resultant matrix, and loop counters.
  4. Input the Dimensions and Elements of the Matrices: Use loops to take input from the user for both matrices.
  5. Subtract the Corresponding Elements of the Matrices: Use nested loops to iterate through each element of the matrices and subtract them.
  6. Display the Resulting Matrix: Use loops to display the resulting matrix.

C Program to Subtract Two Matrices

#include <stdio.h>

int main() {
    // Step 1: Declare variables to hold the matrices and dimensions
    int rows, cols;
    int matrix1[100][100], matrix2[100][100], result[100][100];
    int i, j;

    // Step 2: Prompt the user to enter the dimensions of the matrices
    printf("Enter the number of rows and columns of the matrices: ");
    scanf("%d %d", &rows, &cols);

    // Step 3: Input the elements of the first matrix
    printf("Enter elements of the first matrix:\n");
    for (i = 0; i < rows; i++) {
        for (j = 0; j < cols; j++) {
            scanf("%d", &matrix1[i][j]);
        }
    }

    // Step 4: Input the elements of the second matrix
    printf("Enter elements of the second matrix:\n");
    for (i = 0; i < rows; i++) {
        for (j = 0; j < cols; j++) {
            scanf("%d", &matrix2[i][j]);
        }
    }

    // Step 5: Subtract the corresponding elements of the matrices
    for (i = 0; i < rows; i++) {
        for (j = 0; j < cols; j++) {
            result[i][j] = matrix1[i][j] - matrix2[i][j];
        }
    }

    // Step 6: Display the resulting matrix
    printf("Resultant matrix after subtraction:\n");
    for (i = 0; i < rows; i++) {
        for (j = 0; j < cols; j++) {
            printf("%d ", result[i][j]);
        }
        printf("\n");
    }

    return 0;  // Step 7: Return 0 to indicate successful execution
}

Explanation

Step 1: Declare Variables

  • The variables rows and cols store the dimensions of the matrices. matrix1, matrix2, and result are 2D arrays to store the input matrices and the resulting matrix, respectively. The variables i and j are used as loop counters.

Step 2: Input the Dimensions of the Matrices

  • The program prompts the user to enter the number of rows and columns for the matrices using scanf.

Step 3: Input the Elements of the First Matrix

  • The program uses nested for loops to take input for each element of the first matrix from the user.

Step 4: Input the Elements of the Second Matrix

  • Similarly, the program uses nested for loops to take input for each element of the second matrix.

Step 5: Subtract the Corresponding Elements of the Matrices

  • The program uses nested for loops to iterate through each element of the matrices:
    • The corresponding elements from matrix2 are subtracted from matrix1 and stored in the result matrix.

Step 6: Display the Resulting Matrix

  • The program uses nested for loops to display the elements of the resulting matrix after subtraction.

Step 7: Return 0

  • The return 0; statement indicates that the program executed successfully.

Output Example

Example:

Enter the number of rows and columns of the matrices: 3 3
Enter elements of the first matrix:
5 6 7
8 9 10
11 12 13
Enter elements of the second matrix:
1 2 3
4 5 6
7 8 9
Resultant matrix after subtraction:
4 4 4 
4 4 4 
4 4 4

Another Example:

Enter the number of rows and columns of the matrices: 2 2
Enter elements of the first matrix:
10 20
30 40
Enter elements of the second matrix:
1 2
3 4
Resultant matrix after subtraction:
9 18
27 36

Conclusion

This C program demonstrates how to subtract one matrix from another by iterating through each element and subtracting the corresponding elements from the two matrices. It covers basic concepts such as arrays, loops, and matrix operations, making it a useful example for beginners learning C programming.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top