C Program to Find Scalar Multiplication of a Matrix

  • Write a C program to multiply a scalar number and a matrix
  • C program for scalar multiplication of a matrix.

There are two kinds of matrix multiplication, scalar multiplication and matrix multiplication. Matrix multiplication is multiplication of two matrices whereas scalar multiplication is multiplication of a matrix and a single number.


In matrix algebra, a real number is called a scalar. The scalar product of a real number s, and a matrix A is the matrix sA. In scalar multiplication of matrix, we simply multiply each element of the matrix by a scalar number.
Algorithm of scalar multiplication of matrix
Let s be scalar (real numbers) and A be a m x n matrix.
  • To multiply a matrix with a number, we multiply each element of matrix with that number.
  • Traverse every element of matrix using two loops.
  • For every element A[i][j], multiply it with scalar s and stores the result back in A[i][j](A[i][j] = A[i][j] x s)

C program to find scalar multiplication of a matrix.

/*
* C Program to find scalar multiplication of a matrix
*/

#include <stdio.h>
#include <conio.h>

int main(){
    int rows, cols, rowCounter, colCounter, scalar;
    int inputMatrix[50][50];
    printf("Enter Rows and Columns of Matrix\n");
    scanf("%d %d", &rows, &cols);
    
    printf("Enter Matrix of size %dX%d\n", rows, cols);
    /*  Input matrix*/
    for(rowCounter = 0; rowCounter < rows; rowCounter++){
        for(colCounter = 0; colCounter < cols; colCounter++){
            scanf("%d", &inputMatrix[rowCounter][colCounter]);
        }
    }
    /*  Input scalar number*/
    printf("Enter a number to multiply with matrix \n");
    scanf("%d", &scalar);
    /* Multiply each element of matrix with scalar*/
    for(rowCounter = 0; rowCounter < rows; rowCounter++){
        for(colCounter = 0; colCounter < cols; colCounter++){
            inputMatrix[rowCounter][colCounter] = inputMatrix[rowCounter][colCounter]*scalar;
        }
    }
    
    printf("Product Matrix\n");
    for(rowCounter = 0; rowCounter < rows; rowCounter++){
        for(colCounter = 0; colCounter < cols; colCounter++){
            printf("%d ", inputMatrix[rowCounter][colCounter]);
        }
        printf("\n");
    }
    getch();
    return 0;
}

Program Output
Enter Rows and Columns of Matrix
2 2
Enter Matrix of size 2X2
0 1
2 1
Enter a number to multiply with matrix
2
Product Matrix
0 2 
4 2
Enter Rows and Columns of Matrix
2 3
Enter Matrix of size 2X2
1 2 0
2 8 1
Enter a number to multiply with matrix
-3
Product Matrix
-3 -6 0 
-6 -24 -3

Properties of scalar multiplication of matrix
Let s and t be scalar (real numbers) and A and B are m x n matrix.
  • Commutative : sA = As
  • Associative : s(tA) = (st)A
  • Distributive : (s + t)A = sA + tA and t(A + B) = tA + tB
  • Identity : 1 · A = A
  • If A id a mxn matrix then sA is also a mxn matrix

Related Topics
C program to add two matrix
C program to compare two matrix
C program to check identity matrix
C Program for matrix multiplication
C program to print upper triangular matrix
C Program to find transpose of matrix
C Program to print fibonacci series
C program to check if two strings are anagram
List of all C programs