Enter a Number : -4 Number is Negative
This is a C# Program to accept a number from the user and display it if it is Positive.
This C# Program Accepts a Number from the user and Display it if it is Positive.
Here if a given number is less than zero then it is displayed as positive else negative.
Here is source code of the C# Program to Accept a Number from the user and Display it if it is Positive. The C# program is successfully compiled and executed with Microsoft Visual Studio. The program output is also shown below.
/* * C# Program to Accept a Number from the user and Display it * if it is Positive */ using System; class program { public static void Main(string[] args) { Console.WriteLine("Enter a number: "); int number = Convert.ToInt32(Console.ReadLine()); if (number > 0) { Console.WriteLine("Number is positive"); } else if (number == 0) { Console.WriteLine("Number is 0"); } else { Console.WriteLine("Number is negative"); } Console.ReadLine(); } }
In this C# program, we are reading a number using the ‘number’ variable. Nested if else condition statement is used to check that given number is less than zero. If the condition is true then execute the statement and print the statement as positive. Otherwise, if the condition is false, then execute the else statement and print the statement as the number is 0.
Enter a Number : -4 Number is Negative