# Check whether the number can be made perfect square after adding K

Given two numbers **N** and **K**, the task is to check whether the given number **N** can be made a perfect square after adding **K** to it.**Examples:**

Input:N = 7, K = 2Output:YesExplanation:

7 + 2 = 9 which is a perfect square.Input:N = 5, K = 3Output:NoExplanation:

5 + 3 = 8 which is not a perfect square.

**Approach:** The idea is to compute the value of **N + K** and check if it is a perfect square or not. In order to check if a number is a perfect square or not, refer to this article.

Below is the implementation of the above approach:

## C++

`// C++ program to check whether the number` `// can be made perfect square after adding K` `#include <bits/stdc++.h>` `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

`<script>` `// Javascript program to check whether the number` `// can be made perfect square after adding K` `// Function to check whether the number` `// can be made perfect square after adding K` `function` `isPerfectSquare(x)` `{` ` ` `// Computing the square root of` ` ` `// the number` ` ` `var` `sr = Math.round(Math.sqrt(x));` ` ` `// Print Yes if the number` ` ` `// is a perfect square` ` ` `if` `(sr * sr == x)` ` ` `document.write(` `"Yes"` `);` ` ` `else` ` ` `document.write(` `"No"` `);` `}` `// Driver code` `var` `n = 7, k = 2;` `isPerfectSquare(n + k);` `</script>` |

**Output:**

Yes

Note: Similar, it can be checked whether (N – K) can be a perfect square or not, by replacing ‘+’ with ‘-‘ sign in the above function.

Attention reader! Don’t stop learning now. Get hold of all the important mathematical concepts for competitive programming with the **Essential Maths for CP Course** at a student-friendly price. To complete your preparation from learning a language to DS Algo and many more, please refer **Complete Interview Preparation Course****.**