Python Basic
Python Interview Programs
- Python Interview Programs
- Python Abundant Number
- Python Amicable Number
- Python Armstrong Number
- Python Average of N Numbers
- Python Automorphic Number
- Python Biggest of three numbers
- Python Binary to Decimal
- Python Common Divisors
- Python Composite Number
- Python Condense a Number
- Python Cube Number
- Python Decimal to Binary
- Python Decimal to Octal
- Python Disarium Number
- Python Even Number
- Python Evil Number
- Python Factorial of a Number
- Python Fibonacci Series
- Python GCD
- Python Happy Number
- Python Harshad Number
- Python LCM
- Python Leap Year
- Python Magic Number
- Python Matrix Addition
- Python Matrix Division
- Python Matrix Multiplication
- Python Matrix Subtraction
- Python Matrix Transpose
- Python Maximum Value of an Array
- Python Minimum Value of an Array
- Python Multiplication Table
- Python Natural Number
- Python Number Combination
- Python Odd Number
- Python Palindrome Number
- Python Pascalβs Triangle
- Python Perfect Number
- Python Perfect Square
- Python Power of 2
- Python Power of 3
- Python Pronic Number
- Python Prime Factor
- Python Prime Number
- Python Smith Number
- Python Strong Number
- Python Sum of Array
- Python Sum of Digits
- Python Swap Two Numbers
- Python Triangular Number
Python Program to Check Magic Numbern
Photo Credit to CodeToFun
π Introduction
In the world of programming, the concept of magic numbers is a fascinating one.
A magic number is a number in which the eventual sum of its digits, when the sum is recursively calculated, leads to a single-digit number that is 1. If the final sum is not 1, the number is not considered magic.
In this tutorial, we will delve into a Python program that checks whether a given number is a magic number or not.
π Example
Let's examine the Python code that verifies the magic number property.
# Function to check if a number is magic
def is_magic_number(num):
while num > 9:
num = sum(int(digit) for digit in str(num))
# Check if the final sum is 1
return num == 1
# Driver program
if __name__ == "__main__":
# Replace this value with your desired number
number = 19
# Check if the number is magic
if is_magic_number(number):
print(f"{number} is a Magic Number.")
else:
print(f"{number} is not a Magic Number.")
π» Testing the Program
To test the program with different numbers, simply replace the value of number in the if __name__ == "__main__": block.
19 is a Magic Number.
Run the script to see whether the number is a magic number or not.
π§ How the Program Works
- The program defines a function is_magic_number that takes an integer as input and checks if it is a magic number.
- Inside the function, it iteratively calculates the sum of the digits of the number until the number becomes a single-digit number.
- It then checks if the final sum is equal to 1, indicating a magic number.
π Between the Given Range
Let's take a look at the python code that checks for Magic Numbers within the specified range.
# Function to check if a number is a magic number
def is_magic_number(num):
while num > 9:
num = sum(int(digit) for digit in str(num))
return num == 1
# Define the range
start_range = 1
end_range = 50
# Find and print magic numbers in the range
magic_numbers = [num for num in range(start_range, end_range + 1) if is_magic_number(num)]
# Output format
print(f"Magic Numbers in the range {start_range} to {end_range}:\n{' '.join(map(str, magic_numbers))}")
π» Testing the Program
Magic Numbers in the range 1 to 50: 1 10 19 28 37 46
Run the script to see the magic numbers in the specified range.
π§ How the Program Works
- The program defines a function is_magic_number that checks if a given number is a magic number.
- Inside the function, it repeatedly sums the digits of the number until a single-digit number is obtained.
- The program then iterates through the specified range (1 to 50) and identifies magic numbers.
- The output is formatted to display the magic numbers in the specified range.
π§ Understanding the Concept of Magic Numbers
A magic number is a number in which the sum of its digits leads to a single-digit number, and that single-digit number is 1.
For example, the number 19 is a magic number because the sum of its digits (1 + 9) is 10, and the sum of 1 and 0 is 1.
π’ Optimizing the Program
While the provided program is effective, consider exploring and implementing optimizations or additional features based on your specific requirements.
Feel free to incorporate and modify this code as needed for your specific use case. Happy coding!
π¨βπ» Join our Community:
Author
For over eight years, I worked as a full-stack web developer. Now, I have chosen my profession as a full-time blogger at codetofun.com.
Buy me a coffee to make codetofun.com free for everyone.
Buy me a Coffee
If you have any doubts regarding this article (Python Program to Check Magic Number), please comment here. I will help you immediately.