×

VB.Net Programs

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

By Nidhi Last Updated : October 13, 2024

Check Armstrong Number using While loop in VB.Net

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

Program/Source Code:

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

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

'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
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 four integer variables number, temp, result and remainder, 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

In the above code, we read the integer number from the user and then we divided the number till it becomes zero and find the sum of the cube of each digit. If the resulted number is equivalent to the original number then we print "Number is Armstrong" otherwise we will "Number is not Armstrong" on the console screen.

VB.Net Basic Programs »


Comments and Discussions!

Load comments ↻





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