1
0
Fork 0
mirror of https://github.com/gytisrepecka/go-learning synced 2024-11-23 19:43:36 +02:00

Added recursive-function.go initial version.

This commit is contained in:
Gytis Repečka 2018-10-09 20:16:16 +03:00
parent d46be4de20
commit b4c4f41f44

49
recursive-function.go Normal file
View file

@ -0,0 +1,49 @@
// Copyright 2018 Gytis Repečka. All rights reserved.
// Use of this source code is governed by a GNU GPL
// license that can be found in the LICENSE file.
package main
import (
"fmt"
"bufio"
"os"
"strings"
"strconv"
)
func factorial(n int) int {
if n == 0 {
return 1
}
return n*factorial(n-1)
}
func main() {
reader := bufio.NewReader(os.Stdin)
fmt.Print("Input number (and press ENTER): ")
inputString, _ := reader.ReadString('\n')
// Convert CRLF to LF
// Windows
// inputString = strings.Replace(inputString, "\r\n", "", -1)
// Unix
inputString = strings.Replace(inputString, "\n", "", -1)
// Convert string (inputString) to integer (inputInt)
var inputInt int
inputInt, err := strconv.Atoi(inputString)
// If there was error converting string to int
if err != nil {
fmt.Println(err)
}
fmt.Printf("Value (string): %s\n", inputString)
fmt.Printf("Value length: %d\n", len(inputString))
fmt.Printf("Value (int): %d\n", inputInt)
fmt.Println("\n---------------")
fmt.Printf("Factorial of %d is: %d", inputInt, factorial(inputInt))
fmt.Println("\n---------------\n")
}