Scalar matrix multiplication
C program to perform scalar matrix multiplication.
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.
For example, scalar (s) = 3
For example, scalar (s) = 3
Program
#include<stdio.h>
#include<conio.h>
#define MAX 10
void main()
{
int mat[MAX][MAX];
int row, col;
int i, j, num;
printf("Enter the number of rows\t:");
scanf("%d",&row);
printf("Enter the number of columns\t:");
scanf("%d",&col);
printf("Enter the elements\n");
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
{
printf("Element [%d][%d]\t:",i,j);
scanf("%d",&mat[i][j]);
}
}
printf("Enter the number to multiply with the matrix\t:");
scanf("%d",&num);
printf("\n\nMatrix entered is\n");
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
{
printf("%d\t",mat[i][j]);
}
printf("\n");
}
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
{
mat[i][j] = num*mat[i][j];
}
}
printf("\n\nMatrix after scalar multiplication\n");
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
{
printf("%d\t",mat[i][j]);
}
printf("\n");
}
getch();
}
Output
Enter the number of rows :3
Enter the number of columns :4
Enter the elements
Element [0][0] :1
Element [0][1] :2
Element [0][2] :3
Element [0][3] :4
Element [1][0] :5
Element [1][1] :6
Element [1][2] :7
Element [1][3] :8
Element [2][0] :9
Element [2][1] :10
Element [2][2] :11
Element [2][3] :12
Enter the number to multiply with the matrix :3
Matrix entered is
1 2 3 4
5 6 7 8
9 10 11 12
Matrix after scalar multiplication
3 6 9 12
15 18 21 24
27 30 33 36