Java Program to Find the Largest and Smallest Number in an Array
The below Java code finds the Smallest and Largest number from an array and print the output.
Program:
//Largest and Smallest Number in an Array
public class ArraySmallestLargest {
public static void main(String args[]){
int x[]={56, 34, 43, 66, 78, 23};
int largest=0;
int smallest=0;
for (int i = 0; i < x.length; i++){
if(x[i] > largest)
largest=x[i];
else if(x[i] < smallest)
smallest = x[i];
}
System.out.println("Largest Number: " + largest);
System.out.println("Smallest Number: " + smallest);
}
}
Output:
Largest Number: 78
Smallest Number: 23