Explain everyline of this code.
1. In this Java program. Explain everyline of this code.
Source Code:
import java.util.*;
public class Main
{
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
System.out.print("Enter the number of lockers: ");
int noOfLockers = keyboard.nextInt();
boolean[] isLockerOpen = new boolean[noOfLockers + 1];
for (int i = 1; i <= noOfLockers; i++) {
for (int j = i; j <= noOfLockers; j += i) {
isLockerOpen[j] = !isLockerOpen[j];
}
}
System.out.println("\n\nThe number of lockers: " + noOfLockers);
System.out.println("The locker numbers of the lockers that are open: ");
for (int i = 1; i <= noOfLockers; i++) {
if (isLockerOpen[i] == true)
System.out.print(i + " ");
}
}
}
Step by step
Solved in 3 steps with 2 images