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


Introduction

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..


techstudy.org

HP 15 Core i3 7th gen Laptop(4GB, 1TB HDD, Windows 10) | Rs. 31,490

  1.  
  2. import java.util.Arrays;
  3. public class Javaexcercise {
  4.  
  5. static int max;
  6. static int min;
  7.  
  8. public static void max_min(int myarray[]) {
  9. max = myarray[0];
  10. min = myarray[0];
  11. int len = myarray.length;
  12. for (int i = 1; i < len - 1; i = i + 2) {
  13. if (i + 1 > len) {
  14. if (myarray[i] > max) max = myarray[i];
  15. if (myarray[i] < min) min = myarray[i];
  16. }
  17. if (myarray[i] > myarray[i + 1]) {
  18. if (myarray[i] > max) max = myarray[i];
  19. if (myarray[i + 1] < min) min = myarray[i + 1];
  20. }
  21. if (myarray[i] < myarray[i + 1]) {
  22. if (myarray[i] < min) min = myarray[i];
  23. if (myarray[i + 1] > max) max = myarray[i + 1];
  24. }
  25. }
  26. }
  27.  
  28. public static void main(String[] args) {
  29. int[] myarray = {1,2,3,4,5,6,7,8,9,10};
  30. max_min(myarray);
  31. System.out.println(" Original Array: "+Arrays.toString(myarray));
  32. System.out.println(" Maximum value for the above array = " + max);
  33. System.out.println(" Minimum value for the above array = " + min);
  34. }
  35. }

Result

java techstudy.org

     





Related Topics




Popular Posts