📜  加K后检查数字是否可以做平方

📅  最后修改于: 2021-04-23 20:04:00             🧑  作者: Mango

给定两个数字NK ,任务是检查在将给定数字NK之后是否可以使它成为一个完美的平方。
例子:

方法:这个想法是计算N + K的值,并检查它是否是一个完美的平方。为了检查数字是否是一个完美的平方,请参阅本文。
下面是上述方法的实现:

C++
// C++ program to check whether the number
// can be made perfect square after adding K
 
#include 
using namespace std;
 
// Function to check whether the number
// can be made perfect square after adding K
void isPerfectSquare(long long int x)
{
 
    // Computing the square root of
    // the number
    long double sr = round(sqrt(x));
 
    // Print Yes if the number
    // is a perfect square
    if (sr * sr == x)
        cout << "Yes";
    else
        cout << "No";
}
 
// Driver code
int main()
{
    int n = 7, k = 2;
    isPerfectSquare(n + k);
 
    return 0;
}


Java
// Java program to check whether the number
// can be made perfect square after adding K
import java.util.*;
 
class GFG
{
     
    // Function to check whether the number
    // can be made perfect square after adding K
    static void isPerfectSquare(int x)
    {
     
        // Computing the square root of
        // the number
        int sr = (int)Math.sqrt(x);
     
        // Print Yes if the number
        // is a perfect square
        if (sr * sr == x)
            System.out.println("Yes");
        else
            System.out.println("No");
    }
     
    // Driver code
    public static void main(String args[])
    {
        int n = 7, k = 2;
        isPerfectSquare(n + k);
     
    }
}
 
// This code is contributed by Yash_R


Python3
# Python3 program to check whether the number
# can be made perfect square after adding K
from math import sqrt
 
# Function to check whether the number
# can be made perfect square after adding K
def isPerfectSquare(x) :
 
    # Computing the square root of
    # the number
    sr = int(sqrt(x));
 
    # Print Yes if the number
    # is a perfect square
    if (sr * sr == x) :
        print("Yes");
    else :
        print("No");
 
# Driver code
if __name__ == "__main__" :
 
    n = 7; k = 2;
    isPerfectSquare(n + k);
 
# This code is contributed by Yash_R


C#
// C# program to check whether the number
// can be made perfect square after adding K
using System;
 
class GFG
{
     
    // Function to check whether the number
    // can be made perfect square after adding K
    static void isPerfectSquare(int x)
    {
     
        // Computing the square root of
        // the number
        int sr = (int)Math.Sqrt(x);
     
        // Print Yes if the number
        // is a perfect square
        if (sr * sr == x)
            Console.WriteLine("Yes");
        else
            Console.WriteLine("No");
    }
     
    // Driver code
    public static void Main(String []args)
    {
        int n = 7;
        int k = 2;
        isPerfectSquare(n + k);   
    }
}
 
// This code is contributed by Yash_R


Javascript


输出:
Yes

注意:类似地,可以通过在上述函数中用’-‘符号代替(+-K)来检查(N-K)是否可以是一个完美的正方形。