Java Program to Find the Minimum Element in the Array - The Coding Shala

Home >> Java Programs >> Find the Minimum Element in the Array

 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


Other Posts You May Like
Please leave a comment below if you like this post or found some errors, it will help me to improve my content.

Comments

Popular Posts from this Blog

Shell Script to find sum, product and average of given numbers - The Coding Shala

Shell Script to Create a Simple Calculator - The Coding Shala

Add two numbers in Scala - The Coding Shala

LeetCode - Number of Good Pairs Solution - The Coding Shala

Richest Customer Wealth LeetCode Solution - The Coding Shala