C Program

Write a C program to find sum of two matrices

#include <stdio.h>

void main() {
float a[2][2], b[2][2], c[2][2]; // Declare three 2x2 matrices
int i, j;

printf("Enter the elements of the 1st matrix:\n");
// Reading the first matrix
for(i = 0; i < 2; i++)
for(j = 0; j < 2; j++) {
scanf("%f", &a[i][j]);
}

printf("Enter the elements of the 2nd matrix:\n");
// Reading the second matrix
for(i = 0; i < 2; i++)
for(j = 0; j < 2; j++) {
scanf("%f", &b[i][j]);
}

// Calculating the sum of the two matrices
for(i = 0; i < 2; i++)
for(j = 0; j < 2; j++) {
c[i][j] = a[i][j] + b[i][j]; // Sum of corresponding elements
}

// Displaying the resulting sum matrix
printf("\nSum of the matrices:\n");
for(i = 0; i < 2; i++) {
for(j = 0; j < 2; j++) {
printf("%f ", c[i][j]); // Print each element
}
printf("\n"); // New line after each row
}
}

Explanation of the Code

  1. Header File Inclusion

    #include <stdio.h>
    

    This line includes the standard input-output library, which is necessary for using functions like printf and scanf.

  2. Main Function

    void main() {
    

    The main function is where the program execution starts. It is defined with a return type of void here, but it’s generally recommended to use int main() in C for standard compliance.

  3. Matrix Declaration

    float a[2][2], b[2][2], c[2][2];
    

    Three 2×2 matrices are declared: a for the first matrix, b for the second, and c for the resulting sum matrix.

  4. Input for First Matrix

    printf("Enter the elements of the 1st matrix:\n");
    for(i = 0; i < 2; i++)
        for(j = 0; j < 2; j++) {
            scanf("%f", &a[i][j]);
        }
    

    The user is prompted to enter the elements of the first matrix. A nested loop is used to read each element

  5. Input for Second Matrix

    printf("Enter the elements of the 2nd matrix:\n");
    for(i = 0; i < 2; i++)
        for(j = 0; j < 2; j++) {
            scanf("%f", &b[i][j]);
        }
    

    Similarly, the user is prompted to enter the elements of the second matrix, again using a nested loop.

  6. Matrix Addition

    for(i = 0; i < 2; i++)
        for(j = 0; j < 2; j++) {
            c[i][j] = a[i][j] + b[i][j]; // Sum of corresponding elements
        }
    

    This nested loop iterates through each element of the matrices a and b, adding the corresponding elements together and storing the result in the matrix c.

  7. Display Resulting Matrix

    printf("\nSum of the matrices:\n");
    for(i = 0; i < 2; i++) {
        for(j = 0; j < 2; j++) {
            printf("%f ", c[i][j]); // Print each element
        }
        printf("\n"); // New line after each row
    }
    

    This section prints the resulting sum matrix c. Each element is printed in a formatted manner, with a new line after each row for clarity.

Output

When you run the program, it will prompt you to enter the elements of the two matrices and then display their sum.

Example Input:

Enter the elements of the 1st matrix:
1 2
3 4
Enter the elements of the 2nd matrix:
5 6
7 8

Example Output:

Sum of the matrices:
6.000000 8.000000 
10.000000 12.000000 

Team Educate

About Author

Leave a comment

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

You may also like

C program to test whether a given identifier is valid or not
C Program

C Program to Test Whether a Given Identifier is Valid or Not

Rules for Valid Identifiers in C: The identifier must begin with a letter or an underscore (_). It cannot start
C program to test whether a given identifier is valid or not
C Program Compiler Design

C program to implement simple code generator

ALGORITHM:1. Start2. Get address code sequence.3. Determine current location of 3 using address (for 1st operand).4. If current location not