Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Public Class Form1
- Dim hundredcounter, tencounter, onecounter, total As Integer
- Dim x, y As Integer
- Dim str As String
- Dim eulercounter, eulertotal As Integer
- Private Sub CalculateButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CalculateButton.Click
- total = 0
- y = 0
- hundredcounter = 0
- tencounter = 0
- str = ""
- Try
- x = TextBox1.Text 'save number entered in text box
- y = x 'use y for calculations and keep x untouched
- If x > 1000 Or x <= 0 Then 'error check to assure number is between 1-1000
- Throw New Exception
- End If
- If (Not Integer.TryParse(TextBox1.Text, x)) Then 'error check to ensure no decimal is entered
- Throw New Exception
- End If
- If y = 0 Then total = 4 'if number is 0 // no longer needed as 0 is not allowed
- If x / 100 >= 1 Then 'check to see if the number is over 100
- Do While y / 100 >= 1 'perform loop to check the hundreds place
- y = y - 100 'subtract 100 from y for each iteration of the loop
- hundredcounter = hundredcounter + 1 'add 1 to hundred counter to keep track of the hundred place
- Loop
- End If
- If hundredcounter = 1 Or hundredcounter = 2 Or hundredcounter = 6 Then 'if number is 100,200,600
- total = 13 'add 13 to the total numbers
- If y = 0 Then total = total - 3 'remove the "and" if the number is an even hundred
- ElseIf hundredcounter = 4 Or hundredcounter = 5 Or hundredcounter = 9 Then 'if number is 400,500,900
- total = 14 'add 14 to the total numbers
- If y = 0 Then total = total - 3 'remove the "and" if the number is an even hundred
- ElseIf hundredcounter = 3 Or hundredcounter = 7 Or hundredcounter = 8 Then 'if number is 300,700,800
- total = 15 'add 15 to the total numbers
- If y = 0 Then total = total - 3 'remove the "and" if the number is an even hundred
- ElseIf hundredcounter = 10 Then 'if the number is 1000
- total = 11 'letter count for one thousand
- End If
- Select Case hundredcounter 'add to string depending on the hundreds place
- Case 1
- str = str + "One Hundred"
- Case 2
- str = str + "Two Hundred"
- Case 3
- str = str + "Three Hundred"
- Case 4
- str = str + "Four Hundred"
- Case 5
- str = str + "Five Hundred"
- Case 6
- str = str + "Six Hundred"
- Case 7
- str = str + "Seven Hundred"
- Case 8
- str = str + "Eight Hundred"
- Case 9
- str = str + "Nine Hundred"
- Case 10
- str = str + "One Thousand"
- End Select
- If y / 10 >= 1 Then 'if there is a value in the tens place
- Do While y / 10 >= 1 'loop to determine the value in the tens place
- tencounter = tencounter + 1 'counter for tens place
- y = y - 10 'decrease the tens place by 1 for each iteration of loop
- Loop
- ElseIf y > 0 And x > 10 Then 'if the tens place is empty and the ones place is not
- str = str + " and " 'add the and if the tens place is empty
- End If
- If tencounter = 1 Then 'if the ten value is in the teens
- If hundredcounter >= 1 Then
- str = str + " and " 'add the and if the number is over 100
- End If
- Select Case y
- Case 0 'case for 10 through 19
- total = total + 3 'add to the total
- str = str + "Ten" 'add to the string
- Case 1
- total = total + 6
- str = str + "Eleven"
- Case 2
- total = total + 6
- str = str + "Twelve"
- Case 3
- total = total + 8
- str = str + "Thirteen"
- Case 4
- total = total + 8
- str = str + "Fourteen"
- Case 5
- total = total + 7
- str = str + "Fifteen"
- Case 6
- total = total + 7
- str = str + "Sixteen"
- Case 7
- total = total + 9
- str = str + "Seventeen"
- Case 8
- total = total + 8
- str = str + "Eighteen"
- Case 9
- total = total + 8
- str = str + "Nineteen"
- End Select
- y = 0 'set y to 0 as number should now be completed
- End If
- If tencounter = 2 Or tencounter = 3 Or tencounter = 8 Or tencounter = 9 Then 'determine value of 10s place
- total = total + 6 'add to the total
- ElseIf tencounter = 4 Or tencounter = 5 Or tencounter = 6 Then 'determine value of 10s place
- total = total + 5
- ElseIf tencounter = 7 Then 'determine value of 10s place
- total = total + 7
- If y = 0 Then 'determine if the ones place is 0
- End If
- End If
- If y = 1 Then total = total + 3 'add to total for ones place remaining
- If y = 2 Then total = total + 3
- If y = 3 Then total = total + 5
- If y = 4 Then total = total + 4
- If y = 5 Then total = total + 4
- If y = 6 Then total = total + 3
- If y = 7 Then total = total + 5
- If y = 8 Then total = total + 5
- If y = 9 Then total = total + 4
- If tencounter > 1 And hundredcounter > 0 Then 'add the and if the number is over 100 and the 10s place is not 0
- str = str + " and "
- End If
- Select Case tencounter 'output for tens place
- Case 2
- str = str + "Twenty "
- Case 3
- str = str + "Thirty "
- Case 4
- str = str + "Fourty "
- Case 5
- str = str + "Fifty "
- Case 6
- str = str + "Sixty "
- Case 7
- str = str + "Seventy "
- Case 8
- str = str + "Eighty "
- Case 9
- str = str + "Ninety "
- End Select
- Select Case y 'output for ones place
- Case 1
- str = str + "One"
- Case 2
- str = str + "Two"
- Case 3
- str = str + "Three"
- Case 4
- str = str + "Four"
- Case 5
- str = str + "Five"
- Case 6
- str = str + "Six"
- Case 7
- str = str + "Seven"
- Case 8
- str = str + "Eight"
- Case 9
- str = str + "Nine"
- End Select
- MsgBox("There are " & total & " letters in the number " & str) 'output
- Catch ex As Exception
- MsgBox("Nice try Cam.", MsgBoxStyle.Critical) 'message box for when an error occurs
- End Try
- TextBox1.Text = "" 'clear text box after each iteration
- End Sub
- Private Sub EulerButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles EulerButton.Click
- total = 0
- y = 0
- hundredcounter = 0
- tencounter = 0
- str = ""
- For x As Integer = 1 To 1000 'loop for numbers 1 to 1000
- y = x 'reinitialize variables for each iteration of the loop
- hundredcounter = 0
- tencounter = 0
- str = ""
- If y = 0 Then total = 4 'if number is 0
- If x / 100 >= 1 Then 'check to see if the number is over 100
- Do While y / 100 >= 1 'perform loop to check the hundreds place
- y = y - 100 'subtract 100 from y for each iteration of the loop
- hundredcounter = hundredcounter + 1 'add 1 to hundred counter to keep track of the hundred place
- Loop
- End If
- If hundredcounter = 1 Or hundredcounter = 2 Or hundredcounter = 6 Then 'if number is 100,200,600
- total = total + 13 'add 13 to the total numbers
- If y = 0 Then total = total - 3 'remove the "and" if the number is an even hundred
- ElseIf hundredcounter = 4 Or hundredcounter = 5 Or hundredcounter = 9 Then 'if number is 400,500,900
- total = total + 14 'add 14 to the total numbers
- If y = 0 Then total = total - 3 'remove the "and" if the number is an even hundred
- ElseIf hundredcounter = 3 Or hundredcounter = 7 Or hundredcounter = 8 Then 'if number is 300,700,800
- total = total + 15 'add 15 to the total numbers
- If y = 0 Then total = total - 3 'remove the "and" if the number is an even hundred
- ElseIf hundredcounter = 10 Then 'if the number is 1000
- total = total + 11 'letter count for one thousand
- End If
- Select Case hundredcounter 'add to string depending on the hundreds place
- Case 1
- str = str + "One Hundred"
- Case 2
- str = str + "Two Hundred"
- Case 3
- str = str + "Three Hundred"
- Case 4
- str = str + "Four Hundred"
- Case 5
- str = str + "Five Hundred"
- Case 6
- str = str + "Six Hundred"
- Case 7
- str = str + "Seven Hundred"
- Case 8
- str = str + "Eight Hundred"
- Case 9
- str = str + "Nine Hundred"
- Case 10
- str = str + "One Thousand"
- End Select
- If y / 10 >= 1 Then 'if there is a value in the tens place
- Do While y / 10 >= 1 'loop to determine the value in the tens place
- tencounter = tencounter + 1 'counter for tens place
- y = y - 10 'decrease the tens place by 1 for each iteration of loop
- Loop
- ElseIf y > 0 And x > 10 Then 'if the tens place is empty and the ones place is not
- str = str + " and " 'add the and if the tens place is empty
- End If
- If tencounter = 1 Then 'if the ten value is in the teens
- If hundredcounter >= 1 Then
- str = str + " and "
- End If
- Select Case y
- Case 0 'case for 10 through 19
- total = total + 3
- str = str + "Ten"
- Case 1
- total = total + 6
- str = str + "Eleven"
- Case 2
- total = total + 6
- str = str + "Twelve"
- Case 3
- total = total + 8
- str = str + "Thirteen"
- Case 4
- total = total + 8
- str = str + "Fourteen"
- Case 5
- total = total + 7
- str = str + "Fifteen"
- Case 6
- total = total + 7
- str = str + "Sixteen"
- Case 7
- total = total + 9
- str = str + "Seventeen"
- Case 8
- total = total + 8
- str = str + "Eighteen"
- Case 9
- total = total + 8
- str = str + "Nineteen"
- End Select
- y = 0 'set y to 0
- End If
- If tencounter = 2 Or tencounter = 3 Or tencounter = 8 Or tencounter = 9 Then 'determine value of 10s place
- total = total + 6
- ElseIf tencounter = 4 Or tencounter = 5 Or tencounter = 6 Then 'determine value of 10s place
- total = total + 5
- ElseIf tencounter = 7 Then 'determine value of 10s place
- total = total + 7
- If y = 0 Then 'determine if the ones place is 0
- End If
- End If
- If y = 1 Then total = total + 3 'add to total for ones place remaining
- If y = 2 Then total = total + 3
- If y = 3 Then total = total + 5
- If y = 4 Then total = total + 4
- If y = 5 Then total = total + 4
- If y = 6 Then total = total + 3
- If y = 7 Then total = total + 5
- If y = 8 Then total = total + 5
- If y = 9 Then total = total + 4
- If tencounter > 1 Then
- str = str + " and "
- End If
- Select Case tencounter 'output for tens place
- Case 2
- str = str + "Twenty "
- Case 3
- str = str + "Thirty "
- Case 4
- str = str + "Fourty "
- Case 5
- str = str + "Fifty "
- Case 6
- str = str + "Sixty "
- Case 7
- str = str + "Seventy "
- Case 8
- str = str + "Eighty "
- Case 9
- str = str + "Ninety "
- End Select
- Select Case y 'output for ones place
- Case 1
- str = str + "One"
- Case 2
- str = str + "Two"
- Case 3
- str = str + "Three"
- Case 4
- str = str + "Four"
- Case 5
- str = str + "Five"
- Case 6
- str = str + "Six"
- Case 7
- str = str + "Seven"
- Case 8
- str = str + "Eight"
- Case 9
- str = str + "Nine"
- End Select
- Next
- MsgBox("The sum of all letters for numbers 1-1000 is: " & total) 'output
- End Sub
- End Class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement