Largest perfect square number in an Array | CPP program to find the largest perfect square number among n numbers ; Function to check if a number is perfect square number or not ; takes the sqrt of the number ; checks if it is a perfect square number ; Function to find the largest perfect square number in the array ; stores the maximum of all perfect square numbers ; Traverse all elements in the array ; store the maximum if current element is a perfect square ; Driver Code
#include <iostream> NEW_LINE #include <math.h> NEW_LINE using namespace std ; bool checkPerfectSquare ( double n ) { double d = sqrt ( n ) ; if ( d * d == n ) return true ; return false ; } int largestPerfectSquareNumber ( int a [ ] , double n ) { int maxi = -1 ; for ( int i = 0 ; i < n ; i ++ ) { if ( checkPerfectSquare ( a [ i ] ) ) maxi = max ( a [ i ] , maxi ) ; } return maxi ; } int main ( ) { int a [ ] = { 16 , 20 , 25 , 2 , 3 , 10 } ; double n = sizeof ( a ) / sizeof ( a [ 0 ] ) ; cout << largestPerfectSquareNumber ( a , n ) ; return 0 ; }