Please note, this is a STATIC archive of website www.w3resource.com from 19 Jul 2022, cach3.com does not collect or store any user information, there is no "phishing" involved.
w3resource

C# Sharp exercises: Check whether a number is positive or negative


C# Sharp Conditional Statement: Exercise-3 with Solution

Write a C# Sharp program to check whether a given number is positive or negative.

Sample Solution:-

C# Sharp Code:

using System;  
public class Exercise3  
{  
    public static void Main()  
    {
    int num;
    Console.Write("\n\n");
    Console.Write("Check whether a number is positive or negative:\n");
    Console.Write("----------------------------------------------");
    Console.Write("\n\n");
    Console.Write("Input an integer : ");
    num= Convert.ToInt32(Console.ReadLine());
    if (num >= 0)

 Console.WriteLine("{0} is a positive number.\n",num);
    else
        Console.WriteLine("{0} is a negative number. \n", num);
}
}

Sample Output:

Check whether a number is positive or negative:                                                               
----------------------------------------------
Input an integer : -10                                                                                        
-10 is a negative number.

Pictorial Presentation:

Conditional Statement: Check whether a number is positive or negative.

Flowchart:

Flowchart: Check whether a number is positive or negative.

C# Sharp Code Editor:

Contribute your code and comments through Disqus.

Previous: Write a C program to check whether a given number is even or odd.
Next: Write a C# Sharp program to find whether a given year is a leap year or not.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.