Solution:
This is a pretty much easy program to write. We will declare a variable (say maximum) and store the value of the first element of the given array. Furthermore, we will check every element of the array and compare with the variable maximum. By the end of the iteration, we will have our largest element inside our function largest(). We will call upon the function inside our main method and simply print it.
public class Test{
static int myarr[] = {10,24,15,9,98};
static int largest(){
int n;
int maximum = myarr[0];
for (n = 1; n < myarr.length; n++)
if (myarr[n] > maximum)
maximum = myarr[n];
return maximum;
}
public static void main(String[] args){
System.out.println("Largest number is: " + largest());
}
}
I hope you get it. Leave a kudos if you have got the solution.
Thanks.