Q:

Write a Java program to find the maximum and minimum value of an array

belongs to collection: Array Programs in Java with Examples

0

Write a Java program to find the maximum and minimum value of an array

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

In this demo I have used NetBeans IDE 8.2 for debugging purpose. But you can use any java programming language compiler as per your availability..

import java.util.Arrays; 
public class Javaexcercise {
 
  static int max;
  static int min;
 
    public static void max_min(int myarray[]) {
        max = myarray[0];
        min = myarray[0];
        int len = myarray.length;
        for (int i = 1; i < len - 1; i = i + 2) {
            if (i + 1 > len) {
                if (myarray[i] > max) max = myarray[i];
                if (myarray[i] < min) min = myarray[i];
            }
            if (myarray[i] > myarray[i + 1]) {
                if (myarray[i] > max) max = myarray[i];
                if (myarray[i + 1] < min) min = myarray[i + 1];
            }
            if (myarray[i] < myarray[i + 1]) {
                if (myarray[i] < min) min = myarray[i];
                if (myarray[i + 1] > max) max = myarray[i + 1];
            }
        }
    }
 
    public static void main(String[] args) {
           int[] myarray = {1,2,3,4,5,6,7,8,9,10};
        max_min(myarray);
        System.out.println(" Original Array: "+Arrays.toString(myarray));
        System.out.println(" Maximum value for the above array = " + max);
        System.out.println(" Minimum value for the above array = " + min);
    }
}

Result:

Original Array: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

Maximum value for the above array = 9

Minimum value for the above array = 1

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Array Programs in Java with Examples

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
Write a Java program to insert an element (specifi... >>
<< Write a Java program to test if an array contains ...