Python program to compute the sum of digits in a given numbers
- Home
- Calculations
- Python program to compute the sum of digits in a given numbers
- On
- By
- 0 Comment
- Categories: Calculations, Find elements
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:
- Takes the value of the integer and store the variable num
- Declare and initialize the variable sum to zero
- Get the each digits of numberand add the digits to a variable using while loop
- 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
Python program to compute the sum of digits in a given numbers
Addition of two numbers in Java using method
5 method to find sum of two numbers
JavaScript program to add two numbers
Suggested for you