C# Program to Check Whether a Number is Positive or Not
Posted by Superadmin on August 10 2022 14:20:29

C# Program to Check Whether a Number is Positive or Not

 

This is a C# Program to accept a number from the user and display it if it is Positive.

Problem Description

This C# Program Accepts a Number from the user and Display it if it is Positive.

Problem Solution

Here if a given number is less than zero then it is displayed as positive else negative.

Program/Source Code

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();
    }
}
Program Explanation

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.

 
Runtime Test Cases
 
Enter a Number : -4 
Number is Negative