read n-1 distinct positive integers from user, all of which are less than or equal to a integer ‘n’. Find the integer that is missing from the range [1,2,...,n] without using array.
/* c program to get missing integer without using array */
#include<stdio.h>
int main()
{
int n,i,k,sum;
sum=0;
printf("number of numbers: ");
scanf("%d",&n);
for(i=0;i<n-1;i++) /* loop for reading 'n-1' number of numbers */
{
scanf("%d",&k);
sum+=k;
}
k=(n*(n+1))/2;
printf("\n missing integer=%d",k-sum);
return 0;
}
sample input and output:
number of numbers: 8
1 3 5 4 2 6 8
missing integer=7