The Program in C# Program to Check If Given number is Positive or Negative is given below:
using System;
namespace PositiveNegativeCheck
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello Codeauri Family,enter a number here to know whether the numbe is positive or negarive: ");
int num = Convert.ToInt32(Console.ReadLine());
if (num > 0)
{
Console.WriteLine("Okay,the number is positive.");
}
else if (num < 0)
{
Console.WriteLine("Okay,the number is negative.");
}
else
{
Console.WriteLine("The number is zero.");
}
Console.ReadLine();
}
}
}
Output:
Hello Codeauri Family,enter a number here to know whether the numbe is positive or negarive:
10
Okay,the number is positive.
Pro-Tips💡
Here are the step by step execution of above program:
using System;
: This line includes the System namespace, which contains various classes and methods used in the program.namespace PositiveNegativeCheck
: A namespace is used to organize and group classes in a logical manner.class Program
: A class is a blueprint that defines the properties and methods of an object.static void Main(string[] args)
: The Main method is the entry point of the program and is executed first when the program runs.Console.WriteLine("Hello Codeauri Family,enter a number here to know whether the numbe is positive or negarive: ");
: This line displays a message on the console asking the user to enter a number.int num = Convert.ToInt32(Console.ReadLine());
: This line reads a string from the console and converts it to an integer.if (num > 0)
: This checks if the number is greater than 0. If the condition is true, the code inside the if block is executed.else if (num < 0)
: This checks if the number is less than 0. If the previous if condition was false, and this condition is true, the code inside the else if block is executed.else
: If both the previous conditions are false, the code inside the else block is executed.Console.ReadLine();
: This line is used to keep the console open so that the output can be read.
Learn C-Sharp ↗
C-sharp covers every topic to learn about C-Sharp thoroughly.
Learn C Programming ↗
C-Programming covers every topic to learn about C-Sharp thoroughly.
Learn C++ Programming↗
C++ covers every topic to learn about C-Sharp thoroughly.