Write a Golang program to find the sum of digits for a given number

Go ProgrammingServer Side ProgrammingProgramming

Examples

  • num = 125 => 1 + 2 + 5 = 8
  • num = 101 => 1 + 0 + 1 = 2
  • num = 151 => 1 + 5 + 1 = 7

Approach to solve this problem

Step 1: Define a function that accepts numbers(num); type is int.

Step 2: Start a true loop until num becomes 0 and define res:=0.

Step 3: Find modulo and add to res.

Step 4: Divide num by 10.

Step 5: Return res.

Program

Live Demo

package main
import "fmt"
func findDigitSum(num int) int {
   res := 0
   for num>0 {
      res += num % 10
      num /= 10
   }
   return res
}

func main(){
   fmt.Println(findDigitSum(168))
   fmt.Println(findDigitSum(576))
   fmt.Println(findDigitSum(12345))
}

Output

15
18
15
raja
Published on 04-Feb-2021 10:57:15
Advertisements