Solution
- Set the first element as largest.
- Loop through the array and check if current value > largest, then set current value to largest.
package com.java.programs;
public class LargestNumber {
public static int findLargestNumber(int numbers[]) {
int largest = numbers[0];
for (int number: numbers) {
if (number > largest) {
largest = number;
}
}
return largest;
}
public static void main(String[] args) {
int[] numbers = {2,5,12,3,7,9,1};
System.out.println(findLargestNumber(numbers));
}
}
12