Write a program to find Larget element of an Array
public class Large1{
public static void
main(String[] args) {
double[]
numArray = { 23.4, -55, 50.0, 33.5, 55.5, 100, 5.7, -66.5 };
double large =
numArray[0];
for (double
num: numArray) {
if(large
< num)
large
= num;
}
System.out.format("Largest element = %.2f", large);
}
}
When you run the program, the output will be:
debug:
Largest element = 100.00
In the above program, we store the first element of the array in
the variable large.
Then, large is used to compare other elements in the array. If any
number is greater than large, large is assigned the number.
In this way, the largest number is stored in large when it is printed.
0 comments:
Post a Comment