Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
C# Program to check if a number is Positive, Negative, Odd, Even, Zero
In C#, you can determine if a number is positive, negative, odd, even, or zero using simple conditional statements and arithmetic operations. This involves checking the sign of the number and using the modulus operator to determine if a number is divisible by 2.
Logic for Number Classification
To classify a number, we use the following approaches −
Positive/Negative/Zero: Compare the number with zero using relational operators.
Odd/Even: Use the modulus operator (%) to check if the remainder when divided by 2 is zero.
Syntax
Following is the syntax for checking positive, negative, or zero −
if (number < 0) {
// Negative
} else if (number == 0) {
// Zero
} else {
// Positive
}
Following is the syntax for checking odd or even −
if (number % 2 == 0) {
// Even
} else {
// Odd
}
Example
using System;
public class NumberClassification {
public static void Main(string[] args) {
int n = 19;
Console.WriteLine("Number: " + n);
// checking for odd/even
if (n % 2 == 0) {
Console.WriteLine("Even");
} else {
Console.WriteLine("Odd");
}
// checking for positive, negative or zero
if (n < 0) {
Console.WriteLine("Negative Number!");
} else if (n == 0) {
Console.WriteLine("Zero");
} else {
Console.WriteLine("Positive Number!");
}
}
}
The output of the above code is −
Number: 19 Odd Positive Number!
Testing with Different Numbers
Example
using System;
public class NumberTest {
public static void ClassifyNumber(int number) {
Console.Write("Number " + number + " is: ");
// Check sign
if (number < 0) {
Console.Write("Negative, ");
} else if (number == 0) {
Console.Write("Zero, ");
} else {
Console.Write("Positive, ");
}
// Check odd/even (zero is considered even)
if (number % 2 == 0) {
Console.WriteLine("Even");
} else {
Console.WriteLine("Odd");
}
}
public static void Main(string[] args) {
int[] testNumbers = {-15, -8, 0, 12, 7};
foreach (int num in testNumbers) {
ClassifyNumber(num);
}
}
}
The output of the above code is −
Number -15 is: Negative, Odd Number -8 is: Negative, Even Number 0 is: Zero, Even Number 12 is: Positive, Even Number 7 is: Positive, Odd
Using Methods for Reusable Code
Example
using System;
public class NumberUtility {
public static bool IsPositive(int number) {
return number > 0;
}
public static bool IsNegative(int number) {
return number < 0;
}
public static bool IsEven(int number) {
return number % 2 == 0;
}
public static bool IsOdd(int number) {
return number % 2 != 0;
}
public static void Main(string[] args) {
int number = -24;
Console.WriteLine("Number: " + number);
Console.WriteLine("Is Positive: " + IsPositive(number));
Console.WriteLine("Is Negative: " + IsNegative(number));
Console.WriteLine("Is Even: " + IsEven(number));
Console.WriteLine("Is Odd: " + IsOdd(number));
}
}
The output of the above code is −
Number: -24 Is Positive: False Is Negative: True Is Even: True Is Odd: False
Conclusion
Checking if a number is positive, negative, odd, even, or zero in C# involves using conditional statements and the modulus operator. The modulus operator (%) determines if a number is divisible by 2, while comparison operators check the sign relative to zero.
