Answer:
//This Program is written in C++
// Comments are used for explanatory purpose
#include <iostream>
using namespace std;
enum mailbox{open, close};
int box[149];
void closeAllBoxes();
void OpenClose();
void printAll();
int main()
{
closeAllBoxes();
OpenClose();
printAll();
return 0;
}
void closeAllBoxes()
{
for (int i = 0; i < 150; i++) //Iterate through from 0 to 149 which literarily means 1 to 150
{
box[i] = close; //Close all boxes
}
}
void OpenClose()
{
for(int i = 2; i < 150; i++) {
for(int j = i; j < 150; j += i) {
if (box[j] == close) //Open box if box is closed
box[j] = open;
else
box[j] = close; // Close box if box is opened
}
}
// At the end of this test, all boxes would be closed
}
void printAll()
{
for (int x = 0; x < 150; x++) //use this to test
{
if (box[x] = 1)
{
cout << "Mailbox #" << x+1 << " is closed" << endl;
// Print all close boxes
}
}
}
Explanation: