program that mimics a mailman conducting an experiment in flipping mailboxes Solution
write a program that mimics a mailman conducting an experiment in flipping mailboxes. A flip means opening a mailbox if it is closed, and closing a mailbox if it is open. There are 100 mailboxes numbered 1 to 100 that are all initially closed. In the first step, visiting every mailbox (i.e. mailboxes 1, 2, 3, ..., 100), he flips them all. Then in the second step, visiting every second mailbox (i.e. mailboxes 2, 4, 6, ..., 100), he flips them. Then in the third step, visiting every third mailbox (i.e. mailboxes 3, 6, 9, ..., 99), he flips them. In this manner he goes in steps of 4, 5, ..., 10,...,50, and finally 100, wherein he directly goes to the 100th mailbox and flips it. At the end of this process, which mailboxes are open? Hint: Set up an array of Booleans all initialized to false representing closed mailboxes. Then, in steps of 1, 2, 3, …, 100 go through the sequence of mailboxes and toggle them. In the end, print out the mailbox numbers that are open (have the value true).