Write a Java program to get the difference between the largest and smallest values in an array of integers


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. import java.util.Arrays;
  2. public class Javaexcercise {
  3. public static void main(String[] args)
  4. {
  5. int[] myarray = {10,20,30,40,50,60,70,80,90};
  6. System.out.println("Original Array: "+Arrays.toString(myarray));
  7. int maxval = myarray[0];
  8. int min = myarray[0];
  9. for(int i = 1; i < myarray.length; i++)
  10. {
  11. if(myarray[i] > maxval)
  12. maxval = myarray[i];
  13. else if(myarray[i] < min)
  14. min = myarray[i];
  15. }
  16. System.out.println("Difference between the largest and smallest araay element is: "+(maxval - min));
  17. }
  18. }

Result

java techstudy.org

     





Related Topics




Popular Posts