Home >>VB.Net Built-in Functions >VB.Net program to check given number is Armstrong or not using While loop

VB.Net program to check given number is Armstrong or not using While loop

Write a VB.Net program to check given number is Armstrong or not using While loop

Here, we will read the user's integer number and then check that the given number is an Armstrong number, otherwise we will not print the appropriate message on the console screen.

Program :

The source code to check the specified number is Armstrong, or the While loop is not used below. The program given is compiled and successfully executed.

'VB.Net program to check the given number is Armstrong number

'or not using "While" loop.


Module Module1
    Sub Main()
        Dim number As Integer = 0
        Dim temp As Integer = 0
        Dim remainder As Integer = 0
        Dim result As Integer = 0
        Console.Write("Enter the number: ")
        number = Integer.Parse(Console.ReadLine())
        temp = number
        While (temp > 0)
            remainder = (temp Mod 10)
            result = result + (remainder * remainder * remainder)
            temp = temp \ 10
        End While
        If (number = result) Then
            Console.WriteLine("Number is armstrong")
        Else
            Console.WriteLine("Number is not armstrong")
        End If
    End Sub   
End Module

	
Output:
Enter the number: 153
Number is armstrong

Explanation:

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

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

temp = number
While (temp > 0)
    remainder = (temp Mod 10)
    result = result + (remainder * remainder * remainder)
    temp = temp \ 10
End While

If (number = result) Then
    Console.WriteLine("Number is armstrong")
Else
    Console.WriteLine("Number is not armstrong")
End If

We read the user's integer number in the above code, and then divide the number until it becomes zero and find the sum of each digit's cube. If the resulting number is equal to the original number then we print "Number is Armstrong" otherwise we will "Number is not Armstrong" on the console screen.


No Sidebar ads