Positive or Negative Number Checker in C#

This C# program prompts the user to enter a number and checks if it is positive, negative, or zero. The program utilizes conditional statements to evaluate the number and display the corresponding result.

Source Code 💻

using System;

class Program
{
    static void Main()
    {
        // Prompt user to enter a number
        Console.Write("Enter a number: ");
        double number = Convert.ToDouble(Console.ReadLine());

        // Check if the number is positive, negative, or zero
        if (number > 0)
        {
            Console.WriteLine($"{number} is a positive number.");
        }
        else if (number < 0)
        {
            Console.WriteLine($"{number} is a negative number.");
        }
        else
        {
            Console.WriteLine("The number is zero.");
        }
    }
}

Explanation

  • The program prompts the user to enter a number.
  • It checks whether the number is greater than 0, less than 0, or exactly 0 using if, else if, and else conditions.
    • If the number is greater than 0, it’s positive.
    • If the number is less than 0, it’s negative.
    • If the number is 0, it outputs “The number is zero.”
  • The result is then printed accordingly.

🔚 end of document 🔚