The goal of this second day is the following:
- Declare 3 variables: an int, a double and a string.
- Read 3 lines from stdin and save them in the variables you just defined
- Do some operations with the pre-defined $i$, $d$ and $s$ variables
- Print the sum of the integer $i$ with your integer variable
- Print the sum of the float $d$ with your double variable with one decimal place
- Concatenate $s$ with your string and print the result
My solution was the following
package main
import (
"bufio"
"fmt"
"os"
)
func main() {
var i uint32 = 4
var d float32 = 4.0
var s string = "HackerRank "
scanner := bufio.NewReader(os.Stdin)
// Declaring my 3 variables
var my_i uint32
var my_d float64
var my_s string
fmt.Scanln(&my_i)
fmt.Scanln(&my_d)
// notice: I can't use "\n" (double quotes)
//Pay attention to use ReadString, not Readstring
my_s, _ = scanner.ReadString('\n')
fmt.Println(i + my_i)
// I tried ((float64) d) first (like C), but didn't work
fmt.Printf("%.1f\n", float64(d)+my_d) //notice printf to use decimal place
fmt.Println(s + my_s)
}
Well, this one got me on some points and I wrote about them in the comments.
Some of my mistakes from yesterday helped to use write Scanln
.
That’s all for today! Thanks for reading!