Adding TWO mATrix
public class AddMatrices
{
public static void main(String[] args)
{
int rows = 2, columns = 3;
int[][] firstMatrix = { {2, 3, 4}, {5, 2, 3} };
int[][] secondMatrix = { {-4, 5, 3}, {5, 6, 3} };
// 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();
}
}
}
OuTpuT
Sum Of TWO mATriceS iS:
2 8 7
10 8 6
AddiTiOn Of TWO mATrix
public class MatrixAdditionExample
{
public static void main(String args[])
{
//creating two matrices
int a[][]={{1,3,4},{2,4,3},{3,4,5}};
int b[][]={{1,3,4},{2,4,3},{1,2,4}};
1.
2. //creating another matrix to store the sum of two matrices
3. int c[][]=new int[3][3]; //3 rows and 3 columns
4.
5. //adding and printing addition of 2 matrices
6. for(int i=0;i<3;i++)
7. {
8. for(int j=0;j<3;j++)
9. {
10. c[i][j]=a[i][j]+b[i][j]; //use - for subtraction
11. System.out.print(c[i][j]+" ");
12. }
13. System.out.println();//new line
14. }
15. }
16. }
OuTpuT:
2 6 8
4 8 6
4 6 9
TrAnSpOSe Of mATrix
public class Transpose {
public static void main(String[] args) {
int row = 2, column = 3;
int[][] matrix = { {2, 3, 4}, {5, 6, 4} };
// Display current matrix
display(matrix);
// Transpose the matrix
int[][] transpose = new int[column][row];
for(int i = 0; i < row; i++) {
for (int j = 0; j < column; j++) {
transpose[j][i] = matrix[i][j];
}
}
// Display transposed matrix
display(transpose);
}
public static void display(int[][] matrix) {
System.out.println("The matrix is: ");
for(int[] row : matrix) {
for (int column : row) {
System.out.print(column + " ");
}
System.out.println();
}
}
}
Output
The matrix is:
2 3 4
5 6 4
The matrix is:
2 5
3 6
4 4
Sum Of eLemenTS in mATrix
public class MatrixDiagonals
{
static public void main(String[] args) {
int[][] input_matrix = {
{ 4, 5, 6, 7 },
{ 1, 7, 3, 4 },
{ 11, 12, 13, 14 },
{ 23, 24, 25, 50 }
};
int matrix_size = 4;
System.out.println("The matrix is defined as : ");
for (int i = 0; i < matrix_size; i++) {
for (int j = 0; j < matrix_size; j++)
System.out.print( input_matrix[i][j] + " ");
System.out.print("\n");
}
int principal_diagonal = 0, secondary_diagonal = 0;
for (int i = 0; i < matrix_size; i++) {
for (int j = 0; j < matrix_size; j++) {
if (i == j)
principal_diagonal += input_matrix[i][j];
if ((i + j) == (matrix_size - 1))
secondary_diagonal += input_matrix[i][j];
}
}
System.out.println("\n The sum of principal diagonal elements of the matrix is: " +
principal_diagonal);
System.out.println("\n The sum of secondary diagonal elements of the matrix is: " +
secondary_diagonal);
}
}
OuTpuT
4567
1734
11 12 13 14
23 24 25 50
The sum of principal diagonal elements of the matrix is: 74
The sum of secondary diagonal elements of the matrix is: 45
decLAre & iniTiALize A 2d ArrAy
public class Matrices
{
public static void main(String[] args)
{
// declare & initialize 2D arrays for int and string
int[][] matrix1 = new int[2][2];
int matrix2[][] = new int[2][3];
//the size of matrix3 will be 4x4
int[][] matrix3 = { { 3, 2, 1, 7 },
{ 9, 11, 5, 4 },
{ 6, 0, 13, 17 },
{ 7, 21, 14, 15 } };
String[][] matrix4 = new String[2][2];
//the size of matrix5 will be 2x3
// 3 cols because at max there are 3 columns
String[][] matrix5 = { { "a", "lion", "meo" },
{ "jaguar", "hunt" } };
}
}
Sum Of LefT & rigHT diAgnOLS Of mATrix
import java.util.Scanner;
public class CandidateCode
{
public static void main (String[]args)
{
// Input the matrix
Scanner input = new Scanner(System.in);
System.out.println("Enter the size of the square matrix:");
int size = input.nextInt(), lsum =0, rsum =0;
int [][]a = new int [size][size];
for (int i=0; i<size; i++)
{
for (int j=0; j<size;j++)
{
a[i][j] = input.nextInt();
}
}
// Calculate the sum of the left and right diagonals
for (int i=0; i<size; i++)
{
for (int j=0; j<size;j++)
{
if (i==j)
lsum = lsum + a[i][j];
else if ((i+j)==(size-1))
rsum = rsum + a[i][j];
else
continue;
}
}
System.out.println ("Left Diagonal Sum= "+lsum);
System.out.println ("Right Diagonal Sum= "+rsum);
}
}
Enter the size of the square matrix:
3
123
456
789
OuTpuT: Left Diagonal Sum =15
Right Diagonal Sum =10
Sum Of ALL eLemenTS in mATrix
import java.util.*;
public class matrix {
public static void main(String[] args) throws IOException {
int rows, cols;
int sum = 0;
int[][] matrix;
Scanner input = new Scanner(System.in);
System.out.print("Enter number of rows: ");
rows = input.nextInt();
System.out.print("Enter number of
columns: ");
cols = input.nextInt();
matrix = new int[rows][cols];
System.out.println("Enter elements for Matrix");
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
matrix[i][j] = input.nextInt();
}
}
System.out.println("Matrix is: ");
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
sum = sum + matrix[i][j];
System.out.print(matrix[i][j] + " ");
}
System.out.println();
}
System.out.println("Sum of all elements is: " + sum);
}
}
OuTpuT:
Enter number of rows: 3
Enter number of columns: 3
Enter elements for Matrix
1
2
3
4
5
6
7
8
9
Matrix is:
1 2 3
4 5 6
7 8 9
Sum of all elements is: 45
WriTe A prOgrAm TO decLAre A SingLe dimenSiOnAL ArrAy A[] And A SquAre
mATrix b[][] Of Size n, WHere n>2 And n<10. ALLOW THe uSer TO inpuT pOSiTive
inTegerS inTO THe SingLe dimenSiOnAL ArrAy.
exAmpLe :-
Input :N=3
Enter elements of single dimensional array : 3 1 7
OuTpuT : Sorted Array : 1 3 7
Filled Matrix
1 3 7
1 3 1
1 1 3
import java.io.*; import java.util.*;
class DDA
{
public static void main(String args[])
{
Scanner in = new Scanner(System.in);
int n,i,j,x,l,m,temp;
System.out.println("Enter Array Capacity");
n= in.nextInt(); // Input Array Capacity
int a[]= new int[n]; // Single Dimensional Array
int b[][]= new int[n][n]; // Double Dimension Array
System.out.println("Enter Array
Elements");
for(i=0;i<n;i++)
{
a[i]= in.nextInt(); // Storing numbers in array
}
System.out.println("Original 1D Array");
for(i=0;i<n;i++)
{
System.out.print(a[i]+"\t"); // Displaying unsorted
array
}
System.out.println();
for(i=0;i<n-1;i++)
{
for(j=0;j<((n-1)-i);j++)
{
if(a[j]>a[j+1]) // Sorting array
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}