- Trending Categories
- Data Structure
- Operating System
- C Programming
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Python Program to Find the Sum of Digits in a Number without Recursion
When it is required to find the sum of digits in a number without using the method of recursion, the ‘%’ operator, the ‘+’ operator and the ‘//’ operator can be used.
Below is a demonstration for the same −
def sum_of_digits(my_num): sum_val = 0 while (my_num != 0): sum_val = sum_val + (my_num % 10) my_num = my_num//10 return sum_val my_num = 12345671 print("The number is : ") print(my_num) print("The method to calculate sum of digits is being called...") print("The sum of " +str(my_num) + " is : ") print(sum_of_digits(my_num))
The number is : 12345671 The method to calculate sum of digits is being called... The sum of 12345671 is : 29
- A method named ‘sum_of_digits’ is defined, that takes a number as parameter.
- A sum is initially assigned to 0.
- The number is divided by 10 and the remainder obtained is added to the sum.
- The number is again floor divided by 10 and assigned to the number itself.
- The sum value is returned as output from the function.
- A number is defined, and is displayed on the console.
- The method is called by passing this number as a parameter.
- The output id displayed on the console.
- C# program to find the sum of digits of a number using Recursion
- Java Program to Find Sum of Digits of a Number using Recursion
- Python Program to find the factorial of a number without recursion
- How to find the sum of digits of a number using recursion in C#?
- Program to find the sum of all digits of given number in Python
- Python Program to Find the Fibonacci Series without Using Recursion
- Program to find minimum digits sum of deleted digits in Python
- Python Program to Find the Length of the Linked List without using Recursion
- C program to find sum of digits of a five digit number
- Python Program to Find the Total Sum of a Nested List Using Recursion
- Write a Golang program to find the sum of digits for a given number
- C++ Program to Sum the digits of a given number
- PHP program to sum the digits in a number
- 8085 program to find sum of digits of 8 bit number
- 8086 program to find sum of digits of 8 bit number