By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,898 Members | 2,022 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,898 IT Pros & Developers. It's quick & easy.

Checking a number for positive or negative value

sonic
P: 40
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.
Nov 6 '06 #1
Share this Question
Share on Google+
2 Replies


sicarie
Expert Mod 2.5K+
P: 4,677
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.
Nov 7 '06 #2

seeminsuleri
P: 9
hey there,
try the following code
Expand|Select|Wrap|Line Numbers
  1. #include<stdio.h>
  2. #include<conio.h>
  3. using namespace std;
  4.  
  5. void main()
  6. {
  7. int x;
  8. cout<<"Enter value"<<endl;
  9. cin>>x;
  10. if(x<0)
  11. {
  12. cout<<"Number Negative"<<endl;
  13. }
  14. else
  15. {
  16. if(x>0)
  17. {
  18. cout<<"Number Positive"<<endl;
  19. }
  20. }
  21. else 
  22. cout<<"Number Equal to Zero"<<endl;
  23. return 0;
  24. }
  25. }
  26.  
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.
Nov 7 '06 #3

Post your reply

Sign in to post your reply or Sign up for a free account.