MagicNumber

    0

    3

    Mahendra Kumar

    Mathematicslogical

    to find whether the given number is a magic number or not Eg: 1729 1+7+2+9 = 19 1+9=10 1+0=1 hence the final answer is 1 and it is a magic number.

    import math
    num = int(input())
    digitCount = int(math.log10(num))+1
    sumOfDigits = 0
    temp = num
    #calculating sum of digits of temp(i.e num) until
    #sumOfDigits is a single digit
    while( digitCount > 1):
      sumOfDigits = 0
      while(temp > 0):
        sumOfDigits += temp%10
        temp = temp//10
      temp = sumOfDigits
      #count the digits of sumOfDigits
      digitCount = int(math.log10(sumOfDigits))+1
    #check whether sumOfDigits == 1
    if(sumOfDigits == 1):
        print("Magic number")
    else:
        print("Not a magic number")
    
    Codiga Logo
    Codiga Hub
    • Rulesets
    • Playground
    • Snippets
    • Cookbooks
    soc-2 icon

    We are SOC-2 Compliance Certified

    G2 high performer medal

    Codiga – All rights reserved 2022.