Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,914 questions

51,847 answers

573 users

How to get the difference between the largest and smallest values in an array of integers in Java

1 Answer

0 votes
public class MyClass {
    public static void main(String args[]) {
        int array[] = {2, 4, 9, 8, 3, 5, 6};  
        
        int max = array[0];
	    int min = array[0];
	    
    	for (int i = 1; i < array.length; i++) {
    		if (array[i] > max)
    			max = array[i];
    		else if(array[i] < min)
    			min = array[i];
    	}
    	
    	System.out.println("max = " + max);	
    	System.out.println("min = " + min);	
    	System.out.println("max - min = " + (max - min));	
    }
}






/*
run:
 
max = 9
min = 2
max - min = 7
 
*/

 



answered Aug 20, 2021 by avibootz
...