C++

Find the Missing Number C++ Program – Detailed FAQ

There is a number missing from the list of numbers shown to us in ascending order. In order to find the missing number, we will create a software.
For instance, if a user enters the five digits in the following order: 1, 2, 3, 4, 6, the fifth number, 5, is still needed.

You need be familiar with for loops and functions at a basic level in order to comprehend this program.

Program

We are asking the user to enter the size of the array, or the amount of elements they desire to enter. If any elements are left off, the user is then requested to enter the elements in increasing order. The application locates the lacking component.

We have the following reasoning:
n(n+1)/2 is the sum of n integer elements. Since we are missing one element in this situation, we should change n to n+1, making the total number of elements in our example (n+1)(n+2)/2. When we have the total, we take out all of the user-inputted components to get the remaining value, which is the 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: "; 
   cin>>n;    int arr[n-1];  
   cout<<"Enter array elements: ";   
   for(int i=0; i<n; i++){    
      cin>>arr[i];  
   } 
   int missingNo = findMissingNo(arr,5); 
   cout<<"Missing Number is: "<<missingNo;
   return 0;
}

Output:

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

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button