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");
}

Are there any code examples left?
Made with love
This website uses cookies to make IQCode work for you. By using this site, you agree to our cookie policy

Welcome Back!

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign in
Recover lost password
Or log in with

Create a Free Account

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign up
Or sign up with
By signing up, you agree to the Terms and Conditions and Privacy Policy. You also agree to receive product-related marketing emails from IQCode, which you can unsubscribe from at any time.
Creating a new code example
Code snippet title
Source