You are here: DEVPPL Forum Programming Visual Basic Forum
NOTIFICATIONS
54.086
MEMBERS
15.684
TOPICS
62.255
POSTS
  562
FLASH GAMES
7.740
TUTORIALS
 

Login

E-mail:
Password:

Some Basics Help

0

Loading

Some Basics Help

Postby Chrislenway » Sat Oct 15, 2011 2:34 pm

I am trying to put together a program that searches Armstrong numbers. (Armstrong number example is 153: take the power of the number of digits (3 in 153) and sum to equal original number; 1^3 + 5^3 + 3^3 = 153). The program that I have built so far (Code below) shows the following as I understand it. I have called out my variables with a mathematical formula to separate the digits in the Select case, when the select case runs 1 to 9, it should take each number and run it through the formula with the power and then check to see if it is equal to the number in the case select. If it is equal, the number should then be returned to listbox2, and then continue to the next case and formula. I'm here becuase what I think is obviously not the right way. Can someone help me with the code and explain why my result does not work? :D Thanks in advance :D -Chris

Private Sub Button3_Click(sender As System.Object, e As System.EventArgs) Handles Button3.Click

Dim Answer As Integer
Dim AnswerReturn As Integer
Dim FirstDigit As Integer = Answer / 1000000
Dim Remainder1 As Integer = Answer Mod 100000
Dim SecondDigit As Integer = Answer / 100000
Dim Remainder2 As Integer = Answer Mod 10000
Dim ThirdDigit As Integer = Answer / 10000
Dim Remainder3 As Integer = Answer Mod 1000
Dim FourthDigit As Integer = Answer / 1000
Dim Remainder4 As Integer = Answer Mod 100
Dim FifthDigit As Integer = Answer / 100
Dim Remainder5 As Integer = Answer Mod 10
Dim SixthDigit As Integer = Answer / 10
Dim Remainder6 As Integer = Answer Mod 1
Dim SeventhDigit As Integer = Answer / 1


Do While Answer <= 10000 'NOTE: Shortened to Speed up Program

Select Case Answer


Case 1 To 9

AnswerReturn = SeventhDigit ^ 1

If AnswerReturn = Answer Then

ListBox2.Items.Add(AnswerReturn.ToString)
End If

Case 10 To 99
AnswerReturn = (SixthDigit ^ 2) + (Remainder6 ^ 2) + (SeventhDigit ^ 2)

If AnswerReturn = Answer Then
ListBox2.Items.Add(AnswerReturn.ToString)
End If


Case 100 To 999
AnswerReturn = (FifthDigit ^ 3) + (Remainder5 ^ 3) + (SixthDigit ^ 3) + (Remainder6 ^ 3) + (SeventhDigit ^ 3)

If AnswerReturn = Answer Then
ListBox2.Items.Add(AnswerReturn.ToString)
End If


Case 1000 To 9999
AnswerReturn = (FourthDigit ^ 4) + (Remainder4 ^ 4) + (FifthDigit ^ 4) + (Remainder5 ^ 4) + (SixthDigit ^ 4) + (Remainder6 ^ 4) &
+(SeventhDigit ^ 4)

If AnswerReturn = Answer Then
ListBox2.Items.Add(AnswerReturn.ToString)
End If


Case 10000 To 99999
AnswerReturn = (ThirdDigit ^ 5) + (Remainder3 ^ 5) + (FourthDigit ^ 5) + (Remainder4 ^ 5) + (FifthDigit ^ 5) + (Remainder5 ^ 5) &
+(SixthDigit ^ 5) + (Remainder6 ^ 5) + (SeventhDigit ^ 5)

If AnswerReturn = Answer Then
ListBox2.Items.Add(AnswerReturn.ToString)
End If


Case 100000 To 999999
AnswerReturn = (SecondDigit ^ 6) + (Remainder2 ^ 6) + (ThirdDigit ^ 6) + (Remainder3 ^ 6) + (FourthDigit ^ 6) + (Remainder4 ^ 6) &
+(FifthDigit ^ 6) + (Remainder5 ^ 6) + (SixthDigit ^ 6) + (Remainder6 ^ 6) + (SeventhDigit ^ 6)

If AnswerReturn = Answer Then
ListBox2.Items.Add(AnswerReturn.ToString)
End If


Case 1000000 To 9999999
AnswerReturn = (FirstDigit ^ 7) + (Remainder1 ^ 7) + (SecondDigit ^ 7) + (Remainder2 ^ 7) + (ThirdDigit ^ 7) + (Remainder3 ^ 7) &
+(FourthDigit ^ 7) + (Remainder4 ^ 7) + (FifthDigit ^ 7) + (Remainder5 ^ 7) + (SixthDigit ^ 7) + (Remainder6 ^ 7) &
+(SeventhDigit ^ 7)

If AnswerReturn = Answer Then
ListBox2.Items.Add(AnswerReturn.ToString)
End If


Case Else
AnswerReturn = 707070707 'NOTE- DEFAULT ANSWER TO SEE IF CASES ARE BYPASSED
ListBox2.Items.Add(AnswerReturn.ToString)


End Select

ListBox1.Items.Add(Answer.ToString)

Answer += 1

Loop

End Sub
Chrislenway
 
Reputation: 0
Posts: 1
Joined: Sat Oct 15, 2011 2:19 pm
Highscores: 0
Arcade winning challenges: 0

Some Basics Help - Sponsored results

Sponsored results

Login to get rid of ads

 

0

Loading

Re: Some Basics Help

Postby Sanjon » Fri Oct 21, 2011 2:11 pm

Here, this code should work. It may not be the most efficient method to solve the problem, but I tried to base it mostly on the code you provided. Sorry for the late reply. I don't come here very often.

Code: Select all
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        Dim Answer As Integer
        Dim AnswerReturn As Integer
        Dim FirstDigit, SecondDigit, ThirdDigit, FourthDigit, FifthDigit, SixthDigit, SeventhDigit As Integer

        Do While Answer <= 10000 'NOTE: Shortened to Speed up Program

            Select Case Answer.ToString.Length
                Case 1
                    SeventhDigit = Answer
                Case 2
                    SeventhDigit = Mid(Answer, 2, 1)
                    SixthDigit = Mid(Answer, 1, 1)
                Case 3
                    SeventhDigit = Mid(Answer, 3, 1)
                    SixthDigit = Mid(Answer, 2, 1)
                    FifthDigit = Mid(Answer, 1, 1)
                Case 4
                    SeventhDigit = Mid(Answer, 4, 1)
                    SixthDigit = Mid(Answer, 3, 1)
                    FifthDigit = Mid(Answer, 2, 1)
                    FourthDigit = Mid(Answer, 1, 1)
                Case 5
                    SeventhDigit = Mid(Answer, 5, 1)
                    SixthDigit = Mid(Answer, 4, 1)
                    FifthDigit = Mid(Answer, 3, 1)
                    FourthDigit = Mid(Answer, 2, 1)
                    ThirdDigit = Mid(Answer, 1, 1)
                Case 6
                    SeventhDigit = Mid(Answer, 6, 1)
                    SixthDigit = Mid(Answer, 5, 1)
                    FifthDigit = Mid(Answer, 4, 1)
                    FourthDigit = Mid(Answer, 3, 1)
                    ThirdDigit = Mid(Answer, 2, 1)
                    SecondDigit = Mid(Answer, 1, 1)
                Case 7
                    SeventhDigit = Mid(Answer, 7, 1)
                    SixthDigit = Mid(Answer, 6, 1)
                    FifthDigit = Mid(Answer, 5, 1)
                    FourthDigit = Mid(Answer, 4, 1)
                    ThirdDigit = Mid(Answer, 3, 1)
                    SecondDigit = Mid(Answer, 2, 1)
                    FirstDigit = Mid(Answer, 1, 1)
            End Select

            Select Case Answer

                Case 1 To 9
                    AnswerReturn = SeventhDigit ^ 1

                    If AnswerReturn = Answer Then

                        ListBox2.Items.Add(AnswerReturn.ToString)
                    End If

                Case 10 To 99
                    AnswerReturn = (SixthDigit ^ 2) + (SeventhDigit ^ 2)

                    If AnswerReturn = Answer Then
                        ListBox2.Items.Add(AnswerReturn.ToString)
                    End If

                Case 100 To 999
                    AnswerReturn = (FifthDigit ^ 3) + (SixthDigit ^ 3) + (SeventhDigit ^ 3)

                    If AnswerReturn = Answer Then
                        ListBox2.Items.Add(AnswerReturn.ToString)
                    End If

                Case 1000 To 9999
                    AnswerReturn = (FourthDigit ^ 4) + (FifthDigit ^ 4) + (SixthDigit ^ 4) + (SeventhDigit ^ 4)

                    If AnswerReturn = Answer Then
                        ListBox2.Items.Add(AnswerReturn.ToString)
                    End If

                Case 10000 To 99999
                    AnswerReturn = (ThirdDigit ^ 5) + (FourthDigit ^ 5) + (FifthDigit ^ 5) + (SixthDigit ^ 5) + (SeventhDigit ^ 5)

                    If AnswerReturn = Answer Then
                        ListBox2.Items.Add(AnswerReturn.ToString)
                    End If

                Case 100000 To 999999
                    AnswerReturn = (SecondDigit ^ 6) + (ThirdDigit ^ 6) + (FourthDigit ^ 6) + (FifthDigit ^ 6) + (SixthDigit ^ 6) + (SeventhDigit ^ 6)

                    If AnswerReturn = Answer Then
                        ListBox2.Items.Add(AnswerReturn.ToString)
                    End If

                Case 1000000 To 9999999
                    AnswerReturn = (FirstDigit ^ 7) + (SecondDigit ^ 7) + (ThirdDigit ^ 7) + (FourthDigit ^ 7) + (FifthDigit ^ 7) + (SixthDigit ^ 7) + (SeventhDigit ^ 7)

                    If AnswerReturn = Answer Then
                        ListBox2.Items.Add(AnswerReturn.ToString)
                    End If

                Case Else
                    AnswerReturn = 707070707 'NOTE- DEFAULT ANSWER TO SEE IF CASES ARE BYPASSED
                    ListBox2.Items.Add(AnswerReturn.ToString)

            End Select

            ListBox1.Items.Add(Answer.ToString)

            Answer += 1

        Loop

    End Sub
Sanjon
 
Reputation: 0
Posts: 40
Joined: Sun Dec 05, 2010 7:20 pm
Highscores: 0
Arcade winning challenges: 0
^ Back to Top