Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,894 questions

51,825 answers

573 users

How to print all 3 digits armstrong numbers in VB.NET

1 Answer

0 votes
Imports System

' Armstrong = a number that equals the sum of its digits, 
'             each raised to a power of the number of digits
' For example 153, it's an Armstrong number because 1^3 + 5^3 + 3^3 = 153

Public Class Program
    Public Shared Function armstrong(ByVal n As Integer) As Boolean
        Dim reminder As Integer = 0, sum As Integer = 0, total_digits As Integer = Math.Floor(Math.Log10(n) + 1)
        Dim temp As Integer = n
        
		While temp > 0
            reminder = temp Mod 10
            sum += CInt(Math.Pow(reminder, total_digits))
            temp = temp \ 10
        End While

        If sum = n Then
            Return True
        End If

        Return False
    End Function

    Public Shared Sub Main(ByVal args As String())
		For i As Integer = 100 To 999 step 1
            If armstrong(i) Then
                Console.Write(i & ", ")
            End If
        Next
    End Sub
End Class



' run:
'
' 153, 370, 371, 407, 
'

 



answered Dec 30, 2023 by avibootz

Related questions

1 answer 253 views
1 answer 95 views
1 answer 111 views
1 answer 93 views
1 answer 128 views
1 answer 159 views
...