Checking a number for positive or negative value

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sonic
    New Member
    • Nov 2006
    • 40

    Checking a number for positive or negative value

    I am fairly new to C++. I was wondering how would one go about checking to see if a value entered by the user was negative or positive. Is this something you might use modulo(%) math for?

    Thank you for any that may respond.
  • sicarie
    Recognized Expert Specialist
    • Nov 2006
    • 4677

    #2
    sonic-

    A check to see if an integer is negative is the same that you would do logically - is it less than zero?

    if (x < 0) {
    cout << "It's negative!" << endl;
    }

    Modulus is a bit different, it's an actual function, and it is the remainder after division, so if you do x % 5, it would be the remaind3er after 5 was divided into x.

    Comment

    • seeminsuleri
      New Member
      • Nov 2006
      • 9

      #3
      hey there,
      try the following code
      Code:
      #include<stdio.h>
      #include<conio.h>
      using namespace std;
      
      void main()
      {
      int x;
      cout<<"Enter value"<<endl;
      cin>>x;
      if(x<0)
      {
      cout<<"Number Negative"<<endl;
      }
      else
      {
      if(x>0)
      {
      cout<<"Number Positive"<<endl;
      }
      }
      else 
      cout<<"Number Equal to Zero"<<endl;
      return 0;
      }
      }
      Originally posted by sonic
      I am fairly new to C++. I was wondering how would one go about checking to see if a value entered by the user was negative or positive. Is this something you might use modulo(%) math for?

      Thank you for any that may respond.

      Comment

      Working...