Skip to content

Instantly share code, notes, and snippets.

@hygull
Last active December 19, 2016 17:25
Show Gist options
  • Select an option

  • Save hygull/62918db45c71a222798bf2bc0cad0ff1 to your computer and use it in GitHub Desktop.

Select an option

Save hygull/62918db45c71a222798bf2bc0cad0ff1 to your computer and use it in GitHub Desktop.
To get sum of all the digits in positive numbers created by hygull - https://repl.it/Et3H/3
/*
{
"date_of_creation" => "19 Dec 2016, Mon",
"aim_of_program" => "To get sum of all the digits in positive numbers",
"coded_by" => "Rishikesh Agrawani",
"Go_version" => "1.7",
}
*/
package main
import "fmt"
func main() {
intNumsArr := [5]int{1346, 678844, 575632, 2433434, 1} //An array of 5 intergers
for _, num := range intNumsArr {
sum := 0
fmt.Print("The sum of all digits of ", num, " is\t: ")
for num != 0 {
sum += num % 10 //To get the last digit of num and adding it with current value of sum
num /= 10 //To get a number (after leaving the last digit)
}
fmt.Println(sum)
}
}
/*
The sum of all digits of 1346 is : 14
The sum of all digits of 678844 is : 37
The sum of all digits of 575632 is : 28
The sum of all digits of 2433434 is : 23
The sum of all digits of 1 is : 1
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment