Find the nearest odd and even perfect squares of odd and even array elements respectively | C # program for the above approach ; Function to find the nearest even and odd perfect squares for even and odd array elements ; Traverse the array ; Calculate square root of current array element ; If both are of same parity ; Otherwise ; Driver Code
using System ; class GFG { static void nearestPerfectSquare ( int [ ] arr , int N ) { for ( int i = 0 ; i < N ; i ++ ) { int sr = ( int ) Math . Sqrt ( arr [ i ] ) ; if ( ( sr & 1 ) == ( arr [ i ] & 1 ) ) Console . Write ( ( sr * sr ) + " ▁ " ) ; else { sr ++ ; Console . Write ( ( sr * sr ) + " ▁ " ) ; } } } static public void Main ( ) { int [ ] arr = { 6 , 3 , 2 , 15 } ; int N = arr . Length ; nearestPerfectSquare ( arr , N ) ; } }