Array Exercises
Write a C program to read elements in a matrix and perform scalar multiplication of matrix. C program for scalar multiplication of matrix. How to perform scalar matrix multiplication in C programming. Logic to perform scalar matrix multiplication in C program.
Required knowledge
Basic C programming, C for loop, ArrayS
Scalar 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 to perform scalar matrix multiplication
/** * C program to perform scalar matrix multiplication */ #include <stdio.h> #define SIZE 3 // Maximum size of the array int main() { int A[SIZE][SIZE]; int num, row, col; /* Input elements in matrix from user */ printf("Enter elements in matrix of size %dx%d: \n", SIZE, SIZE); for(row=0; row<SIZE; row++) { for(col=0; col<SIZE; col++) { scanf("%d", &A[row][col]); } } /* Input multiplier from user */ printf("Enter any number to multiply with matrix A: "); scanf("%d", &num); /* Perform scalar multiplication of matrix */ for(row=0; row<SIZE; row++) { for(col=0; col<SIZE; col++) { /* (cAij) = c . Aij */ A[row][col] = num * A[row][col]; } } /* Print result of scalar multiplication of matrix */ printf("\nResultant matrix c.A = \n"); for(row=0; row<SIZE; row++) { for(col=0; col<SIZE; col++) { printf("%d ", A[row][col]); } printf("\n"); } return 0; }
Output
Enter elements in matrix of size 3x3:
10 20 30
40 50 60
70 80 90
Enter any number to multiply with matrix A: 3
Resultant matrix c.A =
30 60 90
120 150 180
210 240 270
Process returned 0 (0x0) execution time : 44.041 s
/** * C program to perform scalar matrix multiplication */ #include <stdio.h> int main() { int A[10][10]; int num, row, col,m,n; printf("Enter no of rows:"); scanf("%d",&m); printf("Enter no of columns:"); scanf("%d",&n); /* Input elements in matrix from user */ printf("Enter elements in matrix of size %d %d: \n", m, n); for(row=0; row<m; row++) { for(col=0; col<n; col++) { scanf("%d", &A[row][col]); } } /* Input multiplier from user */ printf("Enter any number to multiply with matrix A: "); scanf("%d", &num); /* Perform scalar multiplication of matrix */ for(row=0; row<m; row++) { for(col=0; col<n; col++) { /* (cAij) = c . Aij */ A[row][col] = num * A[row][col]; } } /* Print result of scalar multiplication of matrix */ printf("\nResultant matrix c.A = \n"); for(row=0; row<m; row++) { for(col=0; col<n; col++) { printf("%d ", A[row][col]); } printf("\n"); } return 0; }