Tutorial Gateway

  • C
  • C#
  • Java
  • Python
  • SQL
  • MySQL
  • Js
  • BI Tools
    • Informatica
    • Talend
    • Tableau
    • Power BI
    • SSIS
    • SSRS
    • SSAS
    • MDX
    • R Tutorial
    • Alteryx
    • QlikView
  • More
    • C Programs
    • C++ Programs
    • Go Programs
    • Python Programs
    • Java Programs

Go Program to Find Sum of Digits in a Number

by suresh

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 Sum of Digits in a Number 1

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)
}
Go Program to Find Sum of Digits in a Number 2

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)
}
Go Program to Find Sum of Digits in a Number 3

Placed Under: Go Examples

  • Golang Hello World Program
  • Go Add Two Number
  • Go Compound Interest
  • Go Count Digits in a Number
  • Go Cube of a Number
  • Go Even or Odd
  • Go Largest of Three Numbers
  • Go Reverse Natural Numbers
  • Go Multiplication Table
  • Go Number divisible by 5 and 11
  • Go Positive or Negative
  • Go Power of a Number
  • Go Print Natural Numbers
  • Go Profit or Loss
  • Go Print 1 to 100 without loop
  • Go Program to Print 1 to 100
  • Go Product of Digits in a Number
  • Go Palindrome Number Program
  • Go Print Even Numbers
  • Go Print Odd Numbers
  • Go Factors of a Number
  • Go Perfect Number
  • Go Prime Number
  • Go Reverse a Number
  • Go Simple Interest
  • Go Square of a Number
  • Go Square root of a Number
  • Go Sum of Digits in a Number
  • Go Sum & Avg of Natural Nums
  • Go Sum of Even Numbers
  • Go Sum of Odd Numbers
  • Go Sum of Even and Odd
  • Go Sphere Vol & Surface Area
  • Go Cylinder Vol & Surface Area
  • Go Cuboid Vol & Surface Area
  • Go Cube Volume & Surface Area
  • Go Cone Volume & Surface Area

Copyright © 2021 · All Rights Reserved by Suresh

About Us | Contact Us | Privacy Policy