---
title: "How to find the sum of each digit in a positive integer?"  
description: "How to find the sum of each digit in a positive integer?"  
author: "Ashutosh Patel"  
published: 2023-02-08  
updated: 2023-06-15  
canonical: https://www.mindstick.com/forum/157388/how-to-find-the-sum-of-each-digit-in-a-positive-integer  
category: "c#"  
tags: ["c#", "java", "programs"]  
reading_time: 3 minutes  

---

# How to find the sum of each digit in a positive integer?

How to find the [sum](https://www.mindstick.com/forum/159332/how-can-i-use-sum-in-linq) of each digit in a given positive [integer](https://answers.mindstick.com/qa/113667/write-code-for-roman-to-integer)?

## Replies

### Reply by Aryan Kumar

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

1. Initialize a variable to store the sum.
2. Divide the integer by 10 repeatedly, and keep the remainder.
3. Add the remainder to the sum.
4. Repeat steps 2 and 3 until the integer is 0.
5. Return the sum.

For example, to find the sum of the digits in the number 12345, we would do the following:

Code snippet

```plaintext
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

```plaintext
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

```plaintext
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.

### Reply by Rahul Roi

**I am writing code in “Python” language.**

```python
def sum_of_digit(n):
    if(n<10):
        return n;
    return n % 10 + sum_of_digit(n//10);
print(sum_of_digit(14563289));
print(sum_of_digit(-14563289));
```

**Output:**\
**38**

## -14563289

### Reply by Revati S Misra

There is a following simple program to find the sum of each digit in a given positive integer,

```cs
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
```


---

Original Source: https://www.mindstick.com/forum/157388/how-to-find-the-sum-of-each-digit-in-a-positive-integer

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
