Simple program #14 (Source code to check input is a positive or negative or neutral number)
Enter a number: 64
Number is positive.
The program is written in C#
programming language and will accept a number as input. The logic of the
program is to check whether it is positive or negative or neutral (neither
positive nor negative).
Let’s find out a simple and easy way
to code the program.
Practical Implementation:
using System;
namespace patternProblem.Simple_program
//checking input is a number (Positive or negative or
neutral)
{
class Simple14
{
public static void Main()
{
try
{
Console.Write("Enter a number: ");
double num = Convert.ToDouble(Console.ReadLine());
if (num > 0)
{
Console.WriteLine("Number is positive.");
}
else if (num < 0)
{
Console.WriteLine("Number is negative.");
}
else
{
Console.WriteLine("Number 0 is neutral.");
}
}
catch (FormatException)
{
Console.WriteLine("Input is not a numbers. It
might be an alphabet or special character.");
}
catch (OverflowException)
{
Console.WriteLine("Input number is exceeding
its maximum range.");
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
}
}
Output:
The input
number is 64. The program will check whether input number is positive/
negative/ neutral number. The output is shown below:
Enter a number: 64
Number is positive.
Press any key to continue . . .
For any
query, comment us below.
Skip to
Main Table Simple program
Keep learning and sharing...
No comments:
Post a Comment