-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrecursive-function.go
49 lines (40 loc) · 1.11 KB
/
recursive-function.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
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")
}