Program to Check Sunny Number

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 Number

Example2:

Input:

Given number = 122

Output:

The given number{ 122 } is not a Sunny Number

Program 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:

  1. Import the math module using the import keyword.
  2. Give the number as static input and store it in a variable.
  3. Add ‘1’ to the above-given number and store it in another variable say “incremented number”.
  4. Calculate the Square root of the above-obtained number using math.sqrt() built-in function and store it in another variable.
  5. Multiply the above obtained Square root value with itself and store it in another variable say “square_number”.
  6. Check if the value of “square_number ” is equal to the “incremented number” using if conditional statement.
  7. If the statement is True,Print “The given number is Strong Number”
  8. Else if the statement is False, print “The given number is Not a Strong Number”.
  9. 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 Number

Method #2: Using math.sqrt() function (User input)

Approach:

  1. Import the math module using the import keyword.
  2. Give the number as user input using the int(input()) function and store it in a variable.
  3. Add ‘1’ to the above-given number and store it in another variable say “incremented number”.
  4. Calculate the Square root of the above-obtained number using math.sqrt() built-in function and store it in another variable.
  5. Multiply the above obtained Square root value with itself and store it in another variable say “square_number”.
  6. Check if the value of “square_number ” is equal to the “incremented number” using if conditional statement.
  7. If the statement is True, Print “The given number is Strong Number”
  8. Else if the statement is False, print “The given number is Not a Strong Number”.
  9. 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 Number

Explore more instances related to python concepts from Python Programming Examples Guide and get promoted from beginner to professional programmer level in Python Programming Language.