xxxxxxxxxx
public class MinimumNumber{
public static void main(String[] args){
int[] arr=new int[]{2,3,4,1};
int min=arr[0];
for(int i=0;i<arr.length;i++){
if(arr[i]<min){
min=arr[i];
}
}
System.out.println("Minimum Number in array:"+min);
}
}
xxxxxxxxxx
private static int findMin(int[] array) {
int min = array[0];
for(int i=1;i<array.length;i++) {
if(min > array[i]) {
min = array[i];
}
}
return min;
}
xxxxxxxxxx
int /*OR*/ double /*OR*/ float[] array_of_numbers = {/*Whatever values you want*/}
/*For this example we're just using a regular integer array*/
int min = array_of_numbers[0]; /*Important to initialise the first value!!!!*/
for (int i = 0; i < array_of_numbers.length; i++) {
if(array_of_numbers[i] < min) { /* So now we can make the comparison*/
min = array_of_numbers[i]; /* If smaller, make it the new minimum*/
}
}
System.out.println("Minimum value in array: " + min)
xxxxxxxxxx
// Java program to find minimum (or maximum)
// element in an array.
import java.io.*;
class GFG {
static int getMin(int arr[], int n)
{
int res = arr[0];
for (int i = 1; i < n; i++)
res = Math.min(res, arr[i]);
return res;
}
static int getMax(int arr[], int n)
{
int res = arr[0];
for (int i = 1; i < n; i++)
res = Math.max(res, arr[i]);
return res;
}
// Driver code
public static void main(String[] args)
{
int arr[] = { 12, 1234, 45, 67, 1 };
int n = arr.length;
System.out.println("Minimum element of array: " + getMin(arr, n));
System.out.println("Maximum element of array: " + getMax(arr, n));
}
}
// This code is contributed by Aditya Kumar (adityakumar129)
xxxxxxxxxx
// Java program of above implementation
public class GFG {
/* Class Pair is used to return two values from getMinMax() */
static class Pair {
int min;
int max;
}
static Pair getMinMax(int arr[], int n) {
Pair minmax = new Pair();
int i;
/*If there is only one element then return it as min and max both*/
if (n == 1) {
minmax.max = arr[0];
minmax.min = arr[0];
return minmax;
}
/* If there are more than one elements, then initialize min
and max*/
if (arr[0] > arr[1]) {
minmax.max = arr[0];
minmax.min = arr[1];
} else {
minmax.max = arr[1];
minmax.min = arr[0];
}
for (i = 2; i < n; i++) {
if (arr[i] > minmax.max) {
minmax.max = arr[i];
} else if (arr[i] < minmax.min) {
minmax.min = arr[i];
}
}
return minmax;
}
/* Driver program to test above function */
public static void main(String args[]) {
int arr[] = {1000, 11, 445, 1, 330, 3000};
int arr_size = 6;
Pair minmax = getMinMax(arr, arr_size);
System.out.printf("\nMinimum element is %d", minmax.min);
System.out.printf("\nMaximum element is %d", minmax.max);
}
}
xxxxxxxxxx
public class MaxMinArray {
public static void main(String[] args) {
int[] array = {5, 8, 2, 10, 1, 6};
int maximum = array[0]; // Initialize maximum as the first element
int minimum = array[0]; // Initialize minimum as the first element
for (int i = 1; i < array.length; i++) {
if (array[i] > maximum) {
maximum = array[i]; // Update maximum if a larger element is found
}
if (array[i] < minimum) {
minimum = array[i]; // Update minimum if a smaller element is found
}
}
System.out.println("Maximum number: " + maximum);
System.out.println("Minimum number: " + minimum);
}
}