Sunday, May 22, 2016

Sum of Digits in Python

A simple program that I wrote using Python as my programming language that will ask the user to give a number and then our program will add the sum of digits in a given number by our user. The code is very to understand.

Add me at Facebook my address is jakerpomperada@gmail.com and jakerpomperada@yahoo.com

My mobile number here in the Philippines is 09173084360.



Sample Program Output


Program Listing

def sum_digits(num):
  result = 0
  base = 10
  pos = 10
  while pos <= num*base:
    prev = pos/base
    result = result + int( (num % pos)/ prev)
    pos = pos*base
  return result

def my_program():
   print()
   print("SUM OF DIGITS IN PYTHON")
   print()
   number = int(input("Enter a Number : "))
   print()
   solve_me = sum_digits(number)
   print("The total sum of digits is " , solve_me, ".")
   print("\n");
   repeat = input('Do you want to continue ? (Y/N) : ')

   if repeat.upper() == "N":
       print("THANK YOU FOR USING THIS SOFTWARE")
       print("\n")
       print("END OF PROGRAM")
       quit
   if repeat.upper() == "Y":
       my_program()

if __name__ == '__main__':
       my_program()




No comments:

Post a Comment