How do I check if a number is positive or negative in C#?
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.
If the number is zero then it is neither positive nor negative. Same logic we have followed in the below C program.
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.
bool positive = number > 0; bool negative = number < 0;
Of course no-one's actually given the correct answer,
num != 0 // num is positive *or* negative!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With