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.