C# Program to check if a number is Positive, Negative, Odd, Even, Zero

CsharpProgrammingServer Side Programming

Check for the following conditions −

For odd and even, check for the remainder when the number is divided by 2 −

// checking for odd/ even
if(n % 2 == 0) {
   Console.WriteLine("Even");
} else {
   Console.WriteLine("Odd");
}

For positive, negative and checking whether a number is 0 or not −

if (n < 0) {
Console.WriteLine("Negative Number!");
} else if(n == 0) {
   Console.WriteLine("Zero");
} else {
   Console.WriteLine("Positive Number!");
}

The following is the complete code:

Example

 Live Demo

using System;
using System.Collections.Generic;

public class Demo {
   public static void Main(string[] args) {
      int n = 19;

      // checking for odd/ even
      if (n % 2 == 0) {
         Console.WriteLine("Even");
      } else {
         Console.WriteLine("Odd");
      }

      // checking for positive, negative or 0
      if (n < 0) {
         Console.WriteLine("Negative Number!");
      } else if (n == 0) {
         Console.WriteLine("Zero");
      } else {
         Console.WriteLine("Positive Number!");
      }
   }
}

Output

Odd
Positive Number!
raja
Published on 31-Aug-2018 11:55:27
Advertisements