
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 If a Number Is Greater Than Zero in C++
Problem Description
In this problem, we are given a number and have to check whether this number is greater than zero. Using the if-else and ternary approach to check if the given number is greater than zero, equal to zero, or less than zero. In this article, we will discuss how we can find if a given number is greater than zero or not in C++.
Prerequisite
If-else operator: This operator allows the program to execute certain code blocks based on whether a condition is true or false.
Syntax of If-else operator
if (condition) { // Code to execute if the condition is true } else { // Code to execute if the condition is false }
Ternary Operator: This operator is also called a conditional operator. It is a compact form of an if-else statement.
Syntax of Ternary operator
condition ? expression1 : expression2;
Example 1
- Input: 4
- Output: True
Explanation
As 4 > 0, so the function will return true.Example 2
- Input: 0
- Output: False
Explanation
0 equals 0, not greater than it. So, this function will return false.Example 3
- Input: -3
- Output: False
Explanation
As 0 > -3, so this function will return false.Approaches to check if a number is greater than Zero
Using If-else Operator
This is a simple and direct approach. In this approach, we check if a number is greater than zero or not using the if-else operator. If the number is greater than zero, we print "Number is greater than zero". If the number is smaller than zero, we print "Number is less than zero". If the number equals zero, we print "Number is equal to zero".
Example 1
- Input: 5
- Output: The number is greater than zero
Explanation
As 5 > 0 the output is "Number is greater than zero"Implementation Code
#include<bits/stdc++.h>using namespace std; void checkNumber(int number) { if (number > 0) { cout << "The Number is greater than zero" << endl; } else if( number < 0) { cout << "The Number is less than zero" << endl; } else { cout << "The Number is equal to zero" << endl; } } int main() { int number = 5; checkNumber(number); return 0; }
Output
The number is greater than zero
Time Complexity: O(1)
Space Complexity: O(1)
Using Ternary Operator
The logic of this approach is the same as the above approach. This is also a simple approach to check if a number is greater than zero or not using a ternary operator. If the number exceeds zero, we print "Number is greater than zero". If the number is smaller than zero, we print "Number is less than zero". If the number equals zero, we print "Number is equal to zero".
Example
- Input: -3
- Output: Number is less than zero
Explanation
As -3 < 0, the output is "Number is less than zero"Implementation Code
#include<bits/stdc++.h> using namespace std; void checkNumber(int number) { string result = (number > 0) ? "The Number is greater than zero" : (number < 0) ? "The Number is less than zero" : "The Number is equal to zero"; cout << result << endl; } int main() { int number = -3; checkNumber(number); return 0; }
Output
The Number is less than zero
Time Complexity:O(1)
Space Complexity:O(1)