The simplest decision compares a number with zero.
What to build
A program that says whether each number is negative, zero or positive.
Requirements
- Read n, then n whole numbers.
- For each number print
negative,zeroorpositive.
Input
Line 1: n. Then n whole numbers.
Output
n lines.
Constraints
1 ≤ n ≤ 50; each number is between -1000000 and 1000000.
Example
Input:
3
-5 0 8
Output:
negative
zero
positive
Why it works
Exactly one of the three conditions is true for any number, so an if, else if, else chain prints exactly one word.
Examples
Example 1 · Input
3 -5 0 8
Expected output
negative zero positive
Example 2 · Input
2 7 -1
Expected output
positive negative
Hints
Hint 1
Use if, else if and else on each number.
Hint 2
Read n first, then read each number inside a loop.
Solution
Show one correct solution (try it yourself first)
package main
import "fmt"
func main() {
var n int
fmt.Scan(&n)
for i := 0; i < n; i++ {
var x int
fmt.Scan(&x)
if x < 0 {
fmt.Println("negative")
} else if x == 0 {
fmt.Println("zero")
} else {
fmt.Println("positive")
}
}
}