# Program for scalar multiplication of a matrix

Given a matrix and a scalar element k, our task is to find out the scalar product of that matrix.

Examples:

```Input : mat[][] = {{2, 3}
{5, 4}}
k = 5
Output : 10 15
25 20
We multiply 5 with every element.

Input : 1 2 3
4 5 6
7 8 9
k = 4
Output :  4 8  12
16 20 24
28 32 36
```

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

The scalar multiplication of a number k(scalar), multiply it on every entry in the matrix. and a matrix A is the matrix kA.

## C++

 `// C++ program to find the scalar product  ` `// of a matrix ` `#include ` `using` `namespace` `std; ` ` `  `// Size of given matrix ` `#define N 3 ` ` `  `void` `scalarProductMat(``int` `mat[][N], ``int` `k) ` `{ ` `    ``// scalar element is multiplied by the matrix ` `    ``for` `(``int` `i = 0; i < N; i++)  ` `        ``for` `(``int` `j = 0; j < N; j++)  ` `            ``mat[i][j] = mat[i][j] * k;         ` `} ` ` `  `// Driver code ` `int` `main() ` `{ ` `    ``int` `mat[N][N] = { { 1, 2, 3 }, ` `                      ``{ 4, 5, 6 }, ` `                      ``{ 7, 8, 9 } }; ` `    ``int` `k = 4; ` ` `  `    ``scalarProductMat(mat, k); ` ` `  `    ``// to display the resultant matrix ` `    ``printf``(``"Scalar Product Matrix is : \n"``); ` `    ``for` `(``int` `i = 0; i < N; i++) { ` `        ``for` `(``int` `j = 0; j < N; j++)  ` `            ``printf``(``"%d "``, mat[i][j]); ` `        ``printf``(``"\n"``); ` `    ``} ` ` `  `    ``return` `0; ` `} `

## Java

 `// Java program to find ` `// the scalar product  ` `// of a matrix ` `import` `java.io.*; ` ` `  `class` `GFG { ` `     `  `static` `final` `int` `N = ``3``; ` `static` `void` `scalarProductMat(``int` `mat[][],  ` `                                  ``int` `k) ` `{ ` `     `  `    ``// scalar element is multiplied ` `    ``// by the matrix ` `    ``for` `(``int` `i = ``0``; i < N; i++)  ` `        ``for` `(``int` `j = ``0``; j < N; j++)  ` `            ``mat[i][j] = mat[i][j] * k;  ` `} ` ` `  `// Driver code ` `public` `static` `void` `main (String[] args) ` `{ ` `    ``int` `mat[][] = { { ``1``, ``2``, ``3` `}, ` `                    ``{ ``4``, ``5``, ``6` `}, ` `                    ``{ ``7``, ``8``, ``9` `} }; ` `    ``int` `k = ``4``; ` ` `  `    ``scalarProductMat(mat, k); ` ` `  `    ``// to display the resultant matrix ` `    ``System.out.println(``"Scalar Product Matrix is : "``); ` `     `  `    ``for` `(``int` `i = ``0``; i < N; i++) ` `    ``{ ` `        ``for` `(``int` `j = ``0``; j < N; j++)  ` `            ``System.out.print(mat[i][j] + ``" "``); ` `        ``System.out.println(); ` `    ``} ` `} ` `} ` ` `  `// This code is contributed by Ajit. `

## Python 3

 `# Python 3 program to find the scalar  ` `# product of a matrix ` ` `  `# Size of given matrix ` `N ``=` `3` ` `  `def` `scalarProductMat( mat, k): ` ` `  `    ``# scalar element is multiplied  ` `    ``# by the matrix ` `    ``for` `i ``in` `range``( N): ` `        ``for` `j ``in` `range``( N):  ` `            ``mat[i][j] ``=` `mat[i][j] ``*` `k      ` ` `  `# Driver code ` `if` `__name__ ``=``=` `"__main__"``: ` `     `  `    ``mat ``=` `[[ ``1``, ``2``, ``3` `], ` `           ``[ ``4``, ``5``, ``6` `], ` `           ``[ ``7``, ``8``, ``9` `]] ` `    ``k ``=` `4` ` `  `    ``scalarProductMat(mat, k) ` ` `  `    ``# to display the resultant matrix ` `    ``print``(``"Scalar Product Matrix is : "``) ` `    ``for` `i ``in` `range``(N): ` `        ``for` `j ``in` `range``(N): ` `            ``print``(mat[i][j], end ``=` `" "``) ` `        ``print``() ` ` `  `# This code is contributed by ita_c `

## C#

 `// C# program to find ` `// the scalar product  ` `// of a matrix ` `using` `System; ` ` `  `class` `GFG{ ` ` `  `static` `int` `N = 3; ` `static` `void` `scalarProductMat(``int``[,] mat,  ` `                                  ``int` `k) ` `{ ` `     `  `    ``// scalar element is multiplied  ` `    ``// by the matrix ` `    ``for` `(``int` `i = 0; i < N; i++)  ` `        ``for` `(``int` `j = 0; j < N; j++)  ` `            ``mat[i,j] = mat[i, j] * k;      ` `} ` ` `  `// Driver code ` `static` `public` `void` `Main () ` `{ ` `    ``int``[,] mat = {{1, 2, 3}, ` `                  ``{4, 5, 6}, ` `                  ``{7, 8, 9}}; ` `    ``int` `k = 4; ` ` `  `    ``scalarProductMat(mat, k); ` ` `  `    ``// to display the resultant matrix ` `    ``Console.WriteLine(``"Scalar Product Matrix is : "``); ` `     `  `    ``for` `(``int` `i = 0; i < N; i++) { ` `        ``for` `(``int` `j = 0; j < N; j++)  ` `            ``Console.Write(mat[i, j] + ``" "``); ` `        ``Console.WriteLine(); ` `    ``} ` `} ` `} ` ` `  `// This code is contributed by Ajit. `

## PHP

 `

Output:

```Scalar Product Matrix is :
4 8 12
16 20 24
28 32 36
```

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Improved By : jit_t, chitranayal, Code_Mech

Article Tags :
Practice Tags :

1

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.