Check if the door is open or closed | java implementation of doors open or closed ; Function to check whether ' n ' has even number of factors or not ; if ' n ' is a perfect square it has odd number of factors ; else ' n ' has even number of factors ; Function to find and print status of each door ; If even number of factors final status is closed ; else odd number of factors final status is open ; Driver program
import java . io . * ; class GFG { static boolean hasEvenNumberOfFactors ( int n ) { double root_n = Math . sqrt ( n ) ; if ( ( root_n * root_n ) == n ) return false ; return true ; } static void printStatusOfDoors ( int n ) { for ( int i = 1 ; i <= n ; i ++ ) { if ( hasEvenNumberOfFactors ( i ) ) System . out . print ( " closed " + " ▁ " ) ; else System . out . print ( " open " + " ▁ " ) ; } } public static void main ( String [ ] args ) { int n = 5 ; printStatusOfDoors ( n ) ; } }