Find Magic Number - Python

 Hi Friends,

Here I solved magic number problem by python


A number is said to be a magic number, if the sum of its digits are calculated till a single digit recursively by adding the sum of the digits after every addition. If the single digit comes out to be 1,then the number is a magic number


CODE:


def magic(n):

  a = len(str(n))

  sum = 0

  for i in str(n):

    sum += int(i)

  if len(str(sum)) == 1:

    return sum

  else:

    return magic(sum)


a = int(input("Enter a range to find: "))


c = []


for i in range(1,a+1):

  result = magic(i)

  if result == 1:

    c.append(i)

  

print(c)

-------------


By

PyKAR

Comments

  1. This is really good information thank youpython course

    ReplyDelete
  2. This is really a good source of information, I will often follow it to know more information and expand my knowledge, I think everyone should know it, thanks. Best network interview questions service provider.

    ReplyDelete

Post a Comment

Popular posts from this blog

Hardy - Ramanujan Number (Magic Number) Program in Python

FLAMES CODE ON PYTHON

Count Swap for Sorting in Python