Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Semrush - keyword research tool

Turn ChatGPT, Claude, Gemini, And CoPilot Into Your Personal Assistant, Business Coach, Content Creator, And More

AFFILIATE MARKETING Your all-in-one performance engine Manage affiliates, creators, and customer referrals in one unified platform—turning every partnership into measurable growth
Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

42,844 questions

55,671 answers

573 users

How to determine whether an n‑bit binary number is divisible by 5 in VB.NET

1 Answer

0 votes
Imports System

Module Program

    ' function to compute whether a binary number is divisible by 5
    ' it processes the bits left to right and keeps track of the remainder modulo 5
    ' for each bit b:
    '     remainder = (remainder * 2 + b) Mod 5
    Function IsDivisibleByFive(bin As String) As Boolean

        ' remainder modulo 5 while scanning bits
        Dim remainder As Integer = 0

        ' scan each bit of the binary number
        For Each bit As Char In bin

            ' convert "0" or "1" to integer 0 or 1
            Dim b As Integer = Convert.ToInt32(bit) - Convert.ToInt32("0"c)

            ' update remainder using modulo arithmetic
            remainder = (remainder * 2 + b) Mod 5
        Next

        ' divisible if final remainder is zero
        Return remainder = 0
    End Function


    Sub Main()

        ' read an n-bit binary number as a string
        Dim bin As String = "01000110"   ' 70

        Dim divisible As Boolean = IsDivisibleByFive(bin)

        Console.WriteLine("Binary number: " & bin)
        Console.WriteLine("Divisible by 5: " & If(divisible, "yes", "no"))

        '
        '  Example walk-through for bin = 01000110:
        '
        '  Start: remainder = 0
        '
        '  bit = 0 → remainder = (0*2 + 0) Mod 5 = 0
        '  bit = 1 → remainder = (0*2 + 1) Mod 5 = 1
        '  bit = 0 → remainder = (1*2 + 0) Mod 5 = 2
        '  bit = 0 → remainder = (2*2 + 0) Mod 5 = 4
        '  bit = 0 → remainder = (4*2 + 0) Mod 5 = 3
        '  bit = 1 → remainder = (3*2 + 1) Mod 5 = 2
        '  bit = 1 → remainder = (2*2 + 1) Mod 5 = 0
        '  bit = 0 → remainder = (0*2 + 0) Mod 5 = 0
        '
        '  Final remainder = 0 → divisible by 5
        '
    End Sub

End Module



'
' run:
'
' Binary number: 01000110
' Divisible by 5: yes
'

 



answered Jun 27 by avibootz
edited Jun 28 by avibootz

Related questions

...