1.
Find the Second Largest Number in an Array
public class SecondLargest {
public static void main(String[] args) {
int[] array = {10, 20, 30, 40, 50};
int largest = Integer.MIN_VALUE;
int secondLargest = Integer.MIN_VALUE;
// Find the largest and second largest numbers
for (int num : array) {
if (num > largest) {
secondLargest = largest;
largest = num;
} else if (num > secondLargest && num != largest) {
secondLargest = num;
}
}
if (secondLargest == Integer.MIN_VALUE) {
System.out.println("There is no second largest number.");
} else {
System.out.println("The second largest number is: " +
secondLargest);
}
}
}
O/P:
Input:’[10,20,30,40,50]’
Output: ’The Largest number is : 40’
3. Find the biggest number in the array.
public class Largest {
public static void main(String[] args) {
int[] array = {10, 20, 30, 40, 50};
int largest = Integer.MIN_VALUE;
// Find the largest number
for (int num : array) {
if (num > largest) {
largest = num;
}
}
System.out.println("The largest number is: " + largest);
}
}
O/P:
Input:’[10,20,30,40,50]’
Output: ’The Largest number is : 50’
4. Write a program to get n values from the user
and display the count of positive numbers, negative
numbers and zeros entered by the user
import java.util.Scanner;
public class CountNumbers {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the number of values (n): ");
int n = scanner.nextInt();
int positiveCount = 0;
int negativeCount = 0;
int zeroCount = 0;
System.out.println("Enter " + n + " values:");
for (int i = 0; i < n; i++) {
int num = scanner.nextInt();
if (num > 0) {
positiveCount++;
} else if (num < 0) {
negativeCount++;
} else {
zeroCount++;
}
}
System.out.println("Count of positive numbers: " +
positiveCount);
System.out.println("Count of negative numbers: " +
negativeCount);
System.out.println("Count of zeros: " + zeroCount);
}
}
O/P:
Enter the number of values (n): 5 Enter 5 values: 10 -20 30 0 -40
* Output:
```
Count of positive numbers: 2
Count of negative numbers: 2
Count of zeros: 1
5. Calculate the sum of every row of a two-
dimensional integer array.
public class RowSum {
public static void main(String[] args) {
int[][] array = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};
for (int i = 0; i < array.length; i++) {
int rowSum = 0;
for (int j = 0; j < array[i].length; j++) {
rowSum += array[i][j];
}
System.out.println("Sum of row " + (i + 1) + ": " + rowSum);
}
}
}
O/P:
int[][] array = {{1,2,3},{4,5,6},{7,8,9}};
* Output:
```
Sum of row 1: 6
Sum of row 2: 15
Sum of row 3: 24
6. Find common elements between two integer
arrays
import java.util.Arrays;
public class CommonElements {
public static void main(String[] args) {
int[] array1 = {1, 2, 3, 4, 5};
int[] array2 = {4, 5, 6, 7, 8};
// Convert arrays to sets
java.util.Set<Integer> set1 = new
java.util.HashSet<>(Arrays.asList(arrayToArrayList(array1)));
java.util.Set<Integer> set2 = new
java.util.HashSet<>(Arrays.asList(arrayToArrayList(array2)));
// Find common elements
set1.retainAll(set2);
// Print common elements
System.out.println("Common elements: " + set1);
}
// Helper method to convert int array to ArrayList
public static ArrayList<Integer> arrayToArrayList(int[] array) {
ArrayList<Integer> list = new ArrayList<>();
for (int i : array) {
list.add(i);
}
return list;
}
}
O/P:
int[] array1 = {1, 2, 3, 4, 5}; int[] array2 = {4, 5, 6, 7, 8};
* Output:
```
Common elements: [4, 5]
7. Create a class Distance with instance variables km
and m. Use parameterized constructor to initialize
two objects d1 and d2. Perform the addition of two
distances d1 and d2 to print the result
public class Distance {
private int km;
private int m;
// Parameterized constructor
public Distance(int km, int m) {
this.km = km;
this.m = m;
}
// Method to add two distances
public Distance add(Distance other) {
int totalM = this.m + other.m;
int newKm = this.km + other.km + (totalM / 1000);
int newM = totalM % 1000;
return new Distance(newKm, newM);
}
// Method to print the distance
public void print() {
System.out.println(km + " km " + m + " m");
}
public static void main(String[] args) {
// Create two Distance objects
Distance d1 = new Distance(5, 200);
Distance d2 = new Distance(3, 400);
// Add the two distances
Distance result = d1.add(d2);
// Print the result
System.out.print("Distance 1: ");
d1.print();
System.out.print("Distance 2: ");
d2.print();
System.out.print("Result: ");
result.print();
}
}
O/P:
Distance 1: 5 km 200 m Distance 2: 3 km 400 m Result: 8 km 600 m
8. Month of the number – switch case
public class MonthFinder {
public static void main(String[] args) {
int monthNumber = 5; // Change this to find the month for a
different number
switch (monthNumber) {
case 1:
System.out.println("January");
break;
case 2:
System.out.println("February");
break;
case 3:
System.out.println("March");
break;
case 4:
System.out.println("April");
break;
case 5:
System.out.println("May");
break;
case 6:
System.out.println("June");
break;
case 7:
System.out.println("July");
break;
case 8:
System.out.println("August");
break;
case 9:
System.out.println("September");
break;
case 10:
System.out.println("October");
break;
case 11:
System.out.println("November");
break;
case 12:
System.out.println("December");
break;
default:
System.out.println("Invalid month number");
break;
}
}
}
O/P:
Input : monthNumber = 5
Output : May
9. Day of the given number- switch case
public class DayFinder {
public static void main(String[] args) {
int dayNumber = 3; // Change this to find the day for a different
number
switch (dayNumber) {
case 1:
System.out.println("Monday");
break;
case 2:
System.out.println("Tuesday");
break;
case 3:
System.out.println("Wednesday");
break;
case 4:
System.out.println("Thursday");
break;
case 5:
System.out.println("Friday");
break;
case 6:
System.out.println("Saturday");
break;
case 7:
System.out.println("Sunday");
break;
default:
System.out.println("Invalid day number");
break;
}
}
}
Output:
Input: dayNumber = 3
Ouput: Wednesday