HomeTagSubmit NotesAsk AnythingLoginSubscribe Us
1. Feel Free to ask and submit anything on and get satisfactory answer
2. Registration is not compulsory, you can directly login via google or facebook
3. Our Experts are looking for yours ?.

corejava-programming: Program to find how many lockers are then open?

A small school with only 100 lockers has this ritual on the last day of school:
The student go into the hall and stand by their closed lockers.
At the first blow of a whistle, the students open every locker.
At the second whistle, the students close every second locker(lockers 2,4,6 etc. are slammed shut).
At the third whistle , the students toggle every third locker. To "toggle" means to close if it´s open, and to open if it´s closed. They toggle lockers 3,6,9 99.
At whistle four, they toggle every fourth locker.
At whistle five , they toggle every fifth locker and so on.
At the hundreth whistle , the student standing next to locker 100 (and only that student) toggles his locker. How many lockers are then open.

note : the number of students and lockers in subject to change

corejava x 353
programming x 169
Posted On : 2017-09-30 16:19:15.0
profile Divesh - Divesh


It´s about toggling the lockers on each whistle if we consider initially all lockers are closed. Please try below solution:

import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

public class SchoolRitual {
public static void main(String[] args) {
System.out.println("Please Enter the number of lockers");
Scanner input=new Scanner(;
int num=input.nextInt();
Map<Integer, Boolean> lockers=new HashMap<Integer, Boolean>();
for(int i=1;i<=num;i++){
System.out.println("Please Enter the number of whistle rounds");
int whishtles=input.nextInt();
for(int i=1;i<=whishtles;i++){
for(int j=1;j<=num;j++){
lockers.put(j, !lockers.get(j));
System.out.println("Below are the open lockers after "+whishtles+" rounds of whistles");
int openLockers=0;
for(int i=1;i<=lockers.size();i++){
System.out.println("Total Open Lockers="+openLockers);

Posted On : 2017-10-01 18:01:49
Satisfied : 0 Yes  0 No
profile Garima Gupta - Garima Gupta
Reply This Thread

Post Answer
Please Login First to Post Answer: Login login with facebook -