Please note, this is a STATIC archive of website www.w3resource.com from 19 Jul 2022, cach3.com does not collect or store any user information, there is no "phishing" involved.
w3resource

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:

C++ Exercises: Check whether a number is positive, negative or zero

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:

Flowchart: Check whether a number is positive, negative or zero

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?