ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

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

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.

Program/Source Code:

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 »



ADVERTISEMENT



ADVERTISEMENT



Comments and Discussions

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.