- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

# Check Perfect Square or Not

A number is said to be a perfect square number if the square root of that number is an integer. In other words, when a square root is a whole number, then the number is called a perfect square number.

We can check perfect square by finding the square root of that number and match with i again and again to get exact square root. When the square root crosses the value, it is not a perfect square number.

But here to reduce effort, we have not checked the square root again and again. As we know that the square root of a perfect square number is an integer, then we can increase the square root by one, and check for a perfect square match.

## Input and Output

Input: A number to check: 1032 Output: 1032 is not a perfect square number.

## Algorithm

isPerfectSquare(num)

**Input: **The number.

**Output:** True if a number is the perfect square number, and also print the square root.

Begin if num < 0, then exit sqRoot := 1 sq := sqRoot^2 while sq <= num, do if sq = num, then return sqRoot sqRoot := sqRoot + 1 sq := sqRoot^2 done otherwise return error End

## Example

#include<iostream> using namespace std; int isPerfectSquare(int num) { if(num < 0) return -1; //a -ve number is not a valid square term int sqRoot = 1, sq; while((sq =(sqRoot*sqRoot)) <= num) { //when square of square root is not crossed the number if(sq == num) return sqRoot; sqRoot++; //as square root of a perfect square is always integer } return -1; } int main() { int num, res; cout << "Enter a number to check whether it is perfect square or not: "; cin >> num; if((res = isPerfectSquare(num)) != -1) cout << num << " is a perfect square number, square root: " << res; else cout << num << " is not a perfect square number."; }

## Output

Enter a number to check whether it is perfect square or not: 1032 1032 is not a perfect square number.

- Related Questions & Answers
- Program to check number is perfect square or not without sqrt function in Python
- Check for perfect square in JavaScript
- Check whether the number formed by concatenating two numbers is a perfect square or not in Python
- Check for perfect square without using Math libraries - JavaScript
- Check if given number is perfect square in Python
- Check given matrix is magic square or not in C++
- Check if a number is perfect square without finding square root in C++
- Check whether the number can be made perfect square after adding 1 in Python
- Check if product of array containing prime numbers is a perfect square in Python
- Count numbers upto N which are both perfect square and perfect cube in C++
- 8086 program to find the square root of a perfect square root number
- C program to find if the given number is perfect number or not
- Print n numbers such that their sum is a perfect square
- Integers have sum of squared divisors as perfect square in JavaScript
- Count the nodes whose weight is a perfect square in C++

Advertisements