C++ program to check if number is a square number.
// ? 2017 TheFlyingKeyboard and released under MIT License // theflyingkeyboard.net #include <iostream> using namespace std; bool isSquare(unsigned long long n); int main() { unsigned long long num; cout << "Enter a number: "; cin >> num; if (isSquare(num)) { cout << "Number is square" << endl; } else { cout << "Number is not square" << endl; } int a; cin >> a; return 0; } bool isSquare(unsigned long long n) { for (unsigned long long i = 0; i < n / 2 + 2; i++) { if (i * i == n) { return true; } } return false; }
C++ Check If Number Is Square Number
Hey, surely you should break once i*i exceeds n in the function isSquare? As this would give you O(root N) runtime, whereas this currently is O(N)
for (unsigned long long i = 0; i * i <= n ; i++)