Check if the door is open or closed | C ++ 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
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool hasEvenNumberOfFactors ( int n ) { int root_n = sqrt ( n ) ; if ( ( root_n * root_n ) == n ) return false ; return true ; } void printStatusOfDoors ( int n ) { for ( int i = 1 ; i <= n ; i ++ ) { if ( hasEvenNumberOfFactors ( i ) ) cout << " closed " << " ▁ " ; else cout << " open " << " ▁ " ; } } int main ( ) { int n = 5 ; printStatusOfDoors ( n ) ; return 0 ; }