NAME: Abdul Moeed
REGISTRATION NUMBER: FA23-BSE-004
LAB: 10
Activity #01:
Write a Java program to accept a 3X4 array from user and find sum of each row.
import [Link];
public class A1 {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
int[][] arr = new int[3][4];
int[] sum = new int[3];
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 4; j++) {
[Link]("Enter element at position (" + i + ", " + j + "): ");
arr[i][j] = [Link]();
sum[i] += arr[i][j];
}
}
for (int i = 0; i < 3; i++) {
[Link]("Sum of row " + (i + 1) + " is " + sum[i]);
}
}
}
Activity #02:
Write a Java program to initialize a 3X3 2D array with natural numbers starting from 11 and count the
number of prime numbers in the given array.
➢ NOTE: You are required to send each array element to a user-defined method isPrime to know that
whether the given number is prime or not.
import [Link];
public class A2 {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
int[][] arr = new int[3][3];
int count = 0;
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
arr[i][j] = 11 + i * 3 + j;
if (isPrime(arr[i][j])) {
count++;
}
}
}
[Link]("The 3X3 array with natural numbers starting from 11 is:");
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
[Link](arr[i][j] + " ");
}
[Link]();
}
[Link]("The number of prime numbers in the given array is: " + count);
}
public static boolean isPrime(int n) {
if (n <= 1) {
return false;
}
for (int i = 2; i <= [Link](n); i++) {
if (n % i == 0) {
return false;
}
}
return true;
}
}
Activity #03:
Write a Java program to initialize a 3x4 integer array. Pass this array to a user-defined method transpose
and return the transpose of the given matrix. Display the returned matrix in main method.
import [Link];
public class A3 {
public static void main(String[] args) {
int matrix1[][] = {{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}};
[Link]("Original matrix is: ");
for (int i = 0; i < 3; i++) {
[Link]("|");
for (int j = 0; j < 4; j++)
[Link](matrix1[i][j] + "\t");
[Link]("|");
}
int transposeMatrix[][] = new int[4][3];
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 3; j++)
transposeMatrix[i][j] = matrix1[j][i];
}
[Link]("Transposed matrix is: ");
for (int i = 0; i < 4; i++) {
[Link]("|");
for (int j = 0; j < 3; j++)
[Link](transposeMatrix[i][j] + "\t");
[Link]("|");
}
}
}
Activity #04:
Write a Java program to accept a 4x4 integer array from your user and find sum of the diagonal.
import [Link];
public class A4 {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
int[][] matrix = new int[4][4];
for (int i = 0; i < 4; i++) {
[Link]("Enter array elements of row no "+(i+1)+" : " );
for (int j = 0; j < 4; j++) {
matrix[i][j]=[Link]();
}
}
[Link]("Original matrix is: ");
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++)
[Link](matrix[i][j]+"\t");
[Link]();
}
int sum=0;
for(int i=0;i<4;i++){
for(int j=0;j<4;j++){
if(i==j){
sum+=matrix[i][j];
break;
}
}
}
[Link]("Sum of diagonal is "+sum);
}
}
“Graded LAB Tasks”
Task 1:
Write a Java program to accept a 3x4 integer array from user. You are required to:
1. Find the row having maximum sum
2. Find the column having maximum sum.
import [Link];
public class T1 {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
int[][] array = new int[3][4];
[Link]("Enter elements of the 3x4 array:");
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 4; j++) {
[Link]("Enter element at position [" + (i + 1) + "][" + (j + 1) + "]: ");
array[i][j] = [Link]();
}
}
int maxRowSum = Integer.MIN_VALUE;
int maxRow = -1;
for (int i = 0; i < 3; i++) {
int rowSum = 0;
for (int j = 0; j < 4; j++) {
rowSum += array[i][j];
}
if (rowSum > maxRowSum) {
maxRowSum = rowSum;
maxRow = i;
}
}
int maxColSum = Integer.MIN_VALUE;
int maxCol = -1;
for (int j = 0; j < 4; j++) {
int colSum = 0;
for (int i = 0; i < 3; i++) {
colSum += array[i][j];
}
if (colSum > maxColSum) {
maxColSum = colSum;
maxCol = j;
}
}
[Link]("Row with maximum sum: " + (maxRow + 1));
[Link]("Column with maximum sum: " + (maxCol + 1));
[Link]();
}
}
Task #02:
Write a Java program to accept a 3x4 integer array from user. You are required to find the row or
column having maximum number of prime numbersint[ ][ ] matrix = new int[3][4];
Scanner scanner = new Scanner([Link]);
[Link]("Enter elements for the 3x4 matrix:");
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 4; j++) {
[Link]("Enter element at position [" + i + "][" + j + "]: ");
matrix[i][j] = [Link]();
[Link]("Row with the maximum number of prime numbers: " +
(findMaxPrimeRow(matrix) + 1));
[Link]();
public static int findMaxPrimeRow(int[][] matrix) {
int maxPrimeCount = 0;
int maxPrimeRow = -1;
for (int i = 0; i < [Link]; i++) {
int primeCount = countPrimes(matrix[i]);
if (primeCount > maxPrimeCount) {
maxPrimeCount = primeCount;
maxPrimeRow = i;
return maxPrimeRow;
public static int countPrimes(int[] row) {
int primeCount = 0;
for (int num : row) {
if (isPrime(num)) {
primeCount++;
return primeCount;
public static boolean isPrime(int num) {
if (num <= 1) {
return false;
for (int i = 2; i <= [Link](num); i++) {
if (num % i == 0) {
return false;
}
return true;
}
Task #03:
Write a program to accept a 3x4 matrix from user having integer values. Accept another 4x3 matrix from
user having integer values. You are required to multiply these two matrices and display the result.
import [Link];
public class T3{
public static int[][] multiplymatrix(int[][] mat1, int[][] mat2) {
int[][] miul= new int[[Link]][mat2[0].length];
for (int i = 0; i < [Link]; i++) {
for (int j = 0; j < mat2[i].length; j++) {
for (int k = 0; k < mat1[0].length; k++){
int num = mat1[i][j];
int num2 = mat2[j][i];
miul[i][j] += num * num2;
return miul;
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
int[][] matrix1 = new int[3][4];
[Link]("Enter the fistr matrix 3x4");
for (int i = 0; i < [Link]; i++) {
[Link]("Enter the values on row " + (i+1) + " :");
for (int j = 0; j < matrix1[i].length; j++) {
matrix1[i][j] = [Link]();
int[][] matrix2 = new int[4][3];
[Link]("Enter the second matrix 4x3");
for (int i = 0; i < [Link]; i++) {
[Link]("Enter the values on row " + (i+1) + " :");
for (int j = 0; j < matrix2[i].length; j++) {
matrix2[i][j] = [Link]();
if ([Link] == matrix2[0].length) {
int[][] mulip = multiplymatrix(matrix1, matrix2);
for(int[] row : mulip) {
for (int column : row) {
[Link](column + " ");
[Link]();