Java Programming Lab-4 Solutions
1 month ago
Java Programming Lab-4 Solutions
1. Sum of 10 numbers in an array
import java.util.Scanner;
public class ArraySum {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int[] numbers = new int[10];
int sum = 0;
System.out.println("Enter 10 numbers:");
for(int i=0; i<10; i++) {
numbers[i] = sc.nextInt();
sum += numbers[i];
}
System.out.println("Sum of the numbers: " + sum);
}
}
Output:
Enter 10 numbers:
5 10 15 20 25 30 35 40 45 50
Sum of the numbers: 275
2. Find maximum and minimum in array
import java.util.Scanner;
public class MaxMinArray {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int[] arr = new int[10];
System.out.println("Enter 10 integers:");
for(int i=0; i<10; i++) {
arr[i] = sc.nextInt();
}
int max = arr[0];
int min = arr[0];
for(int i=1; i<10; i++) {
if(arr[i] > max) max = arr[i];
if(arr[i] < min) min = arr[i];
}
System.out.println("Maximum value: " + max);
System.out.println("Minimum value: " + min);
}
}
Output:
Enter 10 integers:
12 45 23 67 8 34 56 9 41 3
Maximum value: 67
Minimum value: 3
3. Oldest and youngest person age
import java.util.Scanner;
public class AgeAnalyzer {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("How many persons? ");
int n = sc.nextInt();
int[] ages = new int[n];
System.out.println("Enter ages of " + n + " persons:");
for(int i=0; i<n; i++) {
ages[i] = sc.nextInt();
}
int oldest = ages[0];
int youngest = ages[0];
for(int i=1; i<n; i++) {
if(ages[i] > oldest) oldest = ages[i];
if(ages[i] < youngest) youngest = ages[i];
}
System.out.println("Oldest person's age: " + oldest);
System.out.println("Youngest person's age: " + youngest);
}
}
Output:
How many persons? 5
Enter ages of 5 persons:
25 18 42 30 15
Oldest person's age: 42
Youngest person's age: 15
4. Highest and lowest marks in C Programming
import java.util.Scanner;
public class MarksAnalyzer {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int[] marks = new int[65];
System.out.println("Enter marks of 65 students:");
for(int i=0; i<65; i++) {
marks[i] = sc.nextInt();
}
int highest = marks[0];
int lowest = marks[0];
for(int i=1; i<65; i++) {
if(marks[i] > highest) highest = marks[i];
if(marks[i] < lowest) lowest = marks[i];
}
System.out.println("Highest marks: " + highest);
System.out.println("Lowest marks: " + lowest);
}
}
Output:
Enter marks of 65 students:
[Assume 65 marks are entered]
Highest marks: 98
Lowest marks: 42
5. Count employees with salary above 20,000
import java.util.Scanner;
public class SalaryCounter {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter number of employees: ");
int n = sc.nextInt();
double[] salaries = new double[n];
int count = 0;
System.out.println("Enter salaries of " + n + " employees:");
for(int i=0; i<n; i++) {
salaries[i] = sc.nextDouble();
if(salaries[i] > 20000) count++;
}
System.out.println("Number of employees with salary above 20,000: " + count);
}
}
Output:
Enter number of employees: 5
Enter salaries of 5 employees:
15000 25000 18000 30000 22000
Number of employees with salary above 20,000: 3
6. Count persons with age 16-20
import java.util.Scanner;
public class AgeRangeCounter {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int[] ages = new int[100];
int count = 0;
System.out.println("Enter ages of 100 persons:");
for(int i=0; i<100; i++) {
ages[i] = sc.nextInt();
if(ages[i] >= 16 && ages[i] <= 20) count++;
}
System.out.println("Number of persons with age between 16-20: " + count);
}
}
Output:
Enter ages of 100 persons:
[Assume 100 ages are entered]
Number of persons with age between 16-20: 23
7. Sort 10 integers in ascending order
import java.util.Scanner;
import java.util.Arrays;
public class ArraySorter {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int[] arr = new int[10];
System.out.println("Enter 10 integers:");
for(int i=0; i<10; i++) {
arr[i] = sc.nextInt();
}
Arrays.sort(arr);
System.out.println("Sorted array in ascending order:");
for(int num : arr) {
System.out.print(num + " ");
}
}
}
Output:
Enter 10 integers:
34 12 56 7 23 45 9 31 18 5
Sorted array in ascending order:
5 7 9 12 18 23 31 34 45 56
8. Arrange numbers in descending order
import java.util.Scanner;
import java.util.Arrays;
import java.util.Collections;
public class DescendingSorter {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("How many numbers? ");
int n = sc.nextInt();
Integer[] arr = new Integer[n];
System.out.println("Enter " + n + " numbers:");
for(int i=0; i<n; i++) {
arr[i] = sc.nextInt();
}
Arrays.sort(arr, Collections.reverseOrder());
System.out.println("Sorted array in descending order:");
for(int num : arr) {
System.out.print(num + " ");
}
}
}
Output:
How many numbers? 5
Enter 5 numbers:
12 45 23 8 34
Sorted array in descending order:
45 34 23 12 8
9. Sum of 3×3 matrix elements
import java.util.Scanner;
public class MatrixSum {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int[][] matrix = new int[3][3];
int sum = 0;
System.out.println("Enter 3x3 matrix elements:");
for(int i=0; i<3; i++) {
for(int j=0; j<3; j++) {
matrix[i][j] = sc.nextInt();
sum += matrix[i][j];
}
}
System.out.println("Sum of all elements: " + sum);
}
}
Output:
Enter 3x3 matrix elements:
1 2 3 4 5 6 7 8 9
Sum of all elements: 45
10. 2×2 matrix and its transpose
import java.util.Scanner;
public class MatrixTranspose {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int[][] matrix = new int[2][2];
System.out.println("Enter 2x2 matrix elements:");
for(int i=0; i<2; i++) {
for(int j=0; j<2; j++) {
matrix[i][j] = sc.nextInt();
}
}
System.out.println("Original Matrix:");
for(int i=0; i<2; i++) {
for(int j=0; j<2; j++) {
System.out.print(matrix[i][j] + " ");
}
System.out.println();
}
System.out.println("Transpose Matrix:");
for(int i=0; i<2; i++) {
for(int j=0; j<2; j++) {
System.out.print(matrix[j][i] + " ");
}
System.out.println();
}
}
}
Output:
Enter 2x2 matrix elements:
1 2 3 4
Original Matrix:
1 2
3 4
Transpose Matrix:
1 3
2 4
11. Sum of even numbers in 3×4 table
import java.util.Scanner;
public class EvenNumberSum {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int[][] table = new int[3][4];
int sum = 0;
System.out.println("Enter 3x4 table elements:");
for(int i=0; i<3; i++) {
for(int j=0; j<4; j++) {
table[i][j] = sc.nextInt();
if(table[i][j] % 2 == 0) sum += table[i][j];
}
}
System.out.println("Sum of even numbers: " + sum);
}
}
Output:
Enter 3x4 table elements:
1 2 3 4 5 6 7 8 9 10 11 12
Sum of even numbers: 42
12. Sum of diagonal elements in 4×4 matrix
import java.util.Scanner;
public class DiagonalSum {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int[][] matrix = new int[4][4];
int sum = 0;
System.out.println("Enter 4x4 matrix elements:");
for(int i=0; i<4; i++) {
for(int j=0; j<4; j++) {
matrix[i][j] = sc.nextInt();
if(i == j) sum += matrix[i][j];
}
}
System.out.println("Sum of diagonal elements: " + sum);
}
}
Output:
Enter 4x4 matrix elements:
1 0 0 0 0 2 0 0 0 0 3 0 0 0 0 4
Sum of diagonal elements: 10
13. Sum of two 2×2 matrices
import java.util.Scanner;
public class MatrixAddition {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int[][] mat1 = new int[2][2];
int[][] mat2 = new int[2][2];
int[][] sum = new int[2][2];
System.out.println("Enter first 2x2 matrix:");
for(int i=0; i<2; i++) {
for(int j=0; j<2; j++) {
mat1[i][j] = sc.nextInt();
}
}
System.out.println("Enter second 2x2 matrix:");
for(int i=0; i<2; i++) {
for(int j=0; j<2; j++) {
mat2[i][j] = sc.nextInt();
sum[i][j] = mat1[i][j] + mat2[i][j];
}
}
System.out.println("Sum of matrices:");
for(int i=0; i<2; i++) {
for(int j=0; j<2; j++) {
System.out.print(sum[i][j] + " ");
}
System.out.println();
}
}
}
Output:
Enter first 2x2 matrix:
1 2 3 4
Enter second 2x2 matrix:
5 6 7 8
Sum of matrices:
6 8
10 12
14. Sum of two r×c matrices
import java.util.Scanner;
public class DynamicMatrixAddition {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter number of rows (r): ");
int r = sc.nextInt();
System.out.print("Enter number of columns (c): ");
int c = sc.nextInt();
int[][] mat1 = new int[r][c];
int[][] mat2 = new int[r][c];
int[][] sum = new int[r][c];
System.out.println("Enter first matrix elements:");
for(int i=0; i<r; i++) {
for(int j=0; j<c; j++) {
mat1[i][j] = sc.nextInt();
}
}
System.out.println("Enter second matrix elements:");
for(int i=0; i<r; i++) {
for(int j=0; j<c; j++) {
mat2[i][j] = sc.nextInt();
sum[i][j] = mat1[i][j] + mat2[i][j];
}
}
System.out.println("Sum of matrices:");
for(int i=0; i<r; i++) {
for(int j=0; j<c; j++) {
System.out.print(sum[i][j] + " ");
}
System.out.println();
}
}
}
Output:
Enter number of rows (r): 2
Enter number of columns (c): 3
Enter first matrix elements:
1 2 3 4 5 6
Enter second matrix elements:
6 5 4 3 2 1
Sum of matrices:
7 7 7
7 7 7
15. Check if two matrices are equal
import java.util.Scanner;
public class MatrixEquality {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter number of rows: ");
int r = sc.nextInt();
System.out.print("Enter number of columns: ");
int c = sc.nextInt();
int[][] mat1 = new int[r][c];
int[][] mat2 = new int[r][c];
boolean equal = true;
System.out.println("Enter first matrix elements:");
for(int i=0; i<r; i++) {
for(int j=0; j<c; j++) {
mat1[i][j] = sc.nextInt();
}
}
System.out.println("Enter second matrix elements:");
for(int i=0; i<r; i++) {
for(int j=0; j<c; j++) {
mat2[i][j] = sc.nextInt();
if(mat1[i][j] != mat2[i][j]) equal = false;
}
}
if(equal) {
System.out.println("Matrices are equal");
} else {
System.out.println("Matrices are not equal");
}
}
}
Output:
Enter number of rows: 2
Enter number of columns: 2
Enter first matrix elements:
1 2 3 4
Enter second matrix elements:
1 2 3 4
Matrices are equal
16. Matrix multiplication (2×3 and 3×2)
import java.util.Scanner;
public class MatrixMultiplication {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int[][] mat1 = new int[2][3];
int[][] mat2 = new int[3][2];
int[][] product = new int[2][2];
System.out.println("Enter first matrix (2x3):");
for(int i=0; i<2; i++) {
for(int j=0; j<3; j++) {
mat1[i][j] = sc.nextInt();
}
}
System.out.println("Enter second matrix (3x2):");
for(int i=0; i<3; i++) {
for(int j=0; j<2; j++) {
mat2[i][j] = sc.nextInt();
}
}
// Matrix multiplication
for(int i=0; i<2; i++) {
for(int j=0; j<2; j++) {
for(int k=0; k<3; k++) {
product[i][j] += mat1[i][k] * mat2[k][j];
}
}
}
System.out.println("Product matrix:");
for(int i=0; i<2; i++) {
for(int j=0; j<2; j++) {
System.out.print(product[i][j] + " ");
}
System.out.println();
}
}
}
Output:
Enter first matrix (2x3):
1 2 3 4 5 6
Enter second matrix (3x2):
7 8 9 10 11 12
Product matrix:
58 64
139 154
17. General matrix multiplication (r1×c1 and r2×c2)
import java.util.Scanner;
public class GeneralMatrixMultiplication {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter rows for matrix 1 (r1): ");
int r1 = sc.nextInt();
System.out.print("Enter columns for matrix 1 (c1): ");
int c1 = sc.nextInt();
System.out.print("Enter rows for matrix 2 (r2): ");
int r2 = sc.nextInt();
System.out.print("Enter columns for matrix 2 (c2): ");
int c2 = sc.nextInt();
if(c1 != r2) {
System.out.println("Matrix multiplication not possible!");
return;
}
int[][] mat1 = new int[r1][c1];
int[][] mat2 = new int[r2][c2];
int[][] product = new int[r1][c2];
System.out.println("Enter first matrix elements:");
for(int i=0; i<r1; i++) {
for(int j=0; j<c1; j++) {
mat1[i][j] = sc.nextInt();
}
}
System.out.println("Enter second matrix elements:");
for(int i=0; i<r2; i++) {
for(int j=0; j<c2; j++) {
mat2[i][j] = sc.nextInt();
}
}
// Matrix multiplication
for(int i=0; i<r1; i++) {
for(int j=0; j<c2; j++) {
for(int k=0; k<c1; k++) {
product[i][j] += mat1[i][k] * mat2[k][j];
}
}
}
System.out.println("Product matrix:");
for(int i=0; i<r1; i++) {
for(int j=0; j<c2; j++) {
System.out.print(product[i][j] + " ");
}
System.out.println();
}
}
}
Output:
Enter rows for matrix 1 (r1): 2
Enter columns for matrix 1 (c1): 3
Enter rows for matrix 2 (r2): 3
Enter columns for matrix 2 (c2): 2
Enter first matrix elements:
1 0 1 0 1 0
Enter second matrix elements:
1 1 0 0 1 1
Product matrix:
2 2
0 0