Welcome back, Problem solving is the most important part of a programmers life cause each and every day they work on solving issues so they need to be good in problem solving, So today we are going to Create a Program for Sum of the digits of a given number, We are going to implement this in Golang Programming Language if anyone wants the solutions in other languages as well please don’t hesitate to leave a comment, we are going trying to solve this problem via multiple approaches, approaches mentioned below:

- Sum of the digits of a given number with input as string not using modular and divide
- Sum of the digits of a given number using modular and divide
- Sum of the digits of a given number using recursion
- Sum of the digits of a given number using tail recursion

**EXAMPLES**

```
Input: n = 12345
Output: 15
Input: n = 1234
Output: 10
```

## 1. Given a number, find the sum of its digits.

When the number of digits of that number exceeds 1019 , we can’t take that number as an integer since the range of long long int doesn’t satisfy the given number. So take input as a string, run a loop from start to the length of the string and increase the sum with that character(in this case it is numeric)

Follow the below steps to solve the problem:

- Declare a variable sum equal to zero
- Run a loop from zero to the length of the input string
- Add the value of each character into the sum, by converting the character into it’s integer value
- Return sum

Below is the implementation of the above approach:

package main import ( "fmt" _ "strconv" ) // function to get the sum of digits func doSum(n string) int { // step one to get length of the digits of given number convert it to string sum := 0 for i := range n { // Since ascii value of // numbers starts from 48 // so we subtract it from sum sum += int(n[i]) - 48 } // SECOND APPROACH IF FUNCTION INPUT IS INT // nn := strconv.Itoa(n) // for i := range nn { // // Since ascii value of // // numbers starts from 48 // // so we subtract it from sum // sum = sum + int(nn[i]) - 48 // } return sum } func main() { n := "12345" fmt.Println(doSum(n)) }

**Output:**

```
15
```

## 2. Sum of the digits of a given number using modular and divide:

Follow the below steps to solve the problem:

- Get the number
- Declare a variable to store the sum and set it to 0
- Repeat the next two steps till the number is not 0
- Get the rightmost digit of the number with help of the remainder ‘%’ operator by dividing it by 10 and adding it to the sum.
- Divide the number by 10 with help of ‘/’ operator to remove the rightmost digit.
- Print or return the sum

Below is the implementation of the above approach:

package main import ( "fmt" ) // function to get sum of digits func doSum(n int) int { sum := 0 for n != 0 { sum += n % 10 n = int(n / 10) } return sum } func main() { n := 12345 fmt.Println(doSum(n)) }

**Output:**

```
15
```

## 3. Sum of the digits of a given number using recursion

Follow the below steps to solve the problem:

- Get the number
- Get the remainder and pass the next remaining digits
- Get the rightmost digit of the number with help of the remainder ‘%’ operator by dividing it by 10 and adding it to the sum.
- Divide the number by 10 with help of the ‘/’ operator to remove the rightmost digit.
- Check the base case with n = 0
- Print or return the sum

Below is the implementation of the above approach:

package main import ( "fmt" ) // function to get sum of digits func doSum(n int) int { if n == 0 { return 0 } return n%10 + doSum(n/10) } func main() { n := 12345 fmt.Println(doSum(n)) }

**Output:**

```
15
```

## 4. Sum of the digits of a given number using tail recursion

Follow the below steps to solve the problem:

- Add another variable “Val” to the function and initialize it to ( Val = 0 )
- On every call to the function add the mod value (n%10) to the variable as “(n%10)+val” which is the last digit in n. Along with passing the variable n as n/10.
- So on the First call, it will have the last digit. As we are passing n/10 as n, It follows until n is reduced to a single digit.
- n<10 is the base case so When n < 10, then add the n to the variable as it is the last digit and return the val which will have the sum of digits

Below is the implementation of the above approach:

package main import ( "fmt" ) // function to get sum of digits func tailRecursion(n int, v int) int { if n < 10 { v += n return v } return tailRecursion(int(n/10), (n%10)+v) } func main() { n := 12345 fmt.Println(tailRecursion(n,0)) }

**Output:**

```
15
```

**FULL CODE:**

package main import ( "fmt" ) // function to get the sum of digits func simpleSum(n string) int { sum := 0 for i := range n { // Since ascii value of // numbers starts from 48 // so we subtract it from sum sum += int(n[i]) - 48 } return sum } // function to get the sum of digits func sumUsingModular(n int) int { sum := 0 for n != 0 { sum += n % 10 n = int(n / 10) } return sum } // function to get the sum of digits func sumUsingRecursion(n int) int { if n == 0 { return 0 } return n%10 + sumUsingRecursion(n/10) } // function to get sum of digits func tailRecursion(n int, v int) int { if n < 10 { v += n return v } return tailRecursion(int(n/10), (n%10)+v) } func main() { n := 12345 sn := "12345" simpleSum := simpleSum(sn) sumUsingModular := sumUsingModular(n) sumUsingRecursion := sumUsingRecursion(n) tailSum := tailRecursion(n,0) fmt.Printf("simple sum: %d\nmodular sum: %d\nrecursive sum: %d\ntail sum: %d\n",simpleSum, sumUsingModular, sumUsingRecursion,tailSum) }

**code link:**

If you found code/algorithm incorrect or found better approach then please comment.