Find the Missing Number

noidatut course

We are given a list of numbers in increasing order, but there is a missing number in the list. We will write a program to find that missing number. For example, when user enters the 5 numbers in that order: 1, 2, 3, 4, 6 then the missing number is 5.

To understand this program, you should have the basic knowledge of for loop and functions.


We are asking user to input the size of array which means the number of elements user wish to enter, after that user is asked to enter those elements in increasing order by missing any element. The program finds the missing element.

The logic we are using is:

Sum of n integer elements is: n(n+1)/2. Here we are missing one element which means we should replace n with n+1 so the total of elements in our case becomes: (n+1)(n+2)/2. Once we have the total, we are removing all the elements that user has entered from the total, this way the remaining value is our missing number.

#include <iostream>  
using namespace std;  
int findMissingNo (int arr[], int len){
     int temp;
       temp  = ((len+1)*(len+2))/2;
       for (int i = 0; i<len; i++)
            temp -= arr[i];
       return temp;  }
  int main() {
     int n;
        cout<<"Enter the size of array: ";
    int arr[n-1];
       cout<<"Enter array elements: ";
        for(int i=0; i<n; i++){
      int missingNo = findMissingNo(arr,5);
      cout<<"Missing Number is: "<<missingNo;
     return 0;  }


Enter the size of array: 5
Enter array elements: 1
Missing Number is: 4

Leave Comment