Thursday, 15 August 2019

C programming for addition and subtraction of two matrices


/*Addition and subtraction of two matrices*/


#include<stdio.h>
void
main()
{
            int i,j,m,n,a[20][20],b[20][20],c[20][20],d[20][20];
            printf("enter the order of matrices:");
            scanf("%d%d",&m,&n);
            printf("enter the elements of matrix A:");
                        for(i=0;i<m;i++)
                        {
                                    for(j=0;j<n;j++)
                                                scanf("%d",&a[i][j]);
                                    printf("\n");
                        }
                        printf("the input matrix A is:\n");
                        for(i=0;i<m;i++)
                        {
                                    for(j=0;j<n;j++)
                                                printf("%d\t",a[i][j]);
                                    printf("\n");
                        }
                        printf("enter the elements of matrix B :");
                        for (i=0;i<m;i++)
                        {
                                    for(j=0;j<n;j++)
                                                scanf("%d",&b[i][j]);
                                    printf("\n");
                        }
                        printf("the input matrix B is:\n");
                        for(i=0;i<m;i++)
                        {
                                    for(j=0;j<n;j++)
                                                printf("%d\t",b[i][j]);
                                    printf("\n");
                        }
                        /*matrix summation*/
                        for(i=0;i<m;i++)
                        {
                                    for(j=0;j<n;j++)
                                                c[i][j]=a[i][j]+b[i][j];
                        }
                        printf("the summation matrix is:\n");
                        for(i=0;i<m;i++)
                        {
                                    for(j=0;j<n;j++)
                                                printf("%d\t",c[i][j]);
                                    printf("\n");
                        }
/*subtraction*/
      for(i=0;i<m;i++)
                        {
                                    for(j=0;j<n;j++)
                                                d[i][j]=a[i][j]-b[i][j];
                        }
                        printf("the subtraction matrix is:\n");
                        for(i=0;i<m;i++)
                        {
                                    for(j=0;j<n;j++)
                                                printf("%d\t",d[i][j]);
                                    printf("\n");
                        }
}

No comments:

Post a Comment

Thanks for comment.