Home >>VB.Net Built-in Functions >VB.Net program to count the digits of given number using While loop

VB.Net program to count the digits of given number using While loop

Write a VB.Net program to count the digits of given number using While loop

Here, we'll read the user's integer number, then count the total digits in the specified number and print the console screen count.

Program :

Below is the source code for numbering the digits of the given number by using the While loop. The program given is compiled and successfully executed.

'VB.Net program to count the digits of

'given number using "While" loop.


Module Module1
    Sub Main()
        Dim number As Integer = 0
        Dim count As Integer = 0       
        Console.Write("Enter the number: ")
        number = Integer.Parse(Console.ReadLine())
        While (number > 0)
            count = count + 1
            number = number / 10
        End While
        Console.WriteLine("Total number of digits are: {0}", count)
    End Sub   
End Module

	
Output:
Enter the number: 12345
Total number of digits are: 5

Explanation:

We created a Module1 module in the above program that contains the function Main (). Two integer variables number and count were created in the Main(), which are initialized with 0.

Console.Write("Enter the number: ")
number = Integer.Parse(Console.ReadLine())

While (number > 0)
    count = count + 1
    number = number / 10
End While

Console.WriteLine("Total number of digits are: {0}", count)

We read the integer number from the user in the above code and then we split the number until it becomes zero and increase the element count by one as well. The count of digits on the console screen after printing.


No Sidebar ads