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.
Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!
advertisement
advertisement
Here’s the list of Best Books in Java Programming, Data Structures and Algorithms.
Next Steps:
- Get Free Certificate of Merit in Java Programming
- Participate in Java Programming Certification Contest
- Become a Top Ranker in Java Programming
- Take Java Programming Tests
- Chapterwise Practice Tests: Chapter 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
- Chapterwise Mock Tests: Chapter 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
Related Posts:
- Apply for Information Technology Internship
- Buy Java Books
- Practice BCA MCQs
- Apply for Java Internship
- Buy Programming Books