Home C++ Check if the number is Positive or Negative in C++

Check if the number is Positive or Negative in C++

In this example we will check if a number is positive or negative in C++

Example

We use an if -else statement in this example after taking input from the user

#include <iostream>
using namespace std;

int main()
{
    cout << "number is positive or negative\n\n";

    int myNumber;

    //taking user input
    cout << "Enter a non-zero Number: ";
    cin >> myNumber;

    //display output depending on result
    if (myNumber > 0)
    {
        cout << "\nEntered number is positive";
    }
    else
    {
        cout << "\nEntered number is negative";
    }

    cout << "\n";

    return 0;
}

When you run this you will see something like this

number is positive or negative

Enter a non-zero Number: 6

Entered number is positive
number is positive or negative

Enter a non-zero Number: -2

Entered number is negative

You may also like