38.6k views
0 votes
A school has 100 lockers and 100 students. All lockers are closed on the first day of school. As the students enter, the first student, denoted S1, opens every locker. Then the second student, S2, begins with the second locker, denoted L2, and closes every other locker (i.e. every even numbered locker). Student S3 begins with the third locker and changes every third locker (closes it if it was open, and opens it if it was closed). Student S4 begins with locker L4 and changes every fourth locker. Student

S5 starts with L5 and changes every fifth locker, and so on, until student S100 changes L100.
After all the students have passed through the building and changed the lockers, which lockers areopen? Write a program to find your answer.
(Hint: Use an array of 100 boolean elements. each of which indicates whether a locker is open (true) or closed (false). Initially, all lockers are closed.)

User David Bick
by
4.2k points

1 Answer

5 votes

Answer:

Following are the code to this question:

public class Locker //defining a class Locker

{

public static void main(String[] ax)//defining main method

{

boolean[] locker = new boolean[101]; //defining boolean array locker that's size is 100.

int j,s1,k1,x;

x=locker.length;

for (j=1;j<x; j++)//defining loop to assigns value false in locker array

{

locker[j] = false;// assign value false

}

for (j=1;j<x; j++)//defining loop loop to assign value true in array

{

locker[j] = true;//assign value true

}

for(s1=2; s1<x; s1++)//defining loop to count locker from 2nd place

{

for(k1=s1; k1<x; k1=k1+s1)// defining inner loop to assign value

{

if(locker[k1]==false) //defining if block for check false value

{

locker[k1] = true;// if condition is true it assign value true

}

else //defining else block

{

locker[k1] = false;//assign value false in locker array

}

}

}

for(s1=1; s1<x; s1++)//defining loop to print locker value

{

if (locker[s1] == true) //defining if block that check Locker array value is equal to true

{

System.out.println("Locker " + s1 + " Open");//print Locker value with message

}

}

}

}

Output:

Locker 1 Open

Locker 4 Open

Locker 9 Open

Locker 16 Open

Locker 25 Open

Locker 36 Open

Locker 49 Open

Locker 64 Open

Locker 81 Open

Locker 100 Open

Step-by-step explanation:

In the above code, a class "Locker" is declared, inside the class main method is declared, in which a boolean array "locker" is declared that's size is 100, in the next line fouth integer variable " j,s1,k1, and x" is declared, in which variable x is used to hold the boolean array length value, and other variable used in the loop.

  • In the next step, two for loop is declared, in the first loop assign value "false" in locker array and in seconde loop it assigns the value "true".
  • In the next step, multiple for loop it uses if block uses to check locker value equal to false then assign value true otherwise assign value false.
  • At the last another loop is declared, that uses if block to check locker value is equal to true and print its value with the message.
User Taylorsuk
by
3.5k points