Sunny Number - GeeksforGeeks

07 Jun, 2021
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++
Java
Python3
C#
Javascript
// C++ program for the above approach #include "bits/stdc++.h"using namespace std; // Function check whether x is a// perfect square or notbool 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 Numbervoid 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 Codeint main(){     // Given Number    int N = 8;     // Function call    checkSunnyNumber(N);    return 0;}
// Java program for the above approachimport java.util.*; class GFG {     // Function check whether x is a// perfect square or notstatic 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 Numberstatic 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 codepublic static void main(String[] args){         // Given Number    int N = 8;     // Function call    checkSunnyNumber(N);}} // This code is contributed by offbeat
# Python3 program for the above approachfrom math import * # Function check whether x is a# perfect square or notdef 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 Numberdef 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 Codeif __name__ == '__main__':         # Given Number    N = 8     # Function call    checkSunnyNumber(N) # This code is contributed by Bhupendra_Singh
// C# program for the above approachusing System; class GFG {     // Function check whether x is// a perfect square or notstatic 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 numberstatic 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 codepublic static void Main(String[] args){         // Given Number    int N = 8;     // Function call    checkSunnyNumber(N);}} // This code is contributed by Rohit_ranjan
<script>// Javascript program for the above approach // Function check whether x is a// perfect square or notfunction 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 Numberfunction 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 Numbervar N = 8; // Function callcheckSunnyNumber(N); // This code is contributed by noob2000.</script>
Yes
 
Time Complexity: O(1)
Auxiliary Space: O(1)
offbeat
Rohit_ranjan
bgangwar59
aditya7409
noob2000
maths-perfect-square
Mathematical
School Programming
Write From Home
Mathematical
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Program to find GCD or HCF of two numbers
Median in a stream of integers (running integers)
Program to multiply two matrices
Merge two sorted arrays
Generate all permutation of a set in Python
Python Dictionary
Arrays in C/C++
Reverse a string in Java
Inheritance in C++
Types of Operating Systems