Reading and Printing Matrix
import java.util.Scanner;
public class MatrixInput {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// Reading number of rows and columns
System.out.println("Enter the number of rows:");
int rows = scanner.nextInt();
System.out.println("Enter the number of columns:");
int columns = scanner.nextInt();
// Initializing the matrix
int[][] matrix = new int[rows][columns];
// Reading the matrix elements
System.out.println("Enter the elements of the matrix:");
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
matrix[i][j] = scanner.nextInt();
}
}
// Displaying the matrix
System.out.println("The matrix is:");
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
System.out.print(matrix[i][j] + " ");
}
System.out.println();
}
scanner.close();
}
}
***************************************************************************
Matrix Addition
public class MatrixAddition {
public static void main(String[] args) {
int rows = 2, columns = 3;
int[][] firstMatrix = { {2, 3, 4}, {5, 2, 3} };
int[][] secondMatrix = { {1, 5, 2}, {4, 3, 2} };
// Adding two matrices
int[][] sum = new int[rows][columns];
for(int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
sum[i][j] = firstMatrix[i][j] + secondMatrix[i][j];
}
}
// Displaying the result
System.out.println("Sum of two matrices is: ");
for(int[] row : sum) {
for (int column : row) {
System.out.print(column + " ");
}
System.out.println();
}
}
}
*****************************************************************
Matrix Multiplication
public class MatrixMultiplication {
public static void main(String[] args) {
int rows1 = 2, columns1 = 3;
int rows2 = 3, columns2 = 2;
int[][] firstMatrix = { {2, 3, 4}, {5, 2, 3} };
int[][] secondMatrix = { {3, 4}, {1, 2}, {5, 6} };
// Multiplying two matrices
int[][] product = new int[rows1][columns2];
for(int i = 0; i < rows1; i++) {
for (int j = 0; j < columns2; j++) {
for (int k = 0; k < columns1; k++) {
product[i][j] += firstMatrix[i][k] * secondMatrix[k][j];
}
}
}
// Displaying the result
System.out.println("Product of two matrices is: ");
for(int[] row : product) {
for (int column : row) {
System.out.print(column + " ");
}
System.out.println();
}
}
}
*************************************************************************
Matrix Transpose
public class MatrixTranspose {
public static void main(String[] args) {
int rows = 2, columns = 3;
int[][] matrix = { {2, 3, 4}, {5, 6, 7} };
// Transposing the matrix
int[][] transpose = new int[columns][rows];
for(int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
transpose[j][i] = matrix[i][j];
}
}
// Displaying the result
System.out.println("Transpose of the matrix is: ");
for(int[] row : transpose) {
for (int column : row) {
System.out.print(column + " ");
}
System.out.println();
}
}
}
***************************************************************
Find smallest element in each row
import java.util.Scanner;
public class SmallestElementInRows {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// Reading number of rows and columns
System.out.println("Enter the number of rows:");
int rows = scanner.nextInt();
System.out.println("Enter the number of columns:");
int columns = scanner.nextInt();
// Initializing the matrix
int[][] matrix = new int[rows][columns];
// Reading the matrix elements
System.out.println("Enter the elements of the matrix:");
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
matrix[i][j] = scanner.nextInt();
}
}
// Finding the smallest element in each row
for (int i = 0; i < rows; i++) {
int smallest = matrix[i][0];
for (int j = 1; j < columns; j++) {
if (matrix[i][j] < smallest) {
smallest = matrix[i][j];
}
}
System.out.println("The smallest element in row " + (i + 1) + " is: " + smallest);
}
scanner.close();
}
}
Comments
Post a Comment