C++ Exercises: Check whether a number is positive, negative or zero
C++ Basic: Exercise-32 with Solution
Write a program in C++ to check whether a number is positive, negative or zero.
Pictorial Presentation:
Sample Solution:
C++ Code :
#include <iostream>
using namespace std;
int main()
{
signed long num1 = 0;
cout << "\n\n Check whether a number is positive, negative or zero :\n";
cout << "-----------------------------------------------------------\n";
cout << " Input a number : ";
cin >> num1;
if(num1 > 0)
{
cout << " The entered number is positive.\n\n";
}
else if(num1 < 0)
{
cout << " The entered number is negative.\n\n";
}
else
{
std::cout << " The number is zero.\n\n";
}
return 0;
}
Sample Output:
Check whether a number is positive, negative or zero : ----------------------------------------------------------- Input a number : 8 The entered number is positive.
Flowchart:
C++ Code Editor:
Contribute your code and comments through Disqus.
Previous: Write a program in C++ to input a single digit number and print a rectangular form of 4 columns and 6 rows.
Next: Write a program in C++ to divide two numbers and print on the screen.
What is the difficulty level of this exercise?