Java Program To Find Largest Element Of Array
Chapter:
Miscellaneous
Last Updated:
21-09-2017 16:53:39 UTC
Program:
/* ............... START ............... */
public class JavaFindLargestElementOfArray {
public static void main(String[] args) {
double[] numArray = { 23.4, -34.5, 50.0, 33.5, 55.5, 43.7, 5.7, -66.5 };
double largest = numArray[0];
for (double num : numArray) {
if (largest < num)
largest = num;
}
System.out.format("Largest element = %.2f", largest);
}
}
/* ............... END ............... */
Output
Notes:
-
In the above program, we store all the elements in an double array named as numArray.
- Variable Largest is used to compare other elements in the array.
- If any number is greater than largest, largest is assigned the number.
- After the iteration largest number is stored in largest variable and print the same using System.out.println.
Tags
Find Largest Element Of Array, Java, Miscellaneous