
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
C++ Program to Check Whether a Number is Positive or Negative
In modern programming languages we work with signed numbers and unsigned numbers also. For signed numbers the numbers can be positive or negative or zero. To represent negative numbers, the systems store the numbers in 2’s complement method. In this article we shall discuss how to determine a given number is positive, or negative in C++.
Checking using if-else conditions
The basic sign checking can be done by using if else conditions. The syntax for the if-else conditions is like below −
Syntax
if <condition> { perform action when condition is true } else { perform action when condition is false }
Algorithm
Determining positive or negative numbers the algorithm will be like below −
- take a number input n
- if n < 0, then
- return n as negative number
- otherwise
- return n as positive number
Example
#include <iostream> using namespace std; string solve( int n ) { if( n < 0 ) { return "Negative"; } else { return "Positive"; } } int main() { cout << "The 10 is positive or negative? : " << solve( 10 ) << endl; cout << "The -24 is positive or negative? : " << solve( -24 ) << endl; cout << "The 18 is positive or negative? : " << solve( 18 ) << endl; cout << "The -80 is positive or negative? : " << solve( -80 ) << endl; }
Output
The 10 is positive or negative? : Positive The -24 is positive or negative? : Negative The 18 is positive or negative? : Positive The -80 is positive or negative? : Negative
Checking using ternary operators
We can remove if-else conditions by using ternary operators. The ternary operator uses two symbols ‘?’ and ‘:’. The algorithm is similar. The syntax for the ternary operators are like below −
Syntax
<condition> ? <true case> : <false case>
Example
#include <iostream> using namespace std; string solve( int n ) { string res; res = ( n < 0 ) ? "Negative" : "Positive"; return res; } int main() { cout << "The 56 is positive or negative? : " << solve( 56 ) << endl; cout << "The -98 is positive or negative? : " << solve( -98 ) << endl; cout << "The 45 is positive or negative? : " << solve( 45 ) << endl; cout << "The -158 is positive or negative? : " << solve( -158 ) << endl; }
Output
The 56 is positive or negative? : Positive The -98 is positive or negative? : Negative The 45 is positive or negative? : Positive The -158 is positive or negative? : Negative
Conclusion
Checking a given integer is positive or negative in C++ takes is a basic condition checking problem, where we check the given number is less than zero or not, if so, then the number is negative, otherwise it is positive. This can be extended into negative, zero and positive checking by using else-if condition. The similar approach can be used by using ternary operators. In this article we have discussed them both with few examples.
- 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 or Zero?
- How to check whether a number is Positive or Negative in Golang?
- Python Program to Check if a Number is Positive, Negative or 0
- 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 if a number is positive, negative or zero using Python?
- C# Program to check if a number is Positive, Negative, Odd, Even, Zero
- Check whether product of integers from a to b is positive, negative or zero in Python
- Java program to find if the given number is positive or negative
- Check if a number is positive, negative or zero using bit operators in C++
- C++ Program to Check Whether a Number is Prime or Not
- C++ Program to Check Whether a Number is Palindrome or Not
- C Program to Check Whether a Number is Prime or not?
