Q:

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

belongs to collection: Array Programs in Java with Examples

0

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

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 {
 public static void main(String[] args)
 {
    int[] myarray = {10,20,30,40,50,60,70,80,90};
    System.out.println("Original Array: "+Arrays.toString(myarray)); 
    int maxval = myarray[0];
    int min = myarray[0];
    for(int i = 1; i < myarray.length; i++)
    {
        if(myarray[i] > maxval)
            maxval = myarray[i];
        else if(myarray[i] < min)
            min = myarray[i];
    }
    System.out.println("Difference between the largest and smallest araay element is: "+(maxval - min));    
 }
}

Result:

Original Array: [10, 20, 30, 40, 50, 60, 70, 80, 90]

Difference between the largest and smallest araay element is: 80

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 segregate all 0s on left s... >>
<< Write a Java program to find the number of even an...