Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I check if a number is positive or negative in C#?

Tags:

c#

How do I check if a number is positive or negative in C#?

like image 287
user9969 Avatar asked Nov 04 '10 17:11

user9969


People also ask

How do you check if an integer is positive or negative?

If the Integer is greater than zero then it is a positive integer. If the number is less than zero then it is a negative integer. If the number is equal to zero then it is neither negative nor positive.

Is 0 a positive number in C?

If the number is zero then it is neither positive nor negative. Same logic we have followed in the below C program.

How do you check that a number is positive and even in C?

In the program, the integer entered by the user is stored in the variable num . Then, whether num is perfectly divisible by 2 or not is checked using the modulus % operator. If the number is perfectly divisible by 2 , test expression number%2 == 0 evaluates to 1 (true). This means the number is even.


2 Answers

bool positive = number > 0; bool negative = number < 0; 
like image 117
Simon Fischer Avatar answered Sep 27 '22 23:09

Simon Fischer


Of course no-one's actually given the correct answer,

num != 0   // num is positive *or* negative! 
like image 22
tjm Avatar answered Sep 28 '22 01:09

tjm