Calculations

Python program to compute the sum of digits in a given numbers

Python program to compute the sum of digits in a given numbers

In this tutorial, we will discuss a concept of  Python program to compute the sum of digits in a given numbers

In this article, we are going to learn  how to find the sum of digits in a given numbers in Python programming language

Compute the sum of digits in a given numbers using while loop

Program 1

num=int(input("Enter a number: "))  #input from the user
sum=0
while(num>0):
   digit=num%10
   sum=sum+digit
   num=num//10
print("The sum of digits is: ",sum)

When the above code is executed, it produces the following results

Case 1

Enter a number: 123
The sum of digits is: 6

 

Case 2

Enter a number: 54321
The sum of digits is: 15

 

Approaches:

  1. Takes the value of the integer and store the variable num
  2. Declare and initialize the variable sum to zero
  3. Get the each digits of numberand add the digits to a variable using while loop
  4. Print sum of the digits of the number

 

Similar post

C program to compute the sum of digits in a given numbers

Java program to compute the sum of digits in a given numbers

C++ program to compute the sum of digits in a given numbers

 

Suggested for you

for loop in Python language

while loop in Python language

Operator in Python language

Data type in Python language

 

 

 

C++ program to compute the sum of digits in a given numbers
C++ program to add two numbers using pointer
Karmehavannan

I am Mr S.Karmehavannan. Founder and CEO of this website. This website specially designed for the programming learners and very especially programming beginners, this website will gradually lead the learners to develop their programming skill.

Recent Posts

PHP Star Triangle pattern program

PHP Star Triangle pattern program In this tutorial, we will discuss about PHP Star Triangle…

1 month ago

PHP Full Pyramid pattern program

PHP Full Pyramid pattern program In this tutorial, we will discuss about PHP Full Pyramid…

2 months ago

5 methods to add two numbers in Java

5 methods to add two numbers in Java In this tutorial, we will discuss the…

2 months ago

Python Full Pyramid star pattern program

Python full Pyramid star pattern program In this tutorial, we will discuss  the concept of…

5 months ago

Write a function or method to convert C into F -Entered by user

Write a function or method to convert C into F -Entered by the user In…

10 months ago

How to write a function or method to convert Celsius into Fahrenheit

How to write a function or method to convert Celsius into Fahrenheit In this tutorial,…

10 months ago