{

Golang Example - Calculate Sum of digits of an integer number


Golang example Calculate Sum of digits of an integer number

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

  • Using Iterative Function
  • Using Recursive Function

The Sum of digits of a number is to retrieve each digit of a number and add these numbers to the result.

For example, if the given number is 154, The sum of digits of a number is 1+5+4=10.

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

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

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

Iterate each digit of a number using for a loop until the 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.

It does continue execution until the division of number with base 10 is zero.

Here is a program to calculate add of digits of an 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 the above program is executed, Output is

Enter Number:2535  
Addition digits of 2535 = 15  

For example, If the 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 evaluated to false and Loop terminates its execution.

Example Program 2- Calculates Sum of digits using recursive function

In this program, takes user input from a keyboard terminal, stores it a variable number.

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

Initially, the Recursive function is called from the main function.

Here are the steps for recursion steps to calculate digits

For example, if the given number is 67845

Following are step by step execution

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

Example program:

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  
THE BEST NEWSLETTER ANYWHERE
Join 6,000 subscribers and get a daily digest of full stack tutorials delivered to your inbox directly.No spam ever. Unsubscribe any time.

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