
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Check if a number is perfect square without finding square root in C++
Suppose a number is given, we have to check whether the number is a perfect square or not. We will not use the square root operation to check it. Suppose a number 1024 is there, this is a perfect square, but 1000 is not a perfect square. The logic is simple, we have to follow this algorithm to get the result.
Algorithm
isPerfectSquare(n) −
input − The number n
output − true, if the number is a perfect square, otherwise, false
begin for i := 1, i2 ≤ n, increase i by 1: if n is divisible by i, and n / i = i, then return true done return false end
Example
#include <iostream> using namespace std; bool isPerfectSquare(int number) { for (int i = 1; i * i <= number; i++) { if ((number % i == 0) && (number / i == i)) { return true; } } return false; } int main() { int n = 1024; if(isPerfectSquare(n)){ cout << n << " is perfect square number"; } else { cout << n << " is not a perfect square number"; } }
Output
1024 is perfect square number
- Related Articles
- Finding square root of a number without using Math.sqrt() in JavaScript
- Check if given number is perfect square in Python
- Finding square root of a number without using library functions - JavaScript
- 8086 program to find the square root of a perfect square root number
- Finding square root of a non-negative number without using Math.sqrt() JavaScript
- Check if a number in a list is perfect square using Python
- Program to check number is perfect square or not without sqrt function in Python
- Check for perfect square without using Math libraries - JavaScript
- How to find perfect square root?
- Check Perfect Square or Not
- Check for perfect square in JavaScript
- What is a number which is a square and square root itself?
- Find the smallest number by which 396 must be divided to obtain a perfect square. Also find the square root of the perfect square so obtained.
- Find the smallest number by which 5103 can be divided to get a perfect square. Also find the square root of the perfect square so obtained.
- Write the smallest number that must be subtracted from 9400 to obtain a perfect square.Find this perfect square and its square root.

Advertisements