Add and subtract the values of two different matrices using arrays in C

 

Hey guys once again welcome to the Decoders blog, I hope you all are doing well and today I'm present with another logic but this time with something different it is a C logic which will help us to get the addition and subtraction of given two matrices using arrays in C 

Here's the code which you can go through...........

#include<stdio.h>
int main()
{
    int a[3][3],b[3][3],c[3][3],d[3][3];    //Declaring a 3*3 matrix A,B,C and D
    int n,i,j;
    //Enter the nine entries for the matrix [A]
    printf("Enter the values for matrix A:");    
    //Storing the values for the matrix [A]
    for(i=0;i<3;i++)
    {
        for(j=0;j<3;j++)
        {
        scanf("%d",&a[i][j]);
        }
    }
    //Enter the nine entries for the matrix [B]
    printf("Enter the value for the matrix b:");
    //Storing the values for the matrix [B]
    for(i=0;i<3;i++)
    {
        for(j=0;j<3;j++)
        {
            scanf("%d",&b[i][j]);
        }
    }
    //Displaying the values of the matrix [A]
    printf("The matrix A is \n");
    for (i = 0; i < 3; i++)
    {
        for ( j = 0; j < 3; j++)
        {
            printf("%d\t",a[i][j]);
        }
        printf("\n");
       
    }
    printf("\n");
    //Displaying the values for the matrix [B]
    printf("The matrix B is\n");
    for(i=0;i<3;i++)
    {
        for(j=0;j<3;j++)
        {
            printf("%d\t",b[i][j]);
        }
        printf("\n");
    }
    //Storing the addition value in matrix [C]
    for(i=0;i<3;i++)
    {
        for(j=0;j<3;j++)
        {
            c[i][j]=a[i][j]+b[i][j];
        }

    }
    //Storing the subtracted values in the matrix [D]
    for(i=0;i<3;i++)
    {
        for(j=0;j<3;j++)
        {
             d[i][j]=a[i][j]-b[i][j];
        }
    }

    //Displaying the matrix [C]
    printf("The mattri C is\n");
    for(i=0;i<3;i++)
    {
        for(j=0;j<3;j++)
        {
            printf("%d\t",c[i][j]);
        }
        printf("\n");
    }
    printf("\n");
    //Displaying the matrix [D]
    printf("The matrix D is\n");
    for(i=0;i<3;i++)
    {
        for(j=0;j<3;j++)
        {
            printf("%d\t",d[i][j]);
        }
        printf("\n");
    }
    return(0);
}

Comments

Popular Posts