Java Project to find the Largest Number
Find the Largest Number using a For Loop and Arrays Class:
Find the Largest Number:
Input: An array or list of numbers.
Output: The largest number in the list.
Example:
- Input: [12, 45, 3, 89, 22]
- Output: "Largest number: 89"
- Input: [7, 2, 9, 5]
- Output: "Largest number: 9"
Solution 1: Find the Largest number using a For Loop
Code:
Output:
Enter the number of elements: 5 Enter the numbers: 23 23 4564 4565 23423 Largest number: 23423
Explanation :
- Input: User inputs the size of the array and the numbers.
- Processing: The program assumes the first number is the largest, then iterates through the array, updating the largest value if a bigger number is found.
- Output: The largest number is displayed.
Solution 2: Find the Largest number using Java Arrays Class
Code:
Output:
Enter the number of elements: 7 Enter the numbers: 12 33 78 -34 0 63 45 Largest number: 78
Explanation:
- Input: User enters the size of the array and the elements.
- Processing: The Arrays.sort() method sorts the numbers, and the largest number is the last element in the sorted array.
- Output: The largest number is printed.
Java Code Editor: