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,753 questions

55,518 answers

573 users

How to find the frequency of each digit (0–9) in a number with VB.NET

1 Answer

0 votes
Imports System

Module DigitFrequency

    '
    '    Function: countDigitFrequency
    '    Purpose:  Counts how many times each digit (0–9) appears in a number.
    '    Parameters:
    '        - n: the number whose digits we want to count
    '        - freq: an array of size 10 that stores the frequency of each digit
    '                freq(0) = count of digit '0'
    '                freq(1) = count of digit '1'
    '                ...
    '                freq(9) = count of digit '9'
    '    Explanation:
    '        We repeatedly extract the last digit using n Mod 10,
    '        then remove that digit using n \ 10.
    '
    Sub CountDigitFrequency(ByVal n As Integer, ByVal freq() As Integer)

        ' Process each digit of the number
        While n > 0
            Dim digit As Integer = n Mod 10   ' extract last digit
            freq(digit) += 1                  ' increase its frequency
            n \= 10                           ' remove last digit
        End While

    End Sub

    Sub Main()

        Dim n As Integer = 79712622   ' the number we want to analyze

        Dim freq(9) As Integer        ' array to store digit frequencies

        ' Call the function to count digit frequencies
        CountDigitFrequency(n, freq)

        ' Display the result
		Console.WriteLine("Digit frequencies in " & n & ":" & Environment.NewLine)

        For i As Integer = 0 To 9
            If freq(i) <> 0 Then
                Console.WriteLine("Digit " & i & " occurs " & freq(i) & " times")
            End If
        Next

    End Sub

End Module


'
' run:
'
' Digit frequencies in 79712622:
'
' Digit 1 occurs 1 times
' Digit 2 occurs 3 times
' Digit 6 occurs 1 times
' Digit 7 occurs 2 times
' Digit 9 occurs 1 times
'

 



answered Jul 2 by avibootz
...