Largest number that is not a perfect square | CPP program to find the largest non perfect square number among n numbers ; takes the sqrt of the number ; checks if it is a perfect square number ; function to find the largest non perfect square number ; stores the maximum of all non perfect square numbers ; traverse for all elements in the array ; store the maximum if not a perfect square ; driver code to check the above functions ; function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool check ( int n ) { int d = sqrt ( n ) ; if ( d * d == n ) return true ; return false ; } int largestNonPerfectSquareNumber ( int a [ ] , int n ) { int maxi = -1 ; for ( int i = 0 ; i < n ; i ++ ) { if ( ! check ( a [ i ] ) ) maxi = max ( a [ i ] , maxi ) ; } return maxi ; } int main ( ) { int a [ ] = { 16 , 20 , 25 , 2 , 3 , 10 } ; int n = sizeof ( a ) / sizeof ( a [ 0 ] ) ; cout << largestNonPerfectSquareNumber ( a , n ) ; return 0 ; }