Python program to check if a number is a magic number or not

How to check if a number is a magic number or not in Python:

In this post, we will learn how to check if a number is a magic number or not. We will write one program that will take one number as input from the user, check if it is a magic number or not and print one message.

What is a magic number:

A number is called a magic number if the repeated sum of digits of the number is equal to 1. For example, 1729 is a magic number because its sum of digits is 1 + 7 + 2 + 9 = 19, the sum of digits of 19 is 10 and the sum of digits of 10 is 1. So, the repeated sum of digits of 1729 is 1 and that’s why it is a magic number.

All possible combinations of a magic number are also magic numbers. For 7291, 9721 etc. are all magic numbers.

Algorithm to check if a number is a magic number or not:

We can use the following algorithm to check if a number is a magic number or not:

  • Ask the user to enter a number. Read the number and store it in a variable.
  • Initialize one sum variable as 0 to hold the sum of digits.
  • Find the sum of all digits of the number by using a loop. If it is a two-digit value, run another loop to find the sum of its digits. This process will stop only when the sum will be a single-digit number.
  • If the final sum is 1, it will be a magic number, else it will not a magic number.

Example 1: Python program to check if a number is magic:

Let’s write down the program to check if a number is magic number or not:

no = int(input("Enter a number: "))

total_digits = len(str(no))

copy_no = no
while total_digits > 1:
    sum = 0
    while copy_no > 0:
        sum += copy_no % 10
        copy_no //= 10
    copy_no = sum
    total_digits = len(str(copy_no))

if sum == 1:
    print(f'{no} is a Magic number')
else:
    print(f'{no} is not a Magic number')

In this program,

  • The user input number is assigned to the variable no.
  • The total_digits variable holds the total number of digits of the number.
  • It created a copy of the number, copy_no and runs a while loop to find the repeated sum of its digits.
  • The inner while loop finds the sum of all digits of the number. It assigns that value to the copy_no and the length is calculated. If the length of the number is not 1, the outer while loop runs again to find the sum of digits of the previous sum.
  • If the final sum is equal to 1, it prints that the number is a magic number, else it is not a magic number.

It gives output as below:

Enter a number: 333
333 is not a Magic number

Enter a number: 100
100 is a Magic number

Example 2: Python program to check if a number is magic by using a separate function:

The below program uses a separate function to check if a number is magic or not:

def is_magic(no):
    total_digits = len(str(no))

    copy_no = no
    while total_digits > 1:
        sum = 0
        while copy_no > 0:
            sum += copy_no % 10
            copy_no //= 10
        copy_no = sum
        total_digits = len(str(copy_no))
    return sum == 1


if __name__ == "__main__":
    no = int(input("Enter a number: "))

    if is_magic(no):
        print(f'{no} is a Magic number')
    else:
        print(f'{no} is not a Magic number')

Here, we created a new function is_magic to check for a magic number. It takes one number as its parameter and returns one boolean value. If the number is a magic number, it returns True, else False.

It will give similar results.

Enter a number: 100
100 is a Magic number

Enter a number: 243
243 is not a Magic number

Example 3: Python program to find all magic numbers in a range:

It is easy to find all magic numbers in a range with a loop. We can change the above program to take the start and end limits also as user inputs. We can then use a loop to iterate over the limit to find all magic numbers.

def is_magic(no):
    total_digits = len(str(no))

    copy_no = no
    while total_digits > 1:
        sum = 0
        while copy_no > 0:
            sum += copy_no % 10
            copy_no //= 10
        copy_no = sum
        total_digits = len(str(copy_no))
    return sum == 1


if __name__ == "__main__":
    lower_range = int(input("Enter the lower range: "))
    upper_range = int(input("Enter the upper range: "))

    print(f'Magic numbers between {lower_range} and {upper_range} are: ')
    for n in range(lower_range, upper_range + 1):
        if is_magic(n):
            print(n)

In this program,

  • The lower range and upper range are assigned to the variables lower_range and upper_range.
  • It uses a for loop to iterate from lower_range to upper_range. In the loop, it uses is_magic method to check if the current iterating value is a magic number or not. If yes, it prints its value.

Sample output:

Enter the lower range: 10
Enter the upper range: 100
Magic numbers between 10 and 100 are: 
10
19
28
37
46
55
64
73
82
91
100

You might also like: