C sharp program to check if a number is positive, negative or zero

Check if a number is positive or negative in C# :

In this C# tutorial, we will learn how to check if a number is positive, negative or zero. The program will ask the user to enter a number. It will check if it is positive, negative or zero and finally print out the result.

With this tutorial, you will learn how to compare a number using if-else condition, how to take user input and how to write a message to the user.

C# program :

using System;
namespace dotnet_sample
    class Program
        static void Main(string[] args)
            int n;
            Console.WriteLine("Enter a number : ");
            n = int.Parse(Console.ReadLine());
            if(n == 0)
                Console.WriteLine(n + " is zero.");
            else if(n > 0)
                Console.WriteLine(n + " is a positive number.");
                Console.WriteLine(n + " is a negative number.");

Explanation :

The commented numbers in the above program denote the step numbers below :

  1. Create one integer n to read the user input.
  2. Ask the user to enter a number.
  3. Read the number and store it in_ ā€˜nā€™._
  4. Check if the value of_ ā€˜nā€™_ is 0 or not. If yes, print out that it is zero.
  5. Else check if it is greater than 0 or not. If yes, print out that it is a positive number.
  6. Else print that it is a negative number.

Sample Output :

Enter a number :
0 is zero.

Enter a number :
-23 is a negative number.

Enter a number :
45 is a positive number.

c sharp check positive negative number example

This program is available on Github

Where is the color and why codevscolor ?

Long story short, I love paintings and I paint on weekends. We(me and my wife) have one Youtube channel. Below is a video that I did recently. If you love this please do subscribe to support us šŸ˜Š