Answers for "write a program to add subtract any two matrices in c"

C
0

write a program to add subtract any two matrices in c

#include <stdio.h>
#include <stdlib.h>

void main ()
{
    int i, j, m, n;
    char user_decision;
    int matrix1[10][10],matrix2[10][10],sum[10][10],diff[10][10];

    printf("Enter number of rows : ");
    scanf("%d", &m);
    printf("Enter number of columns : ");
    scanf("%d", &n);

    printf("\n");

    for (i = 0; i < m; i++)
    {
        for (j = 0; j < n; j++)
        {
            printf("Enter element of matrix 1[%d][%d]: ", i, j);
            scanf("%d", &matrix1[i][j]);
        }
    }

    printf("\n");

    for (i = 0; i < m; i++)
    {
        for (j = 0; j < n; j++)
        {
            printf("Enter elements of matrix 2[%d][%d]: ", i, j);
            scanf("%d", &matrix2[i][j]);
        }
    }

    printf("\n");

    printf("type a if you want to add or type s if you want to subtract the matrix: ");
    fflush(stdin);
    scanf("%c",&user_decision);

    printf("\n....Your resultant matrix is....\n\n");

    if(user_decision=='a' || user_decision =='A')
    {
        for (i = 0; i < m; i++)
        {
            for (j = 0; j < n; j++)
            {
                sum[i][j] = matrix1[i][j] + matrix2[i][j];
            }

        }

        for (i = 0; i < m; i++)
        {
            for (j = 0; j < n; j++)
            {
                printf("%d\t", sum[i][j]);
            }
            printf("\n");
        }
    }

    else if(user_decision=='s' || user_decision =='S')
    {
        for (i = 0; i < m; i++)
        {
            for (j = 0; j < n; j++)
            {
                diff[i][j] = matrix1[i][j] - matrix2[i][j];
            }

        }

        for (i = 0; i < m; i++)
        {
            for (j = 0; j < n; j++)
            {
                printf("%d\t", diff[i][j]);
            }
            printf("\n");
        }
    }

    else
        printf("Invalid selection");
}
Posted by: Guest on June-25-2021

Code answers related to "write a program to add subtract any two matrices in c"

Code answers related to "C"

Browse Popular Code Answers by Language