Sunny Number

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 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
// 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 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 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#
// 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 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 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> |
Output:
Yes
Time Complexity: O(1)
Auxiliary Space: O(1)
Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 zambiatek!
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 zambiatek!



