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

Here, we are going to learn how to count the digits of a given number using the While loop in VB.Net?
Submitted by Nidhi, on December 06, 2020 [Last updated : March 06, 2023]

Count the digits of a number using While loop in VB.Net

Here, we will read an integer number from the user and then count the total digits in the given number and print the count on the console screen.

Program/Source Code:

The source code to count the digits of the given number using the While loop is given below. The given program is compiled and executed successfully.

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

'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
Press any key to continue . . .

Explanation:

In the above program, we created a module Module1 that contains a function Main(). In the Main() we created two integer variables number and count, 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)

In the above code, we read the integer number from the user and then we divided the number till it becomes zero and also increase the variable count by one. After the print the count of digits on the console screen.

VB.Net Basic Programs »






Comments and Discussions!

Load comments ↻






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