Pandas multiply column by scalar: In the previous article, we have discussed Java Program to Sort the elements of a Matrix
In this article we are going to see how we can write a program to find the transpose of a matrix in JAVA language.
Java Program to Find Scalar Multiplication of a Matrix
Matrix multiplication in java: A 3*3 Matrix is having 3 rows and 3 columns where this 3*3 represents the dimension of the matrix. Means there are 3*3 i.e. total 9 elements in a 3*3 Matrix.
Let’s understand it in more simpler way.
| A00 A01 A02 |
Matrix A = | A10 A11 A12 |
| A20 A21 A22 | 3*3
Matrix Arepresents a 3*3 matrix.- ‘
A‘ represents the matrix element - ‘
Aij‘ represents the matrix element at it’s matrix position/index. - ‘
i‘ represents the row index - ‘
j‘ represents the column index - Means
A00=Aijwherei=0andj=0,A01=aijwherei=0andj=1and like this. - Here we have started
rowvalue from 0 andcolumnvalue from 0.
Let’s see different ways to find Scalar Multiplication of a Matrix.
Method-1: Java Program to Find Scalar Multiplication of a Matrix By Static Initialization of Array Elements
Approach:
- Initialize and declare one array of size 3×3 with elements.
- Use two for loops to iterate the rows and columns.
- For each element multiply it with the integer.
- Print the resultant array.
Program:
import java.io.*;
import java.util.*;
public class matrix
{
public static void main(String args[])
{
// Initializing the 3X3 matrix i.e. 2D array
int arr[][] = {{7,2,4},{3,2,6},{7,5,6}};
//resultant matrix
int res[][] = new int[3][3];
int row, col, n = 5;
System.out.print("The matrix elements are : ");
printMatrix(arr);
// Multiplying the matrix using a constant
for(row=0;row<3;row++)
for(col=0;col<3;col++)
arr[row][col] *= n;
System.out.print("\nThe matrix after multiplication with : "+n);
printMatrix(arr);
}
// Method to print the matrix
static void printMatrix(int arr[][])
{
int row, col;
// Loop to print the elements
for(row=0;row<3;row++)
{
// Used for formatting
System.out.print("\n");
for(col=0;col<3;col++)
{
System.out.print(arr[row][col]+" ");
}
}
System.out.print("\n");
}
}
Output: The matrix elements are : 7 2 2 4 2 6 7 2 6 The matrix after multiplication with : 5 35 10 10 20 10 30 35 10 30
Method-2: Java Program to Find Scalar Multiplication of a Matrix By Dynamic Initialization of Array Elements
Approach:
- Declare two arrays of size 3×3. One to hold the original matrix and another to hold the resultant matrix.
- Ask the user for input of array elements and store them in the one array using two for loops.
- Ask the user to enter an integer to multiply.
- Use two for loops to iterate the rows and columns .
- For each element multiply it with the integer.
- Print the resultant array.
Program:
import java.util.Scanner;
public class matrix
{
public static void main(String args[])
{
//Scanner class to take input
Scanner scan = new Scanner(System.in);
// Initializing the 3X3 matrix i.e. 2D array
int arr[][] = new int[3][3];
//for resultant matrix
int res[][] = new int[3][3];
int row, col ,n;
// Taking matrix input
System.out.println("\nEnter matrix elements : ");
for(row=0;row<3;row++)
for(col=0;col<3;col++)
arr[row][col] = scan.nextInt();
// Taking the number input
System.out.println("Enter a number to multiply : ");
n = scan.nextInt();
System.out.print("The matrix elements are : ");
printMatrix(arr);
// Multiplying the matrix using a constant
for(row=0;row<3;row++)
for(col=0;col<3;col++)
arr[row][col] *= n;
System.out.print("\nThe matrix after multiplication with : "+n);
printMatrix(arr);
}
// Method to print the matrix
static void printMatrix(int arr[][])
{
int row, col;
// Loop to print the elements
for(row=0;row<3;row++)
{
// Used for formatting
System.out.print("\n");
for(col=0;col<3;col++)
{
System.out.print(arr[row][col]+" ");
}
}
System.out.print("\n");
}
}
Output: Enter matrix elements : 9 8 3 4 5 6 7 3 8 Enter a number to multiply : 4 The matrix elements are : 9 8 3 4 5 6 7 3 8 The matrix after multiplication with : 4 36 32 12 16 20 24 28 12 32
Are you a job seeker and trying to find simple java programs for Interview? This would be the right choice for you, just tap on the link and start preparing the java programs covered to crack the interview.
Related Java Programs: