Java Program to Find the Minimum Element in the Array - The Coding Shala
Home >> Java Programs >> Find the Minimum Element in the Array
Other Posts You May Like
In this post, we will learn how to write a Java Program to Find the Minimum Element in the given Array.
Java Program to Find the Minimum Element in the Array
Write a Java Program to find the minimum element in the given array. The elements can be positive or negative in the array.
Example 1:
Input: [1, 2, 4, 0, -3, -33, 55, 33]
Output: -33
Java Program:
/** * https://www.thecodingshala.com/ */ public class Main { public static void printMinimum(int[] arr) { if (arr.length < 1) { System.out.println("Given array is empty"); } else { int max = arr[0]; for (int i=1; i<arr.length; i++) { if(arr[i] < max) { max = arr[i]; } } System.out.println("Minimum element in the array is: " + max); } } public static void main(String[] args) { int[] arr = {3, 4, -6, 2, 7, -9, 11, -44, 22, 3, 6}; printMinimum(arr); } }
Output:
Minimum element in the array is: -44
- Java Program to Find the Largest Element in the Array
- Java Program to Reverse an Array
- Java Program to Reverse a String
- Java Program to Find Sum of N Numbers
- Java Program to Generate Random Numbers
Comments
Post a Comment