# Java Program to Find Largest and Smallest Number in an Array

• Java program to find the maximum and minimum element in an array.
• Write a program in Java to find largest and smallest number in an integer array.

Given an integer array of size N, we have to find the maximum and minimum element of input array.

Input Array
4 2 7 1 0 -4 10 12
Maximum Element : 12
Minimum Element : -4
Algorithm to find minimum and maximum elements of an array
Let inputArray is an integer array having N elements.
• We will use two integer variable "max" and "min". Initialize them with first element of input array(inputArray[0]).
• Using for loop, traverse inputArray from array from index 0 to N-1.
• If current element is more than max, then update max with current element.
• Else If, current element is less than min, then update min with current element.
• At the end of for loop, "max" and "min" will contain the maximum and minimum elements of inputArray.

## Java program to find max and min number in an array

package com.tcc.java.programs;

import java.util.*;

public class ArrayMaxMinElement {
public static void main(String args[]) {
int count, max, min, i;
int[] inputArray = new int[500];

Scanner in = new Scanner(System.in);

System.out.println("Enter number of elements");
count = in.nextInt();
System.out.println("Enter " + count + " elements");

for(i = 0; i < count; i++) {
inputArray[i] = in.nextInt();
}

max = min = inputArray[0];

for(i = 1; i < count; i++) {
if(inputArray[i] > max)
max = inputArray[i];
else if (inputArray[i] < min)
min = inputArray[i];

}

System.out.println("Largest Number : " + max);
System.out.println("Smallest Number : " + min);
}
}
Output
Enter number of elements
6
Enter 6 elements
7 2 5 1 9 3
Largest Number : 9
Smallest Number : 1