Find the Missing Number

Find the Missing Number
1. Get the sum of numbers
       total = n*(n+1)/2
2  Subtract all the numbers from sum and
   you will get the missing number.

#include<stdio.h>

int MissingNo(int a[],int n)
{
    int i,sum;
    sum=(n+1)*(n+2)/2; //n*(n+1)/ but no. is missing so (a,7) in array 7 element..so to find sum of 8 element sum=(n+1)*(n+2)/2 including missing number
 
    for(i=0;i<n;i++)
       sum=sum-a[i];
    return sum;
}

int main()
{
    int a[] = {1,2,4,5,6,7,8};
    int Number;

    Number=MissingNo(a,7);

    printf("Missing No. is:%d\n",Number);
}

Related Posts