
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Check Whether a Number is Positive or Negative in C++
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.