public class FindLargestNumberExample {
public static void main(String[] args) {
int[] numbers = {10, 5, 20, 15, 25};
int largestNumber = numbers[0]; // Assume the first number is the largest
for (int i = 1; i < numbers.length; i++) {
if (numbers[i] > largestNumber) {
largestNumber = numbers[i];
}
}
System.out.println("The largest number is: " + largestNumber);
}
}
In this example, we have an array of numbers called numbers. We assume that the first number in the array (numbers[0]) is the largest number initially.
We then iterate over the remaining numbers in the array using a for loop starting from index 1. Inside the loop, we compare each number with the current largestNumber. If a number is found to be greater than the largestNumber, we update the largestNumber variable with the new value.
Finally, after the loop ends, we have found the largest number among the given set of numbers. We print it to the console using System.out.println().
Output:
The largest number is: 25
No comments:
Post a Comment