Section9-complete
This commit is contained in:
@@ -6,16 +6,37 @@ nums := [...]int{30, -1, -6, 90, -6}
|
|||||||
|
|
||||||
Write a Go program that counts the number of positive even numbers
|
Write a Go program that counts the number of positive even numbers
|
||||||
in the array.
|
in the array.
|
||||||
*/
|
|
||||||
|
|
||||||
package main
|
package main
|
||||||
|
|
||||||
|
import "fmt"
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
nums := [...]int{30, -1, -6, 90, -6}
|
nums := [...]int{30, -1, -6, 90, -6}
|
||||||
|
|
||||||
for i := 0; i < len(nums); i++ {
|
for i := 0; i < len(nums); {
|
||||||
if i > 0 && i%2 {
|
if i%2 == 0 && i > 0 {
|
||||||
|
i++
|
||||||
|
}
|
||||||
|
fmt.Println("No. of positive even numbers in nums: ", i)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
*/
|
||||||
|
package main
|
||||||
|
|
||||||
|
import "fmt"
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
nums := []int{30, -1, -6, 90, -6}
|
||||||
|
|
||||||
|
var count int
|
||||||
|
|
||||||
|
for _, v := range nums {
|
||||||
|
if v%2 == 0 && v > 0 {
|
||||||
|
count++
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.Println("No. of positive even numbers in nums: ", count)
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|||||||
42
Section9/76-3.go
Normal file
42
Section9/76-3.go
Normal file
@@ -0,0 +1,42 @@
|
|||||||
|
/*
|
||||||
|
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() {
|
||||||
|
myArray := [3]float64{1.2, 5.6}
|
||||||
|
|
||||||
|
myArray[2] = 6
|
||||||
|
|
||||||
|
a := 10
|
||||||
|
myArray[0] = a
|
||||||
|
|
||||||
|
myArray[3] = 10.10
|
||||||
|
|
||||||
|
fmt.Println(myArray)
|
||||||
|
|
||||||
|
}
|
||||||
|
*/
|
||||||
|
|
||||||
|
package main
|
||||||
|
|
||||||
|
import "fmt"
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
myArray := [3]float64{1.2, 5.6}
|
||||||
|
|
||||||
|
myArray[2] = 6
|
||||||
|
|
||||||
|
a := 10
|
||||||
|
myArray[0] = float64(a)
|
||||||
|
|
||||||
|
myArray[2] = 10.10
|
||||||
|
|
||||||
|
fmt.Println(myArray)
|
||||||
|
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user