How to search a missing number in an array that contains integers from 1 to 100? in C++

C++
x
24
#include <iostream>
using namespace std;
int main() {
int a[100];
for (int i = 0; i < 100; i++) {
a[i] = i + 1; //initialize the array with numbers from 1 to 100.
}
a[45] = 0; //make the number 45 missing.
int sum = 0, sum1 = 0;
for (int j = 0; j < 100; j++) { //calculate the sum of all numbers in the array.
sum += a[j];
}
for (int k = 1; k <= 100; k++) { //calculate the sum of all numbers from 1 to 100.
sum1 += k;
}
cout << "The missing number is: " << sum1 - sum << endl ; //the difference between these two sums is the missing number.
🤖 Code Explanation

More problems solved in C++


















