DEV Community

Roshan Shambharkar
Roshan Shambharkar

Posted on

Find The largest Element In the array

For Finding The largest Element In array you have knowledge array traveling and some condition statements like if and else for solving this we are not using else statements but as beginner you have knowledge of if and else statements

Going Forward follow below mentioned steps

Step:- 1 Define the size of array As a N = arr.length;
Step:- 2 Now Travels To whole Array Using for loop
Step:- 3 Now Inside for loop use if statements where you can add some conditions like if(arr[i] > max) note the max variable you have to define in beginning of program like int max= arr[0]
Step:-4 now if our above conditions is true then assign max value to arr[i] using equals operater

Step:- 5 Now Print the max value of arr using System.out.println();

Above Steps Implement below programs

class FindLargestElementInTheArray {
public static void main(string args[]) {
int arr[] = {10, 20, 50, 30, 60};
int N = arr.length;
int max = arr[0];
for(int i = 0; i<N; i++) {
if(arr[i]>max) {
max = arr[i];
}
}
System.out.println(max);
}
}

OUTPUT:- 60

Top comments (0)