write a program to find Largest and Smallest value in Array?

Here we can iterate the array and keep track of small and big element until the end of array.
When …. numbers[i] is greater than largest.big = numbers[i];

when numbers[i] greater than small
small = numbers[i];
The last statements are —
System.out.println(“Big- “+big+” Small- “+small);

				
					public class LargestSmallestArray {
	public static void main(String[] args) {
		int a[] = { 2, 3, 4, 56, 70, 1 };
		int small = a[0];
		int big = a[0];
		for (int i = 0; i < a.length; i++) {
			if (a[i] > big) {
				big = a[i];
			} else if (a[i] < small) {
				small = a[i];
			}
			System.out.println("Big- " + big + " Small- " + small);
		}
	}
}
				
			

Output :-
Big- 70
Small- 1

Leave a Comment

Your email address will not be published. Required fields are marked *