Java Program To Find Minimum and Maximum Element From An Array
===========================================================================
import java.util.Scanner;
public class MinAndMaxElementFromArray {
public static void main(String[] args) {
// Accept the Array Size and Array Elements from the console (User)
int arr[]; // Defining an Array
System.out.println("Enter the size of an Array:");
Scanner sc = new Scanner(System.in);
Integer size = sc.nextInt(); // Accept the Size Of Array From console
arr = new int[size]; // Declaration of an array
// Accepting the elements from console (User)
for (int i = 0; i < arr.length; i++) {
System.out.println("Enter the " + (i + 1) + " Element");
Integer element = sc.nextInt(); // Accepting one by one element from the User / Console
arr[i] = element; // Copy one by one element into Array
}
// Printing the Elements Of an array To Console
// This loop is used for displaying elements on the console
System.out.println("The Elements of an Array are :");
System.out.print("{");
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i] + ",");
}
System.out.println("}");
// Find Out The Max Element From An Array
// Loop for finding out Max Element From An Array
Integer max = arr[0];
for (int i = 1; i < arr.length; i++) {
if (arr[i] > max) {
max = arr[i];
}
}
System.out.println("The Maximum Element Of An Array is:" + max);
// Find Out The Min Element From An Array
// Loop for finding out Min Element From An Array
Integer min = arr[0];
for (int i = 1; i < arr.length; i++) {
if (arr[i] < min) {
min = arr[i];
}
}
System.out.println("The Minimum Element Of An Array is:" + min);
}
}
Comments
Post a Comment