Example:
If matrix A=
1 2 3
4 5 6
7 8 9
Output: 2 . A =
2 4 6
8 10 12
14 16 18
Required knowledge:
Basic C programming, For loop, Array, MatrixScalar multiplication of matrix
Scalar multiplication of matrix is the simplest and easiest way to multiply matrix. Scalar multiplication of matrix is defined by.(cA)ij = c . Aij (Where 1 ≤ i ≤ m and 1 ≤ j ≤ n)
Program:
/**
* C program to perform scalar multiplication of matrix
*/
#include <stdio.h>
int main()
{
int A[3][3];
int num, row, col;
/*
* Reads elements in matrix from user
*/
printf("Enter elements in matrix of size 3x3: \n");
for(row=0; row<3; row++)
{
for(col=0; col<3; col++)
{
scanf("%d", &A[row][col]);
}
}
/* Reads number to perform scalar multiplication from user */
printf("Enter any number to multiply with matrix A: ");
scanf("%d", &num);
/*
* Performs scalar multiplication of matrix
*/
for(row=0; row<3; row++)
{
for(col=0; col<3; col++)
{
/* (cAij) = c . Aij */
A[row][col] = num * A[row][col];
}
}
/*
* Prints the result of scalar multiplication of matrix
*/
printf("\nScalar multiplication of matrix A = \n");
for(row=0; row<3; row++)
{
for(col=0; col<3; col++)
{
printf("%d ", A[row][col]);
}
printf("\n");
}
return 0;
}
Output
Enter elements in matrix of size 3x3:
1 2 3
4 5 6
7 8 9
Enter any number to multiply with matrix A: 2
Scalar multiplication of matrix A =
2 4 6
8 10 12
14 16 18
1 2 3
4 5 6
7 8 9
Enter any number to multiply with matrix A: 2
Scalar multiplication of matrix A =
2 4 6
8 10 12
14 16 18
Happy coding ;)
You may also like
- Array and Matrix programming exercises index.
- C program to multiply two matrices.
- C program to add two matrices.
- C program to subtract two matrices.
- C program to check whether two matrices are equal or not.
- C program to find lower triangular matrix.
- C program to find upper triangular matrix.
- C program to find sum of all elements of an array.
- C program to find maximum and minimum elements in an array.
- C program to search an element in an array.
- C program to sort elements of an array in ascending order.
- C program to sort elements of an array in descending order.
- C program to check even or odd number using switch case.
- C program to create Simple Calculator using switch case.