Untitled

 avatar
unknown
plain_text
6 months ago
811 B
4
Indexable
package lockerpuzzle;

public class Lockerpuzzle {

    public static void main(String[] args) {
       
        boolean[] lockers = new boolean[100];
        
    // Simulate each student toggling the lockers.
    
    for (int student = 1; student <= 100; student++){
        
        for (int locker = student - 1; locker < 100; locker += student)
            
        { 
            lockers[locker] = !lockers[locker]; // Toggle the locker (open if closed, close if open).
    
    
        }
    
        
    }
    
    // Print the locker that are open.
    
    for (int i = 0; i < 100; i++) {
        
        if (lockers[i]) {
            
            System.out.println("Locker " +  (i + 1) + " is open");
            
                  }
             }
        }
 }
Editor is loading...
Leave a Comment