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

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

Write a Program to check EVEN/ODD using bitwise operators in VB.Net

We can read an integer number here and check the input number using bitwise operators to be EVEN or ODD.

Program :

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

Example


	Module Module1
    Sub Main()
        Dim num As Integer = 0
        Console.Write("Enter Your 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:

We created a Module1 module in the above program that contains the function Main(). We read the integer number in the Main() function and then find the number given 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 if the given number is EVEN or ODD and to print the corresponding message on the screen of the console.


No Sidebar ads