The C program for matrix manipulation performs basic matrix operations upon receiving the values for two matrices from the user. The program does addition, subtraction, multiplication, and transpose of a matrix.

To learn about matrices, visit our Linear algebra hub page.

Problem Definition

Suppose you are given two matrices, then you can perform following simple operations on them.

  1. Addition
  2. Subtraction
  3. Multiplication
  4. Transpose

The program uses only square matrices for all its operations. For example, if \Large A and \Large B are two matrices then,

\Large A = \hspace{1cm} \begin{bmatrix} 1 & 2 & 3\\ 1 & 1 & 4\\ 2 & 3 & 5 \end{bmatrix} \\~\\
\Large B = \hspace{1cm} \begin{bmatrix} 3 & 1 & 3\\ 2 & 1 & 1\\ 3 & 6 & 3 \end{bmatrix} \\~\\
\Large A^T = \hspace{.8cm} \begin{bmatrix} 1 & 1 & 2\\ 2 & 1 & 3\\ 3 & 4 & 5 \end{bmatrix} // Transpose \hspace{4px}of \hspace{4px}A \hspace{4px}Matrix\\~\\
\Large A + B = \hspace{0.3cm} \begin{bmatrix} 4 & 3 & 6\\ 3 & 2 & 5\\ 5 & 9 & 8 \end{bmatrix} // Addition \hspace{4px}of \hspace{4px}A \hspace{4px}and \hspace{4px}B \hspace{4px}matrix\\~\\
\Large A - B = \hspace{0.3cm} \begin{bmatrix} -2 & 1 & 0\\ -1 & 0 & 3\\ -1 & -3 & 2 \end{bmatrix} //\hspace{4px}Subtraction \hspace{4px}of \hspace{4px}A \hspace{4px}and \hspace{4px}B \hspace{4px}matrix\\~\\
\Large A * B = \hspace{0.3cm} \begin{bmatrix} -2 & 1 & 0\\ -1 & 0 & 3\\ -1 & -3 & 2 \end{bmatrix} // \hspace{4px}Multiplication \hspace{4px}of \hspace{4px}A \hspace{4px}and \hspace{4px}B \hspace{4px}matrix

More about the operations ..

  1. In multiplication of two matrices \Large A and \Large B, the row in \Large Amultiply all elements in column in \Large Band the result is added to get each element of the output matrix. You do this for all rows and columns.
  2. In transpose of matrix, the row becomes the column and the column becomes the row, in the resultant matrix.
  3. The addition and subtraction of matrices are straight-forward operations.

Flowchart Matrix Manipulation Program

Flowchart - C Program for Matrix Manipulation
Flowchart – C Program for Matrix Manipulation

Program Codes – Matrix Manipulation Program

 /* PROGRAM IN C TO MANIPULATE MATRIX */
#include <stdio.h>
#include <conio.h>
#define N 10
main() 
{
    int a[10][10],b[10][10];
    int c[10][10];
    int i,j,k,n,choice;
    void read_matrix(int a[N][N],int b[N][N],int n);
    void menu(int a[N][N],int b[N][N],int c[N][N],int n);
    /* Get the Size of the Matrices */
    printf("ENTER SIZE OF MATRIX:");
    scanf("%d",&n);
    /* Read Matrices values */
    read_matrix(a,b,n);
    i=0;
    while(choice !=5) 
    {
        menu(a,b,c,n);
        i++;
    }
    system("PAUSE");
    return 0;
}

void menu(int a[N][N],int b[N][N],int c[N][N],int n) 
{
    int i,j,k,choice;
    printf("ENTER A CHOICE:\n");
    for(i=0;i<30;i++)
    printf("_");printf("\n\n");
        printf("1.ADDITION\n");
        printf("2.SUBTRACTION\n");
        printf("3.MULTIPLICATION\n");
        printf("4.TRANSPOSE\n");
        printf("5.QUIT\n")
    for(i=0;i<30;i++)
    printf("_");printf("\n\n");
    scanf("%d",&choice);
    i=0;
    switch(choice) 
    {
        case 1:
            for(i=0;i<n;i++) {
                for(j=0;j<n;j++) {
                    c[i][j] = a[i][j] + b[i][j];
                    }
            }
            /* Print Result */
            system("cls");
            printf("RESULT\n");
            for(i=0;i<n;i++) {
                for(j=0;j<n;j++) {
                    printf("%d\t",c[i][j]);
                }
            printf("n");
            }
            for(i=0;i<30;i++)
            printf("*");
            printf("\n\n");
            break;
        case 2:
            for(i=0;i<n;i++) {
                for(j=0;j<n;j++) {
                    c[i][j] = a[i][j] - b[i][j];
                }
            }
            /* Print Result */
            system("cls");
            printf("RESULT\n");
            for(i=0;i<n;i++) {
                for(j=0;j<n;j++) {
                    printf("%d\t",c[i][j]);
                }
            printf("\n");
            }
            for(i=0;i<30;i++)
            printf("_");printf("\n\n");
            break;
        case 3:
            for(i=0;i<n;i++) {
                for( j=0;j<n;j++) {
                    for( k=0;k<n;k++) {
                    c[i][j] = c[i][j] + a[i][k] * b[k][j];
                    }                 
                }
            }
            /* Print the result */
            system("cls");
            printf("Result\n");
            for( i=0;i<n;i++) {
                for(j=0;j<n;j++) {
                    printf("%d\t",c[i][j]);
                }
            printf("\n");
            }
            for(i=0;i<30;i++)
            printf("_");printf("\n\n");
            break;
            /* Transpose Matrix -A */
        case 4:
            for(i=0;i<n;i++) {
                for(j=0;j<n;j++) {
                    c[i][j] = a[j][i];
                }
            }
            /* Print Result */
            system("cls");
            printf("*RESULT*\n");
            for(i=0;i<n;i++) {
                for(j=0;j<n;j++) {
                    printf("%d\t",c[i][j]);
                }
            printf("\n");
            }
            for(i=0;i<30;i++)
            printf("_");printf("\n\n");
            break;
        default: exit(0);
        }
    }
/* Read Matrix a and b*/
void read_matrix(int a[N][N],int b[N][N],int n) {
    int i,j;
    printf("READ MATRIX A:\n\n");
    printf("ENTER MATRIX VALUES ROW WISE:\n\n");
    for(i=0;i<n;i++) {
        for(j=0;j<n;j++) {
            scanf("%d",&a[i][j]);
        }
    }

    /* Print Matrix A */
    printf("*Matrix-A*\n");
    for(i=0;i<n;i++) {
        for(j=0;j<n;j++) {
            printf("%d\t",a[i][j]);
        }
    printf("\n");
    }
    for(i=0;i<30;i++)
    printf("*");printf("\n\n");
    /* READ MATRIX B */
    printf("READ MATRIX B:\n\n");
    printf("ENTER MATRIX VALUES ROW WISE:\n\n");
    for(i=0;i<n;i++) {
        for(j=0;j<n;j++) {
            scanf("%d",&b[i][j]);
        }
    }

    /*Print Matrix-B */
    printf("*Matrix-B*\n");
    for(i=0;i<n;i++) {
        for(j=0;j<n;j++) {
            printf("%d\t",b[i][j]);
            }
    printf("\n");
    }
    for(i=0;i<30;i++)
    printf("*");printf("\n\n");
}
#include <iostream>
using namespace std;

#define N 10

void readMatrix(int a[N][N], int b[N][N], int n) {
    cout << "READ MATRIX A (row-wise):\n";
    for (int i = 0; i < n; i++)
        for (int j = 0; j < n; j++)
            cin >> a[i][j];

    cout << "\nREAD MATRIX B (row-wise):\n";
    for (int i = 0; i < n; i++)
        for (int j = 0; j < n; j++)
            cin >> b[i][j];
}

void menu(int a[N][N], int b[N][N], int c[N][N], int n) {
    int choice;
    cout << "\n1. ADDITION\n2. SUBTRACTION\n3. MULTIPLICATION\n4. TRANSPOSE\n5. QUIT\n";
    cout << "Enter choice: ";
    cin >> choice;

    switch (choice) {
        case 1:
            for (int i = 0; i < n; i++)
                for (int j = 0; j < n; j++)
                    c[i][j] = a[i][j] + b[i][j];
            break;

        case 2:
            for (int i = 0; i < n; i++)
                for (int j = 0; j < n; j++)
                    c[i][j] = a[i][j] - b[i][j];
            break;

        case 3:
            for (int i = 0; i < n; i++)
                for (int j = 0; j < n; j++) {
                    c[i][j] = 0;
                    for (int k = 0; k < n; k++)
                        c[i][j] += a[i][k] * b[k][j];
                }
            break;

        case 4:
            for (int i = 0; i < n; i++)
                for (int j = 0; j < n; j++)
                    c[i][j] = a[j][i];
            break;

        case 5:
            exit(0);
    }

    cout << "\nRESULT:\n";
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++)
            cout << c[i][j] << "\t";
        cout << endl;
    }
}

int main() {
    int a[N][N], b[N][N], c[N][N], n;

    cout << "ENTER SIZE OF MATRIX: ";
    cin >> n;

    readMatrix(a, b, n);

    while (true)
        menu(a, b, c, n);

    return 0;
}
import java.util.Scanner;

class MatrixOperations {
    static final int N = 10;

    static void readMatrix(int[][] a, int[][] b, int n, Scanner sc) {
        System.out.println("READ MATRIX A (row-wise):");
        for (int i = 0; i < n; i++)
            for (int j = 0; j < n; j++)
                a[i][j] = sc.nextInt();

        System.out.println("READ MATRIX B (row-wise):");
        for (int i = 0; i < n; i++)
            for (int j = 0; j < n; j++)
                b[i][j] = sc.nextInt();
    }

    static void menu(int[][] a, int[][] b, int[][] c, int n, Scanner sc) {
        System.out.println("\n1. ADDITION\n2. SUBTRACTION\n3. MULTIPLICATION\n4. TRANSPOSE\n5. QUIT");
        System.out.print("Enter choice: ");
        int choice = sc.nextInt();

        switch (choice) {
            case 1:
                for (int i = 0; i < n; i++)
                    for (int j = 0; j < n; j++)
                        c[i][j] = a[i][j] + b[i][j];
                break;

            case 2:
                for (int i = 0; i < n; i++)
                    for (int j = 0; j < n; j++)
                        c[i][j] = a[i][j] - b[i][j];
                break;

            case 3:
                for (int i = 0; i < n; i++)
                    for (int j = 0; j < n; j++) {
                        c[i][j] = 0;
                        for (int k = 0; k < n; k++)
                            c[i][j] += a[i][k] * b[k][j];
                    }
                break;

            case 4:
                for (int i = 0; i < n; i++)
                    for (int j = 0; j < n; j++)
                        c[i][j] = a[j][i];
                break;

            case 5:
                System.exit(0);
        }

        System.out.println("\nRESULT:");
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++)
                System.out.print(c[i][j] + "\t");
            System.out.println();
        }
    }

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int[][] a = new int[N][N];
        int[][] b = new int[N][N];
        int[][] c = new int[N][N];

        System.out.print("ENTER SIZE OF MATRIX: ");
        int n = sc.nextInt();

        readMatrix(a, b, n, sc);

        while (true)
            menu(a, b, c, n, sc);
    }
}
def read_matrix(n):
    print("READ MATRIX A (row-wise):")
    a = [[int(input()) for _ in range(n)] for _ in range(n)]

    print("READ MATRIX B (row-wise):")
    b = [[int(input()) for _ in range(n)] for _ in range(n)]

    return a, b


def menu(a, b, n):
    print("\n1. ADDITION\n2. SUBTRACTION\n3. MULTIPLICATION\n4. TRANSPOSE\n5. QUIT")
    choice = int(input("Enter choice: "))

    c = [[0]*n for _ in range(n)]

    if choice == 1:
        for i in range(n):
            for j in range(n):
                c[i][j] = a[i][j] + b[i][j]

    elif choice == 2:
        for i in range(n):
            for j in range(n):
                c[i][j] = a[i][j] - b[i][j]

    elif choice == 3:
        for i in range(n):
            for j in range(n):
                for k in range(n):
                    c[i][j] += a[i][k] * b[k][j]

    elif choice == 4:
        for i in range(n):
            for j in range(n):
                c[i][j] = a[j][i]

    elif choice == 5:
        exit()

    print("\nRESULT:")
    for row in c:
        print(*row)


n = int(input("ENTER SIZE OF MATRIX: "))
a, b = read_matrix(n)

while True:
    menu(a, b, n)

Output

ENTER SIZE OF MATRIX:2
READ MATRIX A:

ENTER MATRIX VALUES ROWWISE:
1 2
4 2
*MATRIX-A*
1    2
4    2
********************************

READ MATRIX B:

ENTER MATRIX VALUES ROWWISE:
1 4
1 1
*MATRIX-B*
1    4
1    1
*****************************
ENTER A CHOICE:
___________________________
1.ADDITION
2.SUBTRACTION
3.MULTIPLICATION
4.TRANSPOSE
5.QUIT