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

Here, we are going to learn how to find the sign of a specified number using Math.Sign() method in VB.Net?
Submitted by Nidhi, on December 02, 2020 [Last updated : February 18, 2023]

Math.Sign() method in VB.Net

Here, we will 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.

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

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

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

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
Press any key to continue . . .

Explanation:

In the above program, we created a module Module1 that contains a method Main(). In the Main() method, we created two variables num and result, 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

Here, we checked the sign of variable num, here the Sign() method may return three values.

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

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

VB.Net Basic Programs »






Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.