Data types and variables
Operators
Modules and Packages
Conversion Programs
More Code Examples
Cheat Sheet

Python Code Example: Sunny Number

This Python code checks if a given number is a sunny number. A number đť‘› is considered a sunny number if đť‘›+1 is a perfect square. The perfectSquare function determines if a number is a perfect square by checking if its square root is an integer. The isSunnyNumber function uses this to verify if đť‘›+1 is a perfect square and prints the result. The user is prompted to enter a number, and the program then outputs whether the number is a sunny number or not.

Code Example

import math

# Function to check if a number is a perfect square
def perfectSquare(n): 
    # Calculate the square root of the number
    square_root = math.sqrt(n)
    # Check if the square root is an integer (perfect square)
    return((square_root - math.floor(square_root)) == 0)

# Function to check if a number is a sunny number
def isSunnyNumber(n):
    # Check if n+1 is a perfect square
    if (perfectSquare(n + 1)):  
        # If true, print that the number is a sunny number
        print(str(n) + " is a sunny number!")
    else:
        # If false, print that the number is not a sunny number
        print(str(n) +  " is not a sunny number!")

# Prompt the user to enter a number
print("Enter a number to check for sunny number:") 
# Read the input and convert it to an integer
sunnyNumber = int(input()) 

# Check if the entered number is a sunny number
isSunnyNumber(sunnyNumber)

Output

Enter a number to check for sunny number:
48
48 is a sunny number!

Code Explanation

Importing the Math Module

import math
  1. Importing Math Module: import math imports the Python math module, which provides mathematical functions, including math.sqrt for calculating the square root of a number and math.floor for getting the largest integer less than or equal to a given number.

Function to Check for Perfect Square

def perfectSquare(n): 
    square_root = math.sqrt(n)
    return((square_root - math.floor(square_root)) == 0)
  1. Function Definition: def perfectSquare(n): defines a function named perfectSquare that takes an integer n as its argument.
  2. Calculate Square Root: square_root = math.sqrt(n) computes the square root of n and assigns it to the variable square_root.
  3. Check if Perfect Square:
    • math.floor(square_root) computes the largest integer less than or equal to square_root.
    • square_root - math.floor(square_root) calculates the difference between square_root and its floored value. If n is a perfect square, this difference will be 0 because square_root will be an integer.
    • return((square_root - math.floor(square_root)) == 0) returns True if the difference is 0 (indicating n is a perfect square) and False otherwise.

Function to Check for Sunny Number

def isSunnyNumber(n):
    if (perfectSquare(n + 1)):  
        print(str(n) + " is a sunny number!")
    else:
        print(str(n) +  " is not a sunny number!")
  1. Function Definition: def isSunnyNumber(n): defines a function named isSunnyNumber that takes an integer n as its argument.
  2. Check if n+1 is a Perfect Square:
    • if (perfectSquare(n + 1)): calls the perfectSquare function with n + 1 as the argument. If the result is True (indicating n + 1 is a perfect square), it executes the following block.
    • print(str(n) + " is a sunny number!") prints that n is a sunny number.
    • else: executes the following block if perfectSquare(n + 1) returns False.
    • print(str(n) + " is not a sunny number!") prints that n is not a sunny number.

Input and Function Call

print("Enter a number to check for sunny number:") 
sunnyNumber = int(input())
  1. Prompt for Input: print("Enter a number to check for sunny number:") prints a message asking the user to enter a number.
  2. Read Input: sunnyNumber = int(input()) reads the user input, converts it to an integer, and assigns it to the variable sunnyNumber.

Check and Print Result

isSunnyNumber(sunnyNumber)
  1. Check Sunny Number: isSunnyNumber(sunnyNumber) calls the isSunnyNumber function with sunnyNumber as the argument to check if it is a sunny number and print the result.