Add isEven exercise
After spending so much time having to do bash exercises, writing just this tiny bit of Go feels incredible
This commit is contained in:
parent
73413b465b
commit
36e2592e4b
|
@ -0,0 +1,3 @@
|
|||
module isEven
|
||||
|
||||
go 1.23.4
|
|
@ -0,0 +1,34 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
func main() {
|
||||
if len(os.Args) != 2 {
|
||||
fmt.Printf("Insufficent arguments:\n")
|
||||
fmt.Printf("Usage: ./isEven 5\n")
|
||||
os.Exit(0)
|
||||
}
|
||||
num, err := strconv.Atoi(os.Args[1])
|
||||
if err != nil {
|
||||
log.Fatalf("Invalid number found: %v\n", err)
|
||||
}
|
||||
|
||||
if s := isEven(num); s == true {
|
||||
fmt.Printf("El número %v es par\n", num)
|
||||
} else {
|
||||
fmt.Printf("El número %v es impar\n", num)
|
||||
}
|
||||
}
|
||||
|
||||
func isEven(n int) bool {
|
||||
if n%2 == 0 {
|
||||
return true
|
||||
} else {
|
||||
return false
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue