VB.Net program to check EVEN/ODD using bitwise operators

Here, we are going to learn how to check EVEN/ODD using bitwise operators in VB.Net?
Submitted by Nidhi, on November 26, 2020 [Last updated : February 18, 2023]

Checking Even/Odd using Bitwise Operators

Here, we will read an integer number and check input number is EVEN or ODD using bitwise operators.

VB.Net code to check EVEN/ODD using bitwise operators

The source code to check EVEN/ODD using bitwise operators is given below. The given program is compiled and executed successfully.

'VB.Net program to check EVEN/ODD using bitwise operators.

Module Module1

    Sub Main()
        Dim num As Integer = 0
        
        Console.Write("Enter the number: ")
        num = Integer.Parse(Console.ReadLine())

        'Result of num&1 is 0000000d where d is your LSB.
        '   - LSB is 1 for odd number
        '   - LSB is 0 for even number
        If ( (num And 1) = 1) Then
            Console.WriteLine("Given number is ODD")
        Else
            Console.WriteLine("Given number is EVEN")
        End If

        Console.ReadLine()
    End Sub

End Module

Output:

Enter the number: 7
Given number is ODD

Explanation:

In the above program, we created a module Module1 that contains a function Main(). In the Main() function, we read the integer number and then find the given number is EVEN or ODD using bitwise operators.

'Result of num&1 is 0000000d where d is your LSB.
'   - LSB is 1 for odd number
'   - LSB is 0 for even number
If ( (num And 1) = 1) Then
    Console.WriteLine("Given number is ODD")
Else
    Console.WriteLine("Given number is EVEN")
End If

The above code is used to check the given number is EVEN or ODD and print the appropriate message on the console screen.

VB.Net Basic Programs »





Comments and Discussions!

Load comments ↻





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