Java Program to Find the Largest Element in the Array - The Coding Shala
Home >> Java Programs >> Find Largest Element in the Array
Other Posts You May Like
In this post, we will learn how to Find the Largest Element in the given Array using Java.
Java Program to Find the Largest Element in the Array
In the given array, we need to print the maximum element of the array. The array can have negative and positive numbers.
Example 1:
Input: arr = {3, 5, 2, 5, -1, -33, 11}
Output: 11
Java Program:
/** * https://www.thecodingshala.com/ */ public class Main { public static void printLargest(int[] arr) { if (arr.length < 1) { System.out.println("Given array is empty"); } else { int max = arr[0]; for (int i=1; i<arr.length; i++) { if(arr[i] > max) { max = arr[i]; } } System.out.println("Largest element in the array is: " + max); } } public static void main(String[] args) { int[] arr = {3, 4, -6, 2, 7, -9, 11, -44, 22, 3, 6}; printLargest(arr); } }
Output:
Largest element in the array is: 22
- Java Program to Reverse an Array
- Java Program to Count Negative Numbers in a Sorted Matrix
- Java Program to Sort an Array in Descending Order
- Java Program to Print Sum of all the Elements of the Array
- Basic Calculator in java
Comments
Post a Comment