VB.Net program to print the binary number of a decimal number

Here, we are going to learn how to print the binary number of a decimal number in VB.Net?
Submitted by Nidhi, on November 26, 2020 [Last updated : February 18, 2023]

Printing the binary number of a decimal number

Here we will print the binary number of a decimal number using bitwise operators.

VB.Net code to print the binary number of a decimal number

The source code to print the binary number of a decimal number is given below. The given program is compiled and executed successfully.

'VB.Net program to print the binary number 
'of a decimal number.

Module Module1
    Sub Main()
        Dim iLoop As SByte
        Dim num As SByte

        Console.Write("Enter Number: ")
        num = Byte.Parse(Console.ReadLine())

        For iLoop = 7 To 0 Step -1
            If ((1 << iLoop) And num) Then
                Console.Write("1")
            Else
                Console.Write("0")
            End If
        Next

        Console.ReadLine()

    End Sub

End Module

Output:

Enter Number: 8
00001000

Explanation:

In the above program, we created a module Module1 that contains a Main() method. And, we created two local variables iLoop and num. After that, we entered a number and then we checked each bit of a given number using bitwise AND and bitwise left-shift operator and print the "1" for a high bit and print "0" for a low bit on the console screen.

VB.Net Basic Programs »






Comments and Discussions!

Load comments ↻






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