Section5.48
This commit is contained in:
194
Section5/48.go
Normal file
194
Section5/48.go
Normal file
@@ -0,0 +1,194 @@
|
|||||||
|
/*
|
||||||
|
Coding Exercise #1
|
||||||
|
|
||||||
|
Consider the following declarations:
|
||||||
|
|
||||||
|
var i int = 3
|
||||||
|
var f float64 = 3.2
|
||||||
|
|
||||||
|
Write a Go program that converts i to float64 and f to int.
|
||||||
|
|
||||||
|
Print out the type and the value of the variables created after conversion.
|
||||||
|
|
||||||
|
|
||||||
|
package main
|
||||||
|
|
||||||
|
import "fmt"
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
var i int = 3
|
||||||
|
var f float64 = 3.2
|
||||||
|
|
||||||
|
fmt.Printf("Type of i is %T and f is %T\n", i, f)
|
||||||
|
|
||||||
|
i1 := float64(i)
|
||||||
|
f1 := int(f)
|
||||||
|
fmt.Printf("Type of i1 is %T and value is %f\n", i1, i1)
|
||||||
|
fmt.Printf("Type of f1 is %T and value is %d\n", f1, f1)
|
||||||
|
}
|
||||||
|
*/
|
||||||
|
|
||||||
|
/*
|
||||||
|
Coding Exercise #2
|
||||||
|
|
||||||
|
Consider the following declarations:
|
||||||
|
|
||||||
|
var i = 3
|
||||||
|
var f = 3.2
|
||||||
|
var s1, s2 = "3.14", "5"
|
||||||
|
|
||||||
|
Write a Go program that converts:
|
||||||
|
|
||||||
|
1. i to string (int to string)
|
||||||
|
|
||||||
|
2. s2 to int (string to int)
|
||||||
|
|
||||||
|
3. f to string (float64 to string)
|
||||||
|
|
||||||
|
4. s1 to float64 (string to float64)
|
||||||
|
|
||||||
|
5. Print the value and the type for each variable created after conversion.
|
||||||
|
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"strconv"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
var i = 3
|
||||||
|
var f = 3.2
|
||||||
|
var s1, s2 = "3.14", "5"
|
||||||
|
|
||||||
|
// 1. int to string
|
||||||
|
s := strconv.Itoa(i)
|
||||||
|
fmt.Printf("s Type is %T, s value is %q\n", s, s)
|
||||||
|
|
||||||
|
// 2. string to int
|
||||||
|
is, err := strconv.Atoi(s2)
|
||||||
|
if err == nil {
|
||||||
|
fmt.Printf("i type is %T, i value is %v\n", is, is)
|
||||||
|
} else {
|
||||||
|
fmt.Println("Can not convert string to int.")
|
||||||
|
}
|
||||||
|
|
||||||
|
// 3. float64 to string
|
||||||
|
ss1 := fmt.Sprintf("%f", f)
|
||||||
|
fmt.Printf("ss1's type: %T, ss1's value: %s\n", ss1, ss1)
|
||||||
|
|
||||||
|
// 4. string to float64
|
||||||
|
f1, err1 := strconv.ParseFloat(s1, 64)
|
||||||
|
if err1 == nil {
|
||||||
|
fmt.Printf("f1's type: %T, f1's value: %v\n", f1, f1)
|
||||||
|
} else {
|
||||||
|
fmt.Println("Cannot convert string to float64.")
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
*/
|
||||||
|
|
||||||
|
/*
|
||||||
|
Coding Exercise #3
|
||||||
|
|
||||||
|
There are some errors in the following Go program. Try to identify the errors, change the code and run the program without errors.
|
||||||
|
|
||||||
|
package main
|
||||||
|
|
||||||
|
import "fmt"
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
x, y := 4, 5.1
|
||||||
|
|
||||||
|
z := x * y
|
||||||
|
fmt.Println(z)
|
||||||
|
|
||||||
|
a := 5
|
||||||
|
b := 6.2 * a
|
||||||
|
fmt.Println(b)
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
package main
|
||||||
|
|
||||||
|
import "fmt"
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
x, y := 4, 5.1
|
||||||
|
|
||||||
|
z := float64(x) * y
|
||||||
|
fmt.Println(z)
|
||||||
|
|
||||||
|
a := 5
|
||||||
|
b := 6.2 * float64(a)
|
||||||
|
fmt.Println(b)
|
||||||
|
|
||||||
|
}
|
||||||
|
*/
|
||||||
|
|
||||||
|
/*
|
||||||
|
Coding Exercise #4
|
||||||
|
|
||||||
|
Create a Go program that computes how long does it take for the Sunlight to reach the Earth if we know that the distance from the Sun to Earth is 149.6 million km and the speed of light is 299,792,458 m / s
|
||||||
|
|
||||||
|
The formula used is: time = distance / speed
|
||||||
|
|
||||||
|
package main
|
||||||
|
|
||||||
|
import "fmt"
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
d := 149600000 * 1000
|
||||||
|
s := 299792458
|
||||||
|
fmt.Printf("Speed of light is %d\n", s)
|
||||||
|
fmt.Printf("Distance from Sun to Earth is %d\n", d)
|
||||||
|
t := d / s
|
||||||
|
fmt.Printf("Sunlight reaches Earth in %v seconds\n", t)
|
||||||
|
}
|
||||||
|
*/
|
||||||
|
|
||||||
|
/*
|
||||||
|
|
||||||
|
Coding Exercise #5
|
||||||
|
|
||||||
|
Write the correct logical operator (&&, || , !) inside the expression so that result1 will be false and result2 will be true.
|
||||||
|
|
||||||
|
Program source code: https://play.golang.org/p/74SCleChC20
|
||||||
|
|
||||||
|
package main
|
||||||
|
|
||||||
|
import "fmt"
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
x, y := 0.1, 5
|
||||||
|
var z float64
|
||||||
|
|
||||||
|
// Write the correct logical operator (&&, || , !)
|
||||||
|
// inside the expression so that result1 will be false and result2 will be true.
|
||||||
|
|
||||||
|
result1 := x < z int(x) != int(z)
|
||||||
|
fmt.Println(result1)
|
||||||
|
|
||||||
|
result2 := y == 1 * 5 int(z) == 0
|
||||||
|
fmt.Println(result2)
|
||||||
|
|
||||||
|
}
|
||||||
|
*/
|
||||||
|
package main
|
||||||
|
|
||||||
|
import "fmt"
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
x, y := 0.1, 5
|
||||||
|
var z float64
|
||||||
|
|
||||||
|
// Write the correct logical operator (&&, || , !)
|
||||||
|
// inside the expression so that result1 will be false and result2 will be true.
|
||||||
|
|
||||||
|
result1 := x < z || int(x) != int(z)
|
||||||
|
fmt.Println(result1)
|
||||||
|
|
||||||
|
result2 := y == 1 * 5 || int(z) == 0
|
||||||
|
fmt.Println(result2)
|
||||||
|
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user