# Go Program to Find Sum of Digits in a Number

Write a Go Program to Find Sum of Digits in a Number using for loop. The for loop condition make sure the number is greater than zero. Within the loop,

• digiReminder = digiNum % 10 – It adds the last digit of a number to digiReminder.
• digiSum = digiSum + digiReminder -> Adds the last digit to digiSum.
• digiNum = digiNum / 10 – It removes the last digit from the digiNum.
```package main

import "fmt"

func main() {

var digiNum, digiSum, digiReminder int

fmt.Print("Enter the Number to find the Sum of Digits = ")
fmt.Scanln(&digiNum)

for digiSum = 0; digiNum > 0; digiNum = digiNum / 10 {
digiReminder = digiNum % 10
digiSum = digiSum + digiReminder
}
fmt.Println("The Sum of Digits in this Number = ", digiSum)
}```

## Go Program to find the Sum of digits in a number using a function

```package main

import "fmt"

var digiSum int

func digitSum(digiNum int) int {
var digiReminder int

for digiSum = 0; digiNum > 0; digiNum = digiNum / 10 {
digiReminder = digiNum % 10
digiSum = digiSum + digiReminder
}
return digiSum
}

func main() {

var digiNum int

fmt.Print("Enter the Number to find the Sum of Digits = ")
fmt.Scanln(&digiNum)

digiSum = digitSum(digiNum)
fmt.Println("The Sum of Digits in this Number = ", digiSum)
}```
``````SureshMac:GoExamples suresh\$ go run digitSum2.go
Enter the Number to find the Sum of Digits = 4567
The Sum of Digits in this Number =  22
SureshMac:GoExamples suresh\$ go run digitSum2.go
Enter the Number to find the Sum of Digits = 98759
The Sum of Digits in this Number =  38``````

Golang Program to Calculate Sum Digits in a Number using Recursion In this Golang Program to calculate the sum of digits in a Number, we call digitSum(digiNum/10) function recursively.

```package main

import "fmt"

var digiSum int

func digitSum(digiNum int) int {
digiSum = 0
if digiNum == 0 {
return 0
}
digiSum = digiNum%10 + digitSum(digiNum/10)
return digiSum
}

func main() {

var digiNum int

fmt.Print("Enter the Number to find the Sum of Digits = ")
fmt.Scanln(&digiNum)

digiSum = digitSum(digiNum)
fmt.Println("The Sum of Digits in this Number = ", digiSum)
}```
``````SureshMac:GoExamples suresh\$ go run digitSum3.go
Enter the Number to find the Sum of Digits = 45678
The Sum of Digits in this Number =  30
SureshMac:GoExamples suresh\$ go run digitSum3.go
Enter the Number to find the Sum of Digits = 8098603
The Sum of Digits in this Number =  34``````