Users Online

· Guests Online: 155

· Members Online: 0

· Total Members: 188
· Newest Member: meenachowdary055

Forum Threads

Newest Threads
No Threads created
Hottest Threads
No Threads created

Latest Articles

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

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

 

Comments

No Comments have been Posted.

Post Comment

Please Login to Post a Comment.

Ratings

Rating is available to Members only.

Please login or register to vote.

No Ratings have been Posted.
Render time: 1.07 seconds
10,815,065 unique visits