Given a positive integer N, the task is to check whether the given number N is Sunny Number or not.
A number N is a sunny number if N + 1 is a perfect square.
Examples:
Input: N = 8
Output: Yes
Explanation:
Since 9 is a perfect square. therefore 8 is a sunny number.Input: N = 11
Output: No
Explanation:
Since 12 is not a perfect square. therefore 8 is not a sunny number.
Approach: The idea is to check whether (N + 1) is a perfect square or not.
Below is the implementation of the above approach:
C++
// C++ program for the above approach #include "bits/stdc++.h" using namespace std; // Function check whether x is a // perfect square or not bool isPerfectSquare( long double x) { // Find floating point value of // square root of x. long double sr = sqrt (x); // If square root is an integer return ((sr - floor (sr)) == 0); } // Function to check Sunny Number void checkSunnyNumber( int N) { // Check if (N + 1) is a perfect // square or not if (isPerfectSquare(N + 1)) { cout << "Yes\n" ; } // If (N+1) is not a perfect square else { cout << "No\n" ; } } // Driver Code int main() { // Given Number int N = 8; // Function call checkSunnyNumber(N); return 0; } |
Java
// Java program for the above approach import java.util.*; class GFG { // Function check whether x is a // perfect square or not static boolean isPerfectSquare( double x) { // Find floating point value of // square root of x. double sr = Math.sqrt(x); // If square root is an integer return ((sr - Math.floor(sr)) == 0 ); } // Function to check Sunny Number static void checkSunnyNumber( int N) { // Check if (N + 1) is a perfect // square or not if (isPerfectSquare(N + 1 )) { System.out.println( "Yes" ); } // If (N+1) is not a perfect square else { System.out.println( "No" ); } } // Driver code public static void main(String[] args) { // Given Number int N = 8 ; // Function call checkSunnyNumber(N); } } // This code is contributed by offbeat |
Python3
# Python3 program for the above approach from math import * # Function check whether x is a # perfect square or not def isPerfectSquare(x): # Find floating point value of # square root of x. sr = sqrt(x) # If square root is an integer return ((sr - floor(sr)) = = 0 ) # Function to check Sunny Number def checkSunnyNumber(N): # Check if (N + 1) is a perfect # square or not if (isPerfectSquare(N + 1 )): print ( "Yes" ) # If (N+1) is not a perfect square else : print ( "No" ) # Driver Code if __name__ = = '__main__' : # Given Number N = 8 # Function call checkSunnyNumber(N) # This code is contributed by Bhupendra_Singh |
C#
// C# program for the above approach using System; class GFG { // Function check whether x is // a perfect square or not static bool isPerfectSquare( double x) { // Find floating point value of // square root of x. double sr = Math.Sqrt(x); // If square root is an integer return ((sr - Math.Floor(sr)) == 0); } // Function to check sunny number static void checkSunnyNumber( int N) { // Check if (N + 1) is a perfect // square or not if (isPerfectSquare(N + 1)) { Console.WriteLine( "Yes" ); } // If (N+1) is not a perfect square else { Console.WriteLine( "No" ); } } // Driver code public static void Main(String[] args) { // Given Number int N = 8; // Function call checkSunnyNumber(N); } } // This code is contributed by Rohit_ranjan |
Javascript
<script> // Javascript program for the above approach // Function check whether x is a // perfect square or not function isPerfectSquare(x) { // Find floating point value of // square root of x. var sr = Math.sqrt(x); // If square root is an integer return ((sr - Math.floor(sr)) == 0); } // Function to check Sunny Number function checkSunnyNumber(N) { // Check if (N + 1) is a perfect // square or not if (isPerfectSquare(N + 1)) { document.write( "Yes" ); } // If (N+1) is not a perfect square else { document.write( "No" ); } } // Driver Code // Given Number var N = 8; // Function call checkSunnyNumber(N); // This code is contributed by noob2000. </script> |
Yes
Time Complexity: O(1)
Auxiliary Space: O(1)
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!