Java Tutorial
Defines integer array with initialized values and finds the minimum element in the integer array by passing integer array to findMin method and gets the min value to print the result.
displayArrayNumbers method also uses passing array concept to display the initialized numbers to the number array.
class MinTest { static int findMin(int[] numbers){ int min = numbers[0]; for (int i=1;i<numbers.length;i++){ if(min>numbers[i]){ min = numbers[i]; } } return min; } static void displayArrayNumbers(int[] numbers){ for (int i=1;i<numbers.length;i++){ System.out.println("Number '"+i+"': "+numbers[i]); } } public static void main(String[] args){ int[] numbers = {5, 7, 8, 2, 3}; int min = findMin(numbers); System.out.println("Finds Minimum Numbers in Array of Integers"); displayArrayNumbers(numbers); System.out.println("Minimum Element is '"+min+"'"); } }Output:
D:\Java_Programs>javac MinTest.java D:\Java_Programs>java MinTest Finds Minimum Numbers in Array of Integers Number '1': 7 Number '2': 8 Number '3': 2 Number '4': 3 Minimum Element is '2'
Java Tutorial
Privacy Policy | Copyright2020 - All Rights Reserved. | Contact us | Report website issues in Github | Facebook page | Google+ page