Here’s a Java program to find the missing number in an array of consecutive integers:
import java.util.Arrays;
public class MissingNumber {
public static void main(String[] args) {
int[] arr = {1, 2, 3, 4, 6, 7, 8, 9, 10};
int missingNum = findMissingNumber(arr);
System.out.println("Missing number in the array: " + missingNum);
}
public static int findMissingNumber(int[] arr) {
Arrays.sort(arr);
int expectedNum = arr[0];
for (int i = 0; i < arr.length; i++) {
if (arr[i] != expectedNum) {
return expectedNum;
}
expectedNum++;
}
return -1;
}
}
In this program, we first create an integer array of consecutive integers with one number missing. We then call the `findMissingNumber()` method to find the missing number.
The `findMissingNumber()` method takes an integer array as a parameter and returns the missing number. We first sort the array using the `Arrays.sort()` method. We then initialize a variable `expectedNum` to the first element of the array. We use a for loop to iterate over each element of the array. For each element, we check if it is equal to `expectedNum`. If it is not, then we return `expectedNum`, which is the missing number. If it is, then we increment `expectedNum`. We repeat this process until we find the missing number.
At the end of the program, we print out the missing number in the array.