How to check whether a number is Positive or Negative in Golang?


In this tutorial, we will learn how to check whether the number is positive or negative. This tutorial includes two ways to achieve this by using the built-in function Signbit() present in the math library. Another way is to use the relation operators using which we can compare a number with zero and predict whether the number is positive or not.

Method 1: Using Signbit() function

In this example, we are going to use the built-in function Signbit() present in the math library to check whether the number is positive or negative.

Syntax

Signbit() is a function in math library that accept a argument of float type as shown in below syntax.

func Signbit(f float64)

Algorithm

  • Step 1 − Initialize the float number.

  • Step 2 − Start an if condition and call Signbit() function in the if condition.

  • Step 3 − Print the result accordingly.

Example

package main
import (

   // fmt package provides the function to print anything
   "fmt"
   
   // Math library is providing Signbit() function
   "math"
)
func main() {

   // declaring and initializing the variable using the shorthand method in Golang
   number := -20.0
   fmt.Println("Golang program to check whether the number is positive or not using Signbit() function in the Math library.")
   
   // calling the Signbit() function and running the if else block accordingly
   if math.Signbit(number) {
      fmt.Printf("The number %f is negative.\n", number)
   } else {
      fmt.Printf("The number %f is positve.\n", number)
   }
}

Output

Golang program to check whether the number is positive or not using Signbit() function in the Math library.
The number -20.000000 is negative.

Method 2: Using Relational Operators

In this example, we will use the relational operator >= and < to check whether the number is positive or negative.

Syntax

This method is using relational operator <, >= with the below syntax.

if number < 0 {}
If number >= 0 {}

Algorithm

  • Step 1 − Initialize the float number.

  • Step 2 − Compare the number with the respective relational operator.

  • Step 3 − Print the result accordingly.

Example

package main
import (

   // fmt package provides the function to print anything
   "fmt"
)
func main() {

   // declaring and initializing the variable using the shorthand method in Golang
   number := 10.0
   fmt.Println("Golang program to check that the number is positive or not using the relational operators.")
   fmt.Println("Checking whether the number is positive or not using the > operator.")
   
   // using the > operator and running the if else block accordingly
   if number < 0 {
      fmt.Printf("The number %f is negative.\n", number)
   } else {
      fmt.Printf("The number %f is positve.\n", number)
   }
   fmt.Println("Checking whether the number is positive or not using the >= operator.")
   
   // using the >= operator and running the if else block accordingly
   if number >= 0 {
      fmt.Printf("The number %f is positve.\n", number)
   } else {
      fmt.Printf("The number %f is negative.\n", number)
   }
}

Output

Golang program to check that the number is positive or not using the relational operators.
Checking whether the number is positive or not using the > operator.
The number 10.000000 is positve.
Checking whether the number is positive or not using the >= operator.
The number 10.000000 is positve.

Conclusion

These are the two ways to check whether the number is positive or not. The first way is more suitable in terms of modularity and code reusability. To learn more about go you can explore these tutorials.

Updated on: 11-Jan-2023

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements