Python Program to Check Sunny Number
In the previous article, we have discussed Python Program to Find the Missing Term of any Arithmetic Progression
Sunny Number:
A number is said to be a sunny number if the number next to it is a perfect square. In other words, if N+1 is a perfect square, then N is a sunny number.
Example :
Let Given number(N) = 3
Then N+1= 3+1 = 4 # which is a perfect square.
Therefore ,the Given number “3” is a Sunny Number.
some of the examples are 3,8 ,15, 24, 35 ,48 ,63, 80 ,99, 120 etc.
Given a number, the task is to check whether the given number is a Sunny Number or not.
Examples:
Example1:
Input:
Given number = 3
Output:
The given number{ 3 } is Sunny NumberExample2:
Input:
Given number = 122
Output:
The given number{ 122 } is not a Sunny NumberProgram to Check Sunny Number
Below are the ways to check whether the given number is a Sunny Number or not.
Method #1: Using math.sqrt() function (Static input)
Approach:
- Import the math module using the import keyword.
- Give the number as static input and store it in a variable.
- Add ‘1’ to the above-given number and store it in another variable say “incremented number”.
- Calculate the Square root of the above-obtained number using math.sqrt() built-in function and store it in another variable.
- Multiply the above obtained Square root value with itself and store it in another variable say “square_number”.
- Check if the value of “square_number ” is equal to the “incremented number” using if conditional statement.
- If the statement is True,Print “The given number is Strong Number”
- Else if the statement is False, print “The given number is Not a Strong Number”.
- The Exit of the program.
Below is the implementation:
# Import the math module using import keyword.
import math
# Give the number as static input and store it in a variable.
numb = 3
# Add '1' to the above given number and store it in another variable say
# "incremented number".
incremnt_num = numb + 1
# Calculate the Square root of the above obtained number using math.sqrt()
# built-in function and store it in another variable.
sqrt_numb = math.sqrt(incremnt_num)
# Multiply the above obtained Square root value with itself and
# store it in another variable say "square_number".
square_number = sqrt_numb * sqrt_numb
# Check if the value of "square_number " is equal the "incremented number"
# using if conditional statement.
if(square_number == incremnt_num):
# If the statement is True ,Print "The given number is Strong Number"
print("The given number{", numb, "} is Sunny Number")
else:
# Else if the statement is False, print "The given number is Not a Strong Number" .
print("The given number{", numb, "} is not a Sunny Number")
Output:
The given number{ 3 } is Sunny NumberMethod #2: Using math.sqrt() function (User input)
Approach:
- Import the math module using the import keyword.
- Give the number as user input using the int(input()) function and store it in a variable.
- Add ‘1’ to the above-given number and store it in another variable say “incremented number”.
- Calculate the Square root of the above-obtained number using math.sqrt() built-in function and store it in another variable.
- Multiply the above obtained Square root value with itself and store it in another variable say “square_number”.
- Check if the value of “square_number ” is equal to the “incremented number” using if conditional statement.
- If the statement is True, Print “The given number is Strong Number”
- Else if the statement is False, print “The given number is Not a Strong Number”.
- The Exit of the program.
Below is the implementation:
# Import the math module using import keyword.
import math
# Give the number as user input using int(input()) and store it in a variable.
numb = int(input("Enter some random number = "))
# Add '1' to the above given number and store it in another variable say
# "incremented number".
incremnt_num = numb + 1
# Calculate the Square root of the above obtained number using math.sqrt()
# built-in function and store it in another variable.
sqrt_numb = math.sqrt(incremnt_num)
# Multiply the above obtained Square root value with itself and
# store it in another variable say "square_number".
square_number = sqrt_numb * sqrt_numb
# Check if the value of "square_number " is equal the "incremented number"
# using if conditional statement.
if(square_number == incremnt_num):
# If the statement is True ,Print "The given number is Strong Number"
print("The given number{", numb, "} is Sunny Number")
else:
# Else if the statement is False, print "The given number is Not a Strong Number" .
print("The given number{", numb, "} is not a Sunny Number")
Output:
Enter some random number = 30
The given number{ 30 } is not a Sunny NumberExplore more instances related to python concepts from Python Programming Examples Guide and get promoted from beginner to professional programmer level in Python Programming Language.
- Python Program to Solve Triangular Matchstick Number
- Python Program to Check Pronic Number or Not
- Python Program to Check Automorphic Number or Not
- Python Program to Check Buzz Number or Not









