Table of Contents
Print All Elements of a Two-Dimensional Array
Given a two-dimensional array containing integer values, write a Java program to display all the elements in matrix format. Each row should be printed on a separate line.
// Java program to print all elements of
// two-dimensional array
public class PrintMatrix
{
public static void main(String[] args)
{
int[][] arr =
{
{10, 20, 30},
{40, 50, 60},
{70, 80, 90}
};
for(int i = 0; i arr.length; i++)
{
for(int j = 0; j arr[i].length; j++)
{
System.out.print(arr[i][j] + " ");
}
System.out.println();
}
}
}
Output:
Explanation:10 20 30
40 50 60
70 80 90
The outer loop traverses each row, while the inner loop traverses each column and prints the elements.
Find the Sum of All Elements
Given a two-dimensional array of integers, write a Java program to calculate the sum of all elements present in the array.
// Java program to find sum of all elements
public class SumMatrixElements
{
public static void main(String[] args)
{
int[][] arr =
{
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};
int sum = 0;
for(int i = 0; i arr.length; i++)
{
for(int j = 0; j arr[i].length; j++)
{
sum += arr[i][j];
}
}
System.out.println("Sum = " + sum);
}
}
Output:
Explanation:Sum = 45
The program visits every element of the matrix and adds it to the sum variable.
Find the Largest Element
Given a two-dimensional array, write a Java program to identify and display the largest element stored in the array.
// Java program to find the largest element
public class LargestElement
{
public static void main(String[] args)
{
int[][] arr =
{
{12, 45, 7},
{89, 34, 21},
{5, 99, 15}
};
int largest = arr[0][0];
for(int i = 0; i arr.length; i++)
{
for(int j = 0; j arr[i].length; j++)
{
if(arr[i][j] largest)
{
largest = arr[i][j];
}
}
}
System.out.println("Largest Element = " + largest);
}
}
Output:
Explanation:Largest Element = 99
The program compares every element with the current largest value and updates it whenever a larger value is found.
Find the Smallest Element
Given a two-dimensional array of integers, write a Java program to find and display the smallest element present in the array.
// Java program to find th
// smallest element
public class SmallestElement
{
public static void main(String[] args)
{
int[][] arr =
{
{12, 45, 7},
{89, 34, 21},
{5, 99, 15}
};
int smallest = arr[0][0];
for(int i = 0; i arr.length; i++)
{
for(int j = 0; j arr[i].length; j++)
{
if(arr[i][j] smallest)
{
smallest = arr[i][j];
}
}
}
System.out.println("Smallest Element = " + smallest);
}
}
Output:
Smallest Element = 5
Calculate Row-Wise Sum
Given a matrix of integers, write a Java program to calculate the sum of elements in each row and display the row-wise sums separately.
// Java program to calculate
// row-wise sum
public class RowWiseSum
{
public static void main(String[] args)
{
int[][] arr =
{
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};
for(int i = 0; i arr.length; i++)
{
int rowSum = 0;
for(int j = 0; j arr[i].length; j++)
{
rowSum += arr[i][j];
}
System.out.println("Row " + (i + 1) + " Sum = " + rowSum);
}
}
}
Output:
Row 1 Sum = 6
Row 2 Sum = 15
Row 3 Sum = 24
Calculate Column-Wise Sum
Given a two-dimensional array, write a Java program to calculate the sum of elements in each column and display the result for every column.
// Java program to calculate
// column-wise sum
public class ColumnWiseSum
{
public static void main(String[] args)
{
int[][] arr =
{
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};
for(int j = 0; j arr[0].length; j++)
{
int colSum = 0;
for(int i = 0; i arr.length; i++)
{
colSum += arr[i][j];
}
System.out.println("Column " + (j + 1) + " Sum = " + colSum);
}
}
}
Output:
Column 1 Sum = 12
Column 2 Sum = 15
Column 3 Sum = 18
Find the Sum of Main Diagonal Elements
Given a square matrix, write a Java program to calculate the sum of all elements present on the main diagonal.
// Java program to find sum of
// main diagonal elements
public class DiagonalSum
{
public static void main(String[] args)
{
int[][] arr =
{
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};
int sum = 0;
for(int i = 0; i arr.length; i++)
{
sum += arr[i][i];
}
System.out.println("Diagonal Sum = " + sum);
}
}
Output:
Diagonal Sum = 15
Find the Transpose of a Matrix
Given a matrix, write a Java program to find and display its transpose.
// Java program to find transpose of
// a matrix
public class MatrixTranspose
{
public static void main(String[] args)
{
int[][] arr =
{
{1, 2, 3},
{4, 5, 6}
};
for(int j = 0; j arr[0].length; j++)
{
for(int i = 0; i arr.length; i++)
{
System.out.print(arr[i][j] + " ");
}
System.out.println();
}
}
}
Output:
1 4
2 5
3 6
Search an Element in a Matrix
Given a two-dimensional array and a target value, write a Java program to search for the target element in the matrix.
// Java program to search an element
// in the matrix
public class SearchElement
{
public static void main(String[] args)
{
int[][] arr =
{
{10, 20, 30},
{40, 50, 60},
{70, 80, 90}
};
int target = 50;
boolean found = false;
for(int i = 0; i arr.length; i++)
{
for(int j = 0; j arr[i].length; j++)
{
if(arr[i][j] == target)
{
found = true;
}
}
}
if(found)
{
System.out.println("Element Found");
}
else
{
System.out.println("Element Not Found");
}
}
}
Output:
Element Found
Conclusion
Two-dimensional arrays are essential for working with matrix-like data in Java. Programs such as matrix traversal, finding sums, searching elements, calculating row-wise and column-wise totals, and performing transpose operations help build a strong understanding of array manipulation. By practicing these common programs, you can improve your coding skills and prepare for more advanced programming challenges.Frequently Asked Questions
1. What is a two-dimensional array in Java?2. Why are nested loops used with two-dimensional arrays?A two-dimensional array is an array of arrays that stores data in rows and columns.
3. What is the transpose of a matrix?Nested loops are used because the array contains both rows and columns that must be traversed separately.
4. Can two-dimensional arrays have different row sizes?A transpose is obtained by interchanging the rows and columns of a matrix.
5. Where are two-dimensional arrays used in real-world applications?Yes, Java supports jagged arrays where each row can have a different number of elements.
They are used in spreadsheets, game boards, image processing, scientific calculations, and matrix operations.
0 Comments