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

VB.Net program to reverse the given number using the While loop

Write a VB.Net program to reverse the given number using the While loop

Here, we'll read the user's integer number and then reverse the specified number and print it on the screen of the console.

Program :

Below is the source code for reversing the specified number by using the While loop. The program given is compiled and successfully executed.

'VB.Net program to reverse the given number

'using the "While" loop.


Module Module1
    Sub Main()
        Dim number As Integer = 0
        Dim remainder As Integer = 0
        Dim reverse As Integer = 0
        Console.Write("Enter the number: ")
        number = Integer.Parse(Console.ReadLine())
        While (number > 0)
            remainder = number Mod 10
            reverse = reverse * 10 + remainder
            number = number / 10
        End While
        Console.WriteLine("Reverse: {0}", reverse)
    End Sub 
End Module
	
Output:
Enter the number: 1234
Reverse: 4321

Explanation:

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

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

While (number > 0)
    remainder = number Mod 10
    reverse = reverse * 10 + remainder
    number = number / 10
End While

Here, by finding the remainder of the number before it becomes zero, we read the integer number from the user and then reverse the number and then print the reverse of the number on the console screen.