• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
PythonForBeginners.com

PythonForBeginners.com

Learn By Example

  • Home
  • Learn Python
    • Python Tutorial
  • Categories
    • Basics
    • Lists
    • Dictionary
    • Code Snippets
    • Comments
    • Modules
    • API
    • Beautiful Soup
    • Cheatsheet
    • Games
    • Loops
  • Python Courses
    • Python 3 For Beginners
You are here: Home / Basics / Find The Sum Of Digits Of An Integer In Python

Find The Sum Of Digits Of An Integer In Python

Author: Aditya Raj
Last Updated: January 9, 2022

Integers are used almost everywhere when you write a program. In this article, we will discuss ways to find the sum of digits of a given integer in python.

How to find the sum of digits of an integer?

To find the sum of digits of a number N, we can extract the digits of a number and add them one by one.  For this, we will extract the digits from right to left. For extracting the digits, we will divide the number by 10 and store the remainder during each division operation. We will keep dividing the number by 10 until it becomes 0. In this way, we can extract digits one by one. 

For example, If we are given the number 12345, we will calculate the sum of digits as follows.

  • First, we will initialize a variable sumOfDigits to zero.
  • After that, we will divide 12345 by 10. 
  • After division, we will add reminder 5 to sumOfDigits and update the number to 1234.
  • Then, we will divide 1234 by 10.
  • After division, we will add reminder 4 to sumOfDigits and update the number to 123.
  • Again, we will divide 123 by 10.
  • After division, we will add reminder 3 to sumOfDigits and update the number to 12.
  • Again, we will divide 12 by 10.
  • After division, we will add reminder 2 to sumOfDigits and update the number to 1.
  • Now, we will divide 1 by 10.
  • After division, we will add reminder 1 to sumOfDigits and update the number to 0.
  • As the number has become 0, we will be having the sum of digits of the number 12345 in the variable sumOfDigits i.e. 15.

Implementation In Python

As we have seen above, to find the sum of digits of an integer in python, we just have to divide the number by 10 until it becomes 0. At the same time, we have to add the remainder in each division to obtain the sum of digits. We can implement the program to perform this operation as follows.

def calculate_sum_of_digits(N):
    sumOfDigits = 0
    while N > 0:
        digit = N % 10
        sumOfDigits = sumOfDigits + digit
        N = N // 10
    return sumOfDigits


input_number = 12345
output = calculate_sum_of_digits(input_number)
print("Sum of digits of {} is {}.".format(input_number, output))
input_number = 126
output = calculate_sum_of_digits(input_number)
print("Sum of digits of {} is {}.".format(input_number, output))

Output:

Sum of digits of 12345 is 15.
Sum of digits of 126 is 9.

Conclusion

In this article, we have discussed and implemented a program to find the sum of digits of an integer in Python. To learn more about numbers in python, you can read this article on decimal numbers in python. You might also like this article on complex numbers in python.

Related

Recommended Python Training

Course: Python 3 For Beginners

Over 15 hours of video content with guided instruction for beginners. Learn how to create real world applications and master the basics.

Enroll Now

Filed Under: Basics Author: Aditya Raj

More Python Topics

API Argv Basics Beautiful Soup Cheatsheet Code Code Snippets Command Line Comments Concatenation crawler Data Structures Data Types deque Development Dictionary Dictionary Data Structure In Python Error Handling Exceptions Filehandling Files Functions Games GUI Json Lists Loops Mechanzie Modules Modules In Python Mysql OS pip Pyspark Python Python On The Web Python Strings Queue Requests Scraping Scripts Split Strings System & OS urllib2

Primary Sidebar

Menu

  • Basics
  • Cheatsheet
  • Code Snippets
  • Development
  • Dictionary
  • Error Handling
  • Lists
  • Loops
  • Modules
  • Scripts
  • Strings
  • System & OS
  • Web

Get Our Free Guide To Learning Python

Most Popular Content

  • Reading and Writing Files in Python
  • Python Dictionary – How To Create Dictionaries In Python
  • How to use Split in Python
  • Python String Concatenation and Formatting
  • List Comprehension in Python
  • How to Use sys.argv in Python?
  • How to use comments in Python
  • Try and Except in Python

Recent Posts

  • Count Rows With Null Values in PySpark
  • PySpark OrderBy One or Multiple Columns
  • Select Rows with Null values in PySpark
  • PySpark Count Distinct Values in One or Multiple Columns
  • PySpark Filter Rows in a DataFrame by Condition

Copyright © 2012–2025 · PythonForBeginners.com

  • Home
  • Contact Us
  • Privacy Policy
  • Write For Us