All we have to do in this problem is to consider 0 as the largest number then loop all the elements of the array and if any number is greater than the currently set largest number then we will replace the largest number with the new number that is greater and we keep doing this till the end of the array.
class findlargest{ public int largest(int arr[], int n) { int largestNum =0; for(int i=0; i<arr.length; i++){ if(arr[i]>largestNum){ largestNum=arr[i]; } } return largestNum; } }
Leave a Reply