C++ Program to check whether a number is positive or negative

Program to Check whether a number is Positive or Negative

 

Here we will discuss how to check whether a number is positive or negative in C++ programming language.

  • A number is positive if it is greater than 0
    • N > 0, number is positive. 
  • To check this we use if else statement
  • If the condition is true then the number is positive
  • Otherwise the number is negative
    • N< 0, number is negative.

Note: 0 is neither positive nor negative   

Program to check whetehr a Number is positive or negative

Working:

  • User gives an input
  • Input is stored in an int type variable say prep
  • prep is first checked for being 0.

                                  if(prep==0) 

  • prep is then checked for being greater than 0.

                                  if(prep>0)

  • If prep is greater than 0 then the input is a positive number.
  • Otherwise the number is a negative number.

C++ Code:

//C++ Program
// number is positive or negative
#include<iostream>
using namespace std;
int main()
{
    cout<<"Enter a number: ";
    int check;
    cin>>check;
    //input is 0
if(check==0)
    {
        cout<<"0 is neither positive nor negative";
}
//checking whether the number is positive or negative
    else if(check>0)
    {
        cout<<check<<" is a positive number";
    }
    else
    {
        cout<<check<<" is a negative number";
    }
    return 0;

Output:

Enter a number: 96
96 is a positive number

Please Login/Signup to comment