I am a software developer at MindStick soft. Pvt. ltd. I joined this company in august 2021 after completing my post-graduation course. I love to see the historical places of my country.
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
Liked By
Write Answer
How to find the sum of each digit in a positive integer?
Join MindStick Community
You have need login or register for voting of answers or question.
Aryan Kumar
15-Jun-2023There 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.
Rahul Roi
10-Feb-2023I am writing code in “Python” language.
Output:
38
-14563289
Revati S Misra
09-Feb-2023There is a following simple program to find the sum of each digit in a given positive integer,