-
Notifications
You must be signed in to change notification settings - Fork 48
Expand file tree
/
Copy pathTaskOne.java
More file actions
29 lines (25 loc) · 877 Bytes
/
TaskOne.java
File metadata and controls
29 lines (25 loc) · 877 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
//Write a method in Java to get the difference between the largest and smallest values
//in an array of integers. The length of the array must be 1 and above.
//Use loops and conditionals to develop the algorithm.
public class TaskOne {
public static void main(String[] args){
int[] nums = {5, 7, 8};
if(nums.length<1){
System.out.println("Invalid array length");
}else{
int largest = nums[0];
for(int num : nums){
if(num>largest){
largest=num;
}
}
int smallest = largest;
for(int num : nums){
if(num<smallest){
smallest=num;
}
}
System.out.println("Amplitude between largest and smallest number is: " + (largest - smallest));
}
}
}