Home >>VB.Net Programs >VB.Net program to find the sign of a specified number using Math.Sign() method

VB.Net program to find the sign of a specified number using Math.Sign() method

Write a VB.Net program to find the sign of a specified number using Math.Sign() method

Here, we can find the sign of a given number using the Sign() method of Math class and then print the appropriate message according to the sign on the console screen.

Program :

The source code used by the Math.Sign() method to find a sign of a specified number is given below. The program given is compiled and successfully executed.

Example


Module Module1
    Sub Main()
        Dim num As Integer = 0
        Dim result As Integer = 0
        Console.Write("Enter number: ")
        num = Integer.Parse(Console.ReadLine())
        result = Math.Sign(num)
        If result < 0 Then
            Console.WriteLine("Number is negative")
        ElseIf result > 0 Then
            Console.WriteLine("Number is positive")
        Else
            Console.WriteLine("Number is zero")
        End If
    End Sub
End Module
	
Output:
Enter number: -20
Number is negative

Explanation:

We created a Module1 module in the above program that contains a method Main(). We have created two num and result variables in the Main() method, which are initialized with 0.

Console.Write("Enter number: ")
num = Integer.Parse(Console.ReadLine())

Here, we read the value of variable num from user.

result = Math.Sign(num)
If result < 0 Then
    Console.WriteLine("Number is negative")
ElseIf result > 0 Then
    Console.WriteLine("Number is positive")
Else
    Console.WriteLine("Number is zero")
End If

We checked the variable num sign here, where the Sign() method will return three values.

  • -1 : for Negative value
  • +1 : for positive value
  • 0 : for zero

After that, we printed an appropriate message on the console screen using If.


No Sidebar ads