JAVA PROGRAM
Program to find smallest, largest, second largest number using array | Java Program|
Introduction
Working with arrays in Java is a fundamental skill that is essential for anyone learning the language. One common task is to find the smallest, largest, and second largest numbers in an array. This blog post will walk you through a Java program that performs these operations.
Program Overview
The Java program below prompts the user to input 10 integers into an array. It then iterates through the array to find the smallest, largest, and second largest numbers. This is a useful exercise to understand array manipulation, conditional statements, and basic logic in Java.
import java.io.*;
class arrays {
public static void main(String args[]) throws IOException {
int a[] = new int[10];
DataInputStream s = new DataInputStream(System.in);
// Input 10 elements from the user
System.out.println("Enter 10 elements:");
for (int i = 0; i < 10; i++)
a[i] = Integer.parseInt(s.readLine());
// Initialize variables to store the largest, smallest, and second largest values
int max = a[0];
int min = a[0];
int smax = a[0];
// Find the smallest and largest elements
for (int i = 0; i < 10; i++) {
if (a[i] > max)
max = a[i];
if (a[i] < min)
min = a[i];
}
// Find the second largest element
for (int i = 0; i < 10; i++) {
if ((a[i] > smax) && a[i] != max)
smax = a[i];
}
// Display the results
System.out.println("Smallest = " + min);
System.out.println("Largest = " + max);
System.out.println("Second Largest = " + smax);
}
}
Output
Enter 10 elements:
2
5
30
41
52
10
22
14
56
32
Smallest = 2
Largest = 56
Second Largest = 52
Explanation
1. Array Initialization and Input:
The program starts by creating an integer array a of size 10. It then uses a for loop to prompt the user to input 10 integers, which are stored in the array.
2. Finding the Smallest and Largest Numbers:
The program initializes three variables: max, min, and smax (second max) with the first element of the array (a[0]).
Another for loop iterates through the array:
If an element is greater than max, it updates max.
If an element is smaller than min, it updates min.
3. Finding the Second Largest Number:
After identifying the maximum number, the program uses a second for loop to find the second largest number: It checks if the current element is greater than smax and not equal to max. If both conditions are true, smax is updated.
4. Displaying the Results:
Finally, the program prints out the smallest, largest, and second largest numbers.
Conclusion
Finding the smallest, largest, and second largest numbers in an array is a common problem that helps you understand basic array operations and control structures in Java. This program is an excellent exercise for beginners looking to strengthen their understanding of arrays and conditional logic.
By working through this example, you’ll gain a better grasp of how to work with arrays in Java, which is a fundamental skill for any Java programmer. Happy coding!
Post a Comment
0 Comments