Created
December 14, 2016 17:46
-
-
Save hygull/06a3e0ef36fb338cd2bc4999f511238b to your computer and use it in GitHub Desktop.
Printing sum of all the digits of all numbers from a given list of positive integers created by hygull - https://repl.it/EqN9/0
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
{ | |
"date_of_creation" => "14 Dec 2016, Wed", | |
"aim_of_program" => "To print sum of all digits of each number of a given list of positive integers using recursion", | |
"coded_by" => "Rishikesh Agrawani", | |
"memory" => "1 submit execution, recursion" | |
"Go_version" => "1.7", | |
} | |
*/ | |
package main | |
import "fmt" | |
/* A function that returns a number which is equal to sum of all the digits in number n */ | |
func getSumOfDigits(n uint) uint { | |
if n > 0 { | |
return n%10 + getSumOfDigits(n/10) | |
} | |
return n | |
} | |
/* Starter function */ | |
func main() { | |
positiveIntsSlice := []uint{1235, 47891, 96354, 0, 2345457, 342846975} /* slice of unsigned integers */ | |
for _, num := range positiveIntsSlice { | |
fmt.Println("The sum of digits of ", num, " is : ", getSumOfDigits(num)) | |
} | |
} | |
/* | |
The sum of digits of 1235 is 11 | |
The sum of digits of 47891 is 29 | |
The sum of digits of 96354 is 27 | |
The sum of digits of 0 is 0 | |
The sum of digits of 2345457 is 30 | |
The sum of digits of 342846975 is 48 | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment