# C program to perform scalar matrix multiplication

Learn how to write a C program to perform scalar matrix multiplication. This article provides a detailed explanation and sample code for multiplying matrix elements by a scalar value using a simple iterative approach.

Scalar matrix multiplication is a fundamental operation in linear algebra where each element of a matrix is multiplied by a scalar value. This operation is commonly used in various fields such as computer graphics, scientific computing, and engineering. In this article, we will guide you through writing a C program to perform scalar matrix multiplication, providing a detailed explanation and sample code.

## What is Scalar Matrix Multiplication?

Scalar matrix multiplication involves taking a matrix and a scalar value, and then multiplying every element of the matrix by that scalar value. For example, if you have a 2x2 matrix:

and a scalar value (k) , the resulting matrix after scalar multiplication would be:

Each element of the original matrix is multiplied by the scalar (k).

## Steps to Perform Scalar Matrix Multiplication

To perform scalar matrix multiplication in C, we can follow these steps:

**Input the Matrix**: Read the elements of the matrix from the user.**Input the Scalar**: Read the scalar value from the user.**Perform the Multiplication**: Multiply each element of the matrix by the scalar value.**Print the Result**: Output the resultant matrix to the console.

## Write a C program to perform scalar matrix multiplication

```
#include <stdio.h>
int main() {
int rows, cols;
float scalar;
// Input the dimensions of the matrix
printf("Enter the number of rows and columns of the matrix -\n");
scanf("%d %d", &rows, &cols);
// Declare the matrix
float matrix[rows][cols];
// Input elements of the matrix
printf("Enter elements of the matrix -\n");
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
printf("Element [%d][%d]: ", i, j);
scanf("%f", &matrix[i][j]);
}
}
// Input the scalar value
printf("Enter the scalar value to multiply: ");
scanf("%f", &scalar);
// Perform scalar multiplication
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
matrix[i][j] *= scalar;
}
}
// Print the resultant matrix
printf("Resultant matrix after scalar multiplication:\n");
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
printf("%.2f ", matrix[i][j]);
}
printf("\n");
}
return 0;
}
```

**Output**

```
Enter the number of rows and columns of the matrix -
3
3
Enter elements of the matrix -
Element [0][0]: 1
Element [0][1]: 2
Element [0][2]: 3
Element [1][0]: 4
Element [1][1]: 5
Element [1][2]: 6
Element [2][0]: 7
Element [2][1]: 8
Element [2][2]: 9
Enter the scalar value to multiply: 3
Resultant matrix after scalar multiplication:
3.00 6.00 9.00
12.00 15.00 18.00
21.00 24.00 27.00
```

### Explanation

**Input the Matrix**: The user inputs the dimensions (number of rows and columns) of the matrix and then enters the elements of the matrix.**Input the Scalar**: The user inputs the scalar value by which the matrix elements will be multiplied.**Perform the Multiplication**:- Nested loops iterate over each element of the matrix.
- Each element of the matrix is multiplied by the scalar value and the result is stored in the same matrix.

**Print the Result**: The program prints the elements of the resultant matrix.

### Detailed Steps

**Step 1: Input the Matrix**- The program prompts the user to enter the number of rows and columns for the matrix.
- The user inputs the elements of the matrix.

**Step 2: Input the Scalar**- The program prompts the user to enter the scalar value for the multiplication.

**Step 3: Perform the Multiplication**- Two nested loops iterate through each element of the matrix.
- Each element is multiplied by the scalar value, and the result is stored back in the matrix.

**Step 4: Print the Result**- The program prints the elements of the resultant matrix row by row.