Check perfect square using addition / subtraction | C ++ program to check if n is perfect square or not ; This function returns true if n is perfect square , else false ; sum is sum of all odd numbers . i is used one by one hold odd numbers ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool isPerfectSquare ( int n ) { for ( int sum = 0 , i = 1 ; sum < n ; i += 2 ) { sum += i ; if ( sum == n ) return true ; } return false ; } int main ( ) { isPerfectSquare ( 35 ) ? cout << " Yes STRNEWLINE " : cout << " No STRNEWLINE " ; isPerfectSquare ( 49 ) ? cout << " Yes STRNEWLINE " : cout << " No STRNEWLINE " ; return 0 ; }