Find the Next perfect square greater than a given number
Given a number N, the task is to find the next perfect square greater than N.
Examples:
Input: N = 6 Output: 9 9 is a greater number than 6 and is also a perfect square Input: N = 9 Output: 16
Approach:
- Find the square root of given N.
- Calculate its floor value using floor function in C++.
- Then add 1 to it.
- Print square of that number.
Below is the implementation of above approach:
C++
// C++ implementation of above approach #include <iostream> #include<cmath> using namespace std; // Function to find the next perfect square int nextPerfectSquare( int N) { int nextN = floor ( sqrt (N)) + 1; return nextN * nextN; } // Driver Code int main() { int n = 35; cout << nextPerfectSquare(n); return 0; } |
chevron_right
filter_none
Java
// Java implementation of above approach import java.util.*; import java.lang.*; import java.io.*; class GFG { // Function to find the // next perfect square static int nextPerfectSquare( int N) { int nextN = ( int )Math.floor(Math.sqrt(N)) + 1 ; return nextN * nextN; } // Driver Code public static void main(String args[]) { int n = 35 ; System.out.println (nextPerfectSquare(n)); } } // This code is contributed by Subhadeep |
chevron_right
filter_none
Python3
# Python3 implementation of above approach import math #Function to find the next perfect square def nextPerfectSquare(N): nextN = math.floor(math.sqrt(N)) + 1 return nextN * nextN if __name__ = = '__main__' : N = 35 print (nextPerfectSquare(N)) # this code is contributed by Surendra_Gangwar |
chevron_right
filter_none
C#
// C# implementation of above approach using System; class GFG { // Function to find the // next perfect square static int nextPerfectSquare( int N) { int nextN = ( int )Math.Floor(Math.Sqrt(N)) + 1; return nextN * nextN; } // Driver Code public static void Main() { int n = 35; Console.WriteLine(nextPerfectSquare(n)); } } // This code is contributed // by Shashank |
chevron_right
filter_none
PHP
<?php // PHP implementation // of above approach // Function to find the // next perfect square function nextPerfectSquare( $N ) { $nextN = floor (sqrt( $N )) + 1; return $nextN * $nextN ; } // Driver Code $n = 35; echo nextPerfectSquare( $n ); // This code is contributed by mits ?> |
chevron_right
filter_none
Output:
36
Recommended Posts:
- Find minimum number to be divided to make a number a perfect square
- Check if a number is perfect square without finding square root
- Perfect cube greater than a given number
- Check if given number is perfect square
- Largest number that is not a perfect square
- Number of times the largest perfect square number can be subtracted from N
- Largest perfect square number in an Array
- Largest factor of a given number which is a perfect square
- Count numbers upto N which are both perfect square and perfect cube
- Check whether the number can be made perfect square after adding 1
- Largest Divisor of a Number not divisible by a perfect square
- Minimum digits to remove to make a number Perfect Square
- Check whether the number formed by concatenating two numbers is a perfect square or not
- Perfect Square String
- Closest perfect square and its distance
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.