Complete the code to print "Even" if the number is divisible by 2.
package main import "fmt" func main() { num := 4 if num [1] 2 == 0 { fmt.Println("Even") } }
The % operator gives the remainder. If num % 2 == 0, the number is even.
Complete the code to print "Positive" if the number is greater than zero.
package main import "fmt" func main() { num := 10 if num [1] 0 { fmt.Println("Positive") } }
The operator > checks if num is greater than zero.
Fix the error in the if–else statement to print "Odd" when the number is not divisible by 2.
package main import "fmt" func main() { num := 7 if num % 2 == 0 { fmt.Println("Even") } [1] { fmt.Println("Odd") } }
In Go, the correct keyword for the alternative block is else, not elseif or others.
Complete the code to check if a number is negative or zero and print the correct message.
package main import "fmt" func main() { num := -3 if num {BLANK_1}} 0 { { fmt.Println("Negative or zero") } else { fmt.Println("Positive") } }
The condition num <= 0 checks if the number is negative or zero. The opening brace { starts the block.
Fill all three blanks to create a nested if–else that prints "Positive even", "Positive odd", or "Non-positive".
package main import "fmt" func main() { num := 8 if num [1] 0 { if num [2] 2 [3] 0 { fmt.Println("Positive even") } else { fmt.Println("Positive odd") } } else { fmt.Println("Non-positive") } }
The outer if checks if num > 0. The inner if checks if num % 2 == 0 to find even numbers.