Java Write Program Find Second Largest Element Array Ask User Enter Array Size Fill Random Q37281443

in java

Write a program to find second largest element in an array. Askthe user to enter the array size. Fill it with random numbers 0-99.Declare two variables max1 and max2 to store first and secondlargest elements. Store the first array element in both max1 andmax2.
Iterate through all array elements. If current array element isgreater than max1, then make largest element as second largest andcurrent array element as largest.

Else if the current array element is greater than max2 but lessthan max1 then make current array element as second largest i.e.max2 = arr[i].
Example:
Enter size of the array (1-100): 10

Random array: 7 2

OR
OR

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.