This is a Java Program to Identify Missing Numbers in a Given Array.
We made a method as getMissingNo in which we pass the array and length of the array as arguments and using this method we calculate the missing number and hence we get the desired output.
Here is the source code of the Java Program to Identify Missing Numbers in a Given Array. The Java program is successfully compiled and run on a Windows system. The program output is also shown below.
public class Missing
{
static int getMissingNo (int a[], int n)
{
int i, total;
total = (n + 1) * (n + 2) / 2;
for ( i = 0; i < n; i++)
total -= a[i];
return total;
}
public static void main(String... s)
{
int a[ ] = {1, 2, 4, 5, 6};
int miss = getMissingNo(a, 5);
System.out.println("The number missing is :"+miss);
}
}
Output:
$ javac Missing.java $ java Missing The number missing is :3
Sanfoundry Global Education & Learning Series – 1000 Java Programs.
advertisement
Here’s the list of Best Books in Java Programming, Data Structures and Algorithms.
Related Posts:
- Check Programming Books
- Apply for Computer Science Internship
- Practice Information Technology MCQs
- Practice Programming MCQs
- Apply for Java Internship