Wednesday, Jul 15, 2026 · Practical tutorials, weekly

How to find max and min in array in java Using Scanner Class

S Saurabh K Jul 11, 2022 2 min read 0 comments

In java there is no any built-in functions for finding minimum and maximum values of an array, and to find that we would have to code by ourself;

Before going to start Coding, if you don’t know the basics about Arrays in Java then watch this video below first:

1. Java Program to find the minimum element of an array:

import java.util.Scanner;

public class ArrayMin{
public static void main(String[] args){
		
int n, min;
Scanner sc = new Scanner(System.in);
System.out.println("Enter the no of elements in Array");
n = sc.nextInt();
int a[] = new int[n];
System.out.println("Enter elements in Array");
for(int i =0; i< n; i++){
a[i] = sc.nextInt();
}
min = a[0];
for(int i =0; i< n; i++){
if(min > a[i]){
min = a[i];
}
}
System.out.println("Minimum Element is " +min);

}	
}

Result:

how-to-find-min-element-in-array-java

2. Java Program to find the maximum element of an array:

import java.util.Scanner;

public class ArrayMin{
public static void main(String[] args){
		
int n, max;
Scanner sc = new Scanner(System.in);
System.out.println("Enter the no of elements in Array");
n = sc.nextInt();
int a[] = new int[n];
System.out.println("Enter elements in Array");
for(int i =0; i< n; i++){
a[i] = sc.nextInt();
}
max = a[0];
for(int i =0; i< n; i++){
if(max < a[i]){
max = a[i];
}
}
System.out.println("Maximum Element is " +max);

}	
}

Result:

Share:
Saurabh K

Saurabh K

Author at Technical Speaks

Writes practical guides and tutorials to help readers build, rank, and grow online.

Leave a Comment

Newsletter

Stay ahead of the curve

Join readers getting our freshest tutorials and guides on web, SEO, tech & more — delivered weekly.