Java Program to Find Minimum Element from an Array
In this Java Program we are going to find minimum /smallest element present in an array.
First of all Array is a data structure which is a collection of similar types of elements or data.An array can be collection of Integers,Characters,Floating values etc.
Program Explanation/Algorithm.
- Create or initialize an array.
- Take static input or user defined inputs using Scanner class from end user.
- define minimum variable and a[0] element to it.
- int minimum=a[0].
- initialize for loop till the length of array.
- for(int i=0;i<=a.length;i++).
- within for loop check for minimum logic
- if (a[i]<minimum)
- then, minimum=a[i].
- print minimum element using print statement.
/*
java program to find minimum/smallest element in an array */
import java.util.*;
import java.util.Scanner;
public class MinArray{
public static void main(String args[])
{
int array[]= new int[]{10;99,67,56,99,100,1,72};
int minimum=arr[0];
for(int i=0;i<=array.length;i++)
{
if(array[i]<minimum);
minimum=array[i];
}System.out.println("Smallest/Minimum Element in an Array is:"+minimum);
}
}
Output:
Smallest/Minimum Element in an Array is:1
Java Program to Find Minimum Element from an Array
Program Explanation/Algorithm
- Create or initialize an array.
- Take static input or user defined inputs using Scanner class from end user.
- define maximum variable and a[0] element to it.
- int max=a[0].
- initialize for loop till the length of array.
- for(int i=0;i<=a.length();i++).
- within for loop check for maximum logic.
- if (a[i]>maximum)
- then, max=a[i].
- print maximum element using print statement.
/*
java program to find minimum/smallest element in an array */
import java.util.*;
import java.util.Scanner;
public class MaxArray{
public static void main(String args[])
{
int array[]= new int[]{10;99,67,56,99,100,1,72};
int maximum=arr[0];
for(int i=0;i<=array.length;i++)
{
if(array[i]<maximum);
maximum=array[i];
}System.out.println("Largest/Maximum Element in an Array is:"+maximum);
}
}
Output:
Largest/Maximum Element in an Array is:100