# Golang Example - Calculate Sum of digits of a integer number

In this post, You will learn different ways to addition of digits of a number in Golanguage

• Using Iterative Function
• Using Recursive Function

Sum of digits of a number is to retrieve each digit of a number and add these numbers to result. For example, if given number is 154, The sum of digits of a number is 1+5+4=10.

To understand this example, You should have following features in Go language

### Example program - Sum of digits of a number using Modulus Operator

In this below program,The intput number is read from the user keyboard console, store this in variable number.

Iterate the each digit of a number using for loop until condition (number!=0) is false i.e number is zero.

Retrieve remainder for the given number using modulus operator and add this to the sumResult. This will be continue execution until division of number with base 10 is zero

Here is a program to calculate add of digits of a integer using for loop

``````package main

import (
"fmt"
)

func sumDigits(number int) int {
remainder := 0
sumResult := 0
for number != 0 {
remainder = number % 10
sumResult += remainder
number = number / 10
}
return sumResult
}
func main() {
var number int
fmt.Print("Enter Number:")
fmt.Scanln(&number)
fmt.Printf("Addition digits of %d = %d\n", number, sumDigits(number))

}

``````

When above program is executed, Output is

``````Enter Number:2535
Addition digits of 2535 = 15

``````

For example, If given input is entered is 2535 and variable sumResult is zero initially

Loop the number and inside the For loop
2535%10=5  modulus operator % gives remainder
sumResult=sumResult+remainder(0+5), sumResult=5 now
2535/10=235 ie number 235
235%10=5
sumResult=sumResult+5 ie 5+5=10, so sumResult=10 now
235/10=23 ie number 23
23%10=3
sumResult=sumResult+3 ie 10+3=13, so sumResult=13 now
23/10=3 ie number 2
2%10=2
sumResult=sumResult+2 ie 13+2=15, so sumResult=15 now
0/10=0 ie number 0
number is zero, conditional expression in for loop is false, Loop terminates its execution

### Example Program 2- Calculates Sum of digits using recursive function

In this program, It takes user input from a keyboard terminal, store it variable number

Declared Recursive function, Recursion is a function which called inside a function.

Initially Recursive funciton called from main function

Here are the steps for recursion steps to calculate digits

For example, if given number is 67845

recursiveSumDigits(67845) - sumResult=0

--recursiveSumDigits(6784) - sumResult=0+5

–recursiveSumDigits(678) - sumResult=5+4

–recursiveSumDigits(67) - sumResult=9+8

–recursiveSumDigits(6) - sumResult=17+7

–recursiveSumDigits(0) - sumResult=24 +6=30 is returned

``````package main

import (
"fmt"
)

func recursiveSumDigits(number int) int {
sumResult := 0
if number == 0 {
return 0
}
sumResult = number%10 + recursiveSumDigits(number/10)
return sumResult
}

func main() {
var number int
fmt.Print("Enter Number:")
fmt.Scanln(&number)
fmt.Printf("Addition digits of %d = %d\n", number, recursiveSumDigits(number))

}

``````

Output is

``````Enter Number:67845
Addition digits of 67845 = 30

``````
Similar Posts
Subscribe
You'll get a notification every time a post gets published here.

Related posts