There are many ways to find the sum of each digit in a positive integer. Here are a few methods:
Method 1: Using a loop
Initialize a variable to store the sum.
Divide the integer by 10 repeatedly, and keep the remainder.
Add the remainder to the sum.
Repeat steps 2 and 3 until the integer is 0.
Return the sum.
For example, to find the sum of the digits in the number 12345, we would do the following:
Code snippet
sum = 0
while 12345 > 0:
sum += 12345 % 10
12345 //= 10
This would give us a sum of 15.
Method 2: Using the sum() function
The sum() function in Python can be used to find the sum of a list of numbers. We can use this function to find the sum of the digits in a number by converting the number to a list of digits first.
For example, to find the sum of the digits in the number 12345, we would do the following:
Code snippet
sum_of_digits = sum(list(str(12345)))
This would give us a sum of 15.
Method 3: Using the reduce() function
The reduce() function in Python can be used to apply a function to all the elements of a list, and then return the result. We can use this function to find the sum of the digits in a number by using the
sum() function as the function to apply.
For example, to find the sum of the digits in the number 12345, we would do the following:
Code snippet
sum_of_digits = reduce(sum, list(str(12345)))
This would give us a sum of 15.
Which method you use to find the sum of the digits in a positive integer depends on your personal preference and the programming language you are using.
There is a following simple program to find the sum of each digit in a given positive integer,
using System;
namespace HelloWorld
{
class Program
{
static void Main(string[] args)
{
Program.SumOfDigits(765);
}
static void SumOfDigits(int num)
{
int sum = 0;
while (num > 0)
{
sum += num % 10;
num /= 10;
}
Console.WriteLine(sum);
}
}
}
Output: 18
Markdown for AI
A clean, structured version of this page for AI assistants and LLMs.
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that you have read and understood our
Cookie Policy &
Privacy Policy.
There are many ways to find the sum of each digit in a positive integer. Here are a few methods:
Method 1: Using a loop
For example, to find the sum of the digits in the number 12345, we would do the following:
Code snippet
This would give us a sum of 15.
Method 2: Using the
sum()functionThe
sum()function in Python can be used to find the sum of a list of numbers. We can use this function to find the sum of the digits in a number by converting the number to a list of digits first.For example, to find the sum of the digits in the number 12345, we would do the following:
Code snippet
This would give us a sum of 15.
Method 3: Using the
reduce()functionThe
reduce()function in Python can be used to apply a function to all the elements of a list, and then return the result. We can use this function to find the sum of the digits in a number by using thesum()function as the function to apply.For example, to find the sum of the digits in the number 12345, we would do the following:
Code snippet
This would give us a sum of 15.
Which method you use to find the sum of the digits in a positive integer depends on your personal preference and the programming language you are using.
I am writing code in “Python” language.
Output:
38
-14563289
There is a following simple program to find the sum of each digit in a given positive integer,