In this article, we will learn how to print Neon numbers within a specific range in Python. You will learn what a Neon number is, how to check whether a given number is a Neon number, and how to write a Python code that outputs all the Neon numbers within the user-specified range.
Neon Number:
A Neon Number is a number whose square sum of digits equals the original number.
Ex: 9
Square of 9 =81
Sum of digits of square = 8 +1 =9 ,So it is Neon Number
Examples:
Example1:
Input:
Given upper limit range =1 Given lower limit range =7
Output:
The Neon numbers in the given range 1 and 7 are: 1
Example2:
Input:
Given upper limit range =1 Given lower limit range =23
Output:
Enter lower limit range and upper limit range separate bt spaces = 1 23 The Neon numbers in the given range 1 and 23 are: 1 9
Program to Print Neon numbers in a Range in Python
Below are the ways to print all the Neon numbers in the given range.
Don’t miss the chance of Java programs examples with output pdf free download as it is very essential for all beginners to experienced programmers for cracking the interviews.
Method #1: Using list() and sum() functions (Static Input)
Approach:
- Create a function checkNeonnumb() which accepts the number as an argument and returns true if it is Neon number else returns False.
- Give the lower limit range as static input and store it in a variable.
- Give the upper limit range as static input and store it in another variable.
- Inside the checkNeonnumb() function
- Calculate the square of the given number/argument using the ** operator or multiply the given number by itself and store it in a variable.
- Convert this squared number into a list of digits using list(),int(),map(),str() functions.
- Store this list in a variable.
- Calculate the sum of digits of this list using the sum() function.
- Check if this sum is equal to the given number or not using the If conditional statement.
- If it is true then the given number is a Neon Number so return True
- Else the given number is not a Neon Number so return False.
- Loop from lower limit range to upper limit range using For loop.
- Inside the for loop pass the iterator value to checkNeonnumb() function.
- If it returns true then print the iterator value.
- The Exit of the Program.
Below is the implementation:
# Create a function checkNeonNumb() which accepts the number as an argument and # returns true if it is Neon number else returns False. def checkNeonNumb(numb): # Calculate the square of the given number/argument using the ** operator or multiply # the given number by itself and store it in a variable. squarnumb = numb**2 # Convert this squared number into a list of digits # using list(),int(),map(),str() functions. # Store this list in a variable. numbedigit = list(map(int, str(squarnumb))) # Calculate the sum of digits of this list using the sum() function. sumdigi = sum(numbedigit) # Check if this sum is equal to the given number # or not using the If conditional statement. # If it is true then the given number is a Neon Number so return True if(sumdigi == numb): return True # Else the given number is not a Neon Number so return False. else: return False # Give the lower limit range as static input and store it in a variable. lowlimrange = 1 # Give the upper limit range as static input and store it in another variable. upplimrange = 7 print('The Neon numbers in the given range', lowlimrange, 'and', upplimrange, 'are:') # Loop from lower limit range to upper limit range using For loop. for p in range(lowlimrange, upplimrange+1): # Inside the for loop pass the iterator value to checkNeonnumb() function. if(checkNeonNumb(p)): # If it returns true then print the iterator value. print(p, end=' ')
Output:
The Neon numbers in the given range 1 and 7 are: 1
Method #2: Using list() and sum() functions (User Input)
Approach:
- Create a function checkNeonnumb() which accepts the number as an argument and returns true if it is Neon number else returns False.
- Give the lower limit range and upper limit range as user input using map(),int(),split() functions.
- Store them in two separate variables.
- Inside the checkNeonnumb() function
- Calculate the square of the given number/argument using the ** operator or multiply the given number by itself and store it in a variable.
- Convert this squared number into a list of digits using list(),int(),map(),str() functions.
- Store this list in a variable.
- Calculate the sum of digits of this list using the sum() function.
- Check if this sum is equal to the given number or not using the If conditional statement.
- If it is true then the given number is a Neon Number so return True
- Else the given number is not a Neon Number so return False.
- Loop from lower limit range to upper limit range using For loop.
- Inside the for loop pass the iterator value to checkNeonnumb() function.
- If it returns true then print the iterator value.
- The Exit of the Program.
Below is the implementation:
# Create a function checkNeonNumb() which accepts the number as an argument and # returns true if it is Neon number else returns False. def checkNeonNumb(numb): # Calculate the square of the given number/argument using the ** operator or multiply # the given number by itself and store it in a variable. squarnumb = numb**2 # Convert this squared number into a list of digits # using list(),int(),map(),str() functions. # Store this list in a variable. numbedigit = list(map(int, str(squarnumb))) # Calculate the sum of digits of this list using the sum() function. sumdigi = sum(numbedigit) # Check if this sum is equal to the given number # or not using the If conditional statement. # If it is true then the given number is a Neon Number so return True if(sumdigi == numb): return True # Else the given number is not a Neon Number so return False. else: return False # Give the lower limit range and upper limit range as # user input using map(),int(),split() functions. # Store them in two separate variables. lowlimrange, upplimrange = map(int, input( 'Enter lower limit range and upper limit range separate bt spaces = ').split()) print('The Neon numbers in the given range', lowlimrange, 'and', upplimrange, 'are:') # Loop from lower limit range to upper limit range using For loop. for p in range(lowlimrange, upplimrange+1): # Inside the for loop pass the iterator value to checkNeonnumb() function. if(checkNeonNumb(p)): # If it returns true then print the iterator value. print(p, end=' ')
Output:
Enter lower limit range and upper limit range separate bt spaces = 1 23 The Neon numbers in the given range 1 and 23 are: 1 9
Related Programs:
- python program to print numbers in a range 1upper without using any loops or by using recursion
- python program to read print prime numbers in a range using sieve of eratosthenes
- python program to print all co binary palindromic numbers in a range
- python program to print sum of negative numbers positive even numbers and positive odd numbers in a list
- python program to find those numbers which are divisible by 7 and multiple of 5 in a given range of numbers
- python program to find all numbers in a range which are perfect squares and sum of all digits in the number is less than 10
- python program to read a text file and print all the numbers present in the text file