Como saber se um número é quadrado perfeito

Dado um número, verifique se é um quadrado perfeito ou não. 

Exemplos : 

Input : 2500 Output : Yes Explanation: 2500 is a perfect square. 50 * 50 = 2500 Input : 2555 Output : No

Abordagem:

  1. Tire a raiz quadrada do número.
  2. Multiplique a raiz quadrada duas vezes
  3. Use o operador booleano igual para verificar se o produto da raiz quadrada é igual ao número fornecido.
// CPP program to find if x is a // perfect square. #include using namespace std; bool isPerfectSquare[long double x] { // Find floating point value of // square root of x. if [x >= 0] { long long sr = sqrt[x]; // if product of square root //is equal, then // return T/F return [sr * sr == x]; } // else return false if n= 0]: sr = math.sqrt[x] # sqrt function returns floating value so we have to convert it into integer #return boolean T/F return [[sr*sr] == float[x]] return false # Driver code x = 2502 if [isPerfectSquare[x]]: print["Yes"] else: print["No"] # This code is contributed # by Anant Agarwal. // C# program to find if x is a // perfect square. using System; class GFG { static bool isPerfectSquare[double x] { // Find floating point value of // square root of x. if [x >= 0] { double sr = Math.Sqrt[x]; // if product of square root // is equal, then // return T/F return [sr * sr == x]; } // else return false if n

Bài mới nhất

Chủ Đề