- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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.
- Related Articles
- Java Program to Check Whether a Number is Positive or Negative
- Haskell Program to Check Whether a Number is Positive or Negative
- C++ Program to Check Whether a Number is Positive or Negative
- C program to Check Whether a Number is Positive or Negative or Zero?
- How to check if a number is positive, negative or zero using Python?
- Python Program to Check if a Number is Positive, Negative or 0
- Check whether product of integers from a to b is positive, negative or zero in Python
- Check if a number is positive, negative or zero using bit operators in C++
- Golang Program to check whether given positive number is power of 2 or not, without using any branching or loop
- How to check whether the input number is a Neon Number in Golang?
- Write a Golang program to check whether a given number is prime number or not
- Java Menu Driven Program to Check Positive Negative or Odd Even Number
- Program to check if a number is Positive, Negative, Odd, Even, Zero?
- How to check whether a number is a prime number or not?
- Write a Golang program to check whether a given number is a palindrome or not
