Check if a number is perfect square without finding square root
We need to check whether a number is a perfect square or not without finding its square root.
Examples:
Input : n = 36
Output : YesInput : n = 12
Output : No
We have discussed a method to check if a number is perfect square.
The idea is to run a loop from i = 1 to floor(sqrt(n)) then check if squaring it makes n.
C++
// C++ program to check if a number is perfect // square without finding square root #include <bits/stdc++.h> using namespace std; bool isPerfectSquare(int n) { for (int i = 1; i * i <= n; i++) { // If (i * i = n) if ((n % i == 0) && (n / i == i)) { return true; } } return false; } // Driver code int main() { long long int n = 36; if (isPerfectSquare(n)) cout << "Yes"; else cout << "No"; return 0; } |
chevron_right
filter_none
Java
// Java program to check if a number is perfect // square without finding the square root public class GfG { static boolean isPerfectSquare(int n) { for (int i = 1; i * i <= n; i++) { // If (i * i = n) if ((n % i == 0) && (n / i == i)) { return true; } } return false; } public static void main(String[] args) { int n = 36; if (isPerfectSquare(n)) System.out.println("Yes"); else System.out.println("No"); } } // This code is contributed by Rituraj Jain |
chevron_right
filter_none
Python3
# Python3 program to check if a number is # perfect square without finding square root # from math import sqrt function def isPerfectSquare(n) : i = 1 while(i * i<= n): # If (i * i = n) if ((n % i == 0) and (n / i == i)): return True i = i + 1 return False # Driver code if __name__ == "__main__" : n = 36 if (isPerfectSquare(n)): print("Yes, it is a perfect square.") else : print("No, it is not a perfect square.") # This code is contributed by Ryuga |
chevron_right
filter_none
C#
// C# program to check if a number is perfect // square without finding the square root using System; public class GfG { static bool isPerfectSquare(int n) { for (int i = 1; i * i <= n; i++) { // If (i * i = n) if ((n % i == 0) && (n / i == i)) { return true; } } return false; } public static void Main() { int n = 36; if (isPerfectSquare(n)) Console.WriteLine("Yes"); else Console.WriteLine("No"); } } /*This code is contributed by Rajput-Ji*/ |
chevron_right
filter_none
PHP
<?php // PHP program to check if a number is perfect // square without finding square root function isPerfectSquare($n) { for ($i = 1; $i * $i <= $n; $i++) { // If (i * i = n) if (($n % $i == 0) && ($n / $i == $i)) { return true; } } return false; } // Driver code $n = 36; if (isPerfectSquare($n)) echo "Yes"; else echo "No"; // This code is contributed // by Akanksha Rai |
chevron_right
filter_none
Output:
Yes
Recommended Posts:
- Check if given number is perfect square
- Check whether the number can be made perfect square after adding 1
- Check perfect square using addition/subtraction
- Check if product of array containing prime numbers is a perfect square
- Euler's criterion (Check if square root under modulo p exists)
- Square root of a number using log
- Largest number that is not a perfect square
- Largest factor of a given number which is a perfect square
- Largest perfect square number in an Array
- Find the Next perfect square greater than a given number
- Square root of a number without using sqrt() function
- Largest Divisor of a Number not divisible by a perfect square
- Minimum digits to remove to make a number Perfect Square
- Find minimum number to be divided to make a number a perfect square
- Number of times the largest perfect square number can be subtracted from N
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



