BeginnersBook

  • Home
  • Java
    • Java OOPs
    • Java Collections
    • Java Examples
  • C
    • C Examples
  • C++
    • C++ Examples
  • DBMS
  • Computer Network
  • Python
    • Python Examples
  • More…
    • jQuery
    • Kotlin
    • WordPress
    • SEO
    • JSON
    • JSP
    • JSTL
    • Servlet
    • MongoDB
    • XML
    • Perl

Python Program to Add Digits of a Number

By Chaitanya Singh | Filed Under: Python Examples

In this tutorial, we will write a simple Python program to add the digits of a number using while loop. For example, if the input number is 1234 then the output would be 1+2+3+4 = 10 (sum of digits).

Program to sum all the digits of an input number

In this program user is asked to enter a positive number and the program then adds the digits of that number using while loop.

# Program published on https://beginnersbook.com

# Python program to sum all the digits of an input number

num = int(input("Enter a Number: "))
result = 0
hold = num

# while loop to iterate through all the digits of input number
while num > 0:
    rem = num % 10
    result = result + rem
    num = int(num/10)

# displaying output
print("Sum of all digits of", hold, "is: ", result)

Output 1:

Enter a Number: 5257
Sum of all digits of 5257 is:  19

Output 2:

Enter a Number: 1200
Sum of all digits of 1200 is:  3

Output 3:

Enter a Number: 1004
Sum of all digits of 1004 is:  5

Python Program to Add Digits of a Number

Related Python Examples:

1. Python program to add subtract multiply and divide two numbers
2. Python program to add two matrices
3. Python program to add two binary numbers
4. Addition of two numbers in Python

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Python Tutorial

Introduction

  • Python Tutorial
  • Python Introduction
  • Install Python
  • PyCharm IDE Installation
  • Python in PyCharm
  • Python Comments
  • Python Variables
  • Python Keywords & Identifiers
  • Python data types

Flow Control

  • Python If
  • Python if..else
  • Python if..elif..else
  • Python Nested If
  • Python for loop
  • Python while loop
  • Python break
  • Python continue
  • Python pass

Python Functions

  • Python Functions
  • Python Recursion

Python Datatypes

  • Python Numbers
  • Python List
  • Python Strings
  • Python Tuple
  • Python Dictionary
  • Python Set

Python OOPs

  • Python OOP
  • Python Class & Object
  • Python Constructors

Python Examples

  • Python Programs

Copyright © 2012 – 2022 BeginnersBook . Privacy Policy . Sitemap