Advertisement
Guest User

Untitled

a guest
Jun 26th, 2019
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 15.99 KB | None | 0 0
  1. Public Class Form1
  2.  
  3. Dim hundredcounter, tencounter, onecounter, total As Integer
  4. Dim x, y As Integer
  5. Dim str As String
  6. Dim eulercounter, eulertotal As Integer
  7.  
  8. Private Sub CalculateButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CalculateButton.Click
  9. total = 0
  10. y = 0
  11. hundredcounter = 0
  12. tencounter = 0
  13. str = ""
  14.  
  15. Try
  16. x = TextBox1.Text 'save number entered in text box
  17. y = x 'use y for calculations and keep x untouched
  18.  
  19. If x > 1000 Or x <= 0 Then 'error check to assure number is between 1-1000
  20.  
  21. Throw New Exception
  22.  
  23. End If
  24.  
  25. If (Not Integer.TryParse(TextBox1.Text, x)) Then 'error check to ensure no decimal is entered
  26.  
  27. Throw New Exception
  28.  
  29. End If
  30.  
  31. If y = 0 Then total = 4 'if number is 0 // no longer needed as 0 is not allowed
  32.  
  33. If x / 100 >= 1 Then 'check to see if the number is over 100
  34.  
  35. Do While y / 100 >= 1 'perform loop to check the hundreds place
  36.  
  37. y = y - 100 'subtract 100 from y for each iteration of the loop
  38. hundredcounter = hundredcounter + 1 'add 1 to hundred counter to keep track of the hundred place
  39.  
  40. Loop
  41.  
  42. End If
  43.  
  44. If hundredcounter = 1 Or hundredcounter = 2 Or hundredcounter = 6 Then 'if number is 100,200,600
  45.  
  46. total = 13 'add 13 to the total numbers
  47. If y = 0 Then total = total - 3 'remove the "and" if the number is an even hundred
  48.  
  49. ElseIf hundredcounter = 4 Or hundredcounter = 5 Or hundredcounter = 9 Then 'if number is 400,500,900
  50.  
  51. total = 14 'add 14 to the total numbers
  52. If y = 0 Then total = total - 3 'remove the "and" if the number is an even hundred
  53.  
  54. ElseIf hundredcounter = 3 Or hundredcounter = 7 Or hundredcounter = 8 Then 'if number is 300,700,800
  55.  
  56. total = 15 'add 15 to the total numbers
  57. If y = 0 Then total = total - 3 'remove the "and" if the number is an even hundred
  58.  
  59. ElseIf hundredcounter = 10 Then 'if the number is 1000
  60. total = 11 'letter count for one thousand
  61.  
  62. End If
  63.  
  64. Select Case hundredcounter 'add to string depending on the hundreds place
  65.  
  66. Case 1
  67. str = str + "One Hundred"
  68. Case 2
  69. str = str + "Two Hundred"
  70. Case 3
  71. str = str + "Three Hundred"
  72. Case 4
  73. str = str + "Four Hundred"
  74. Case 5
  75. str = str + "Five Hundred"
  76. Case 6
  77. str = str + "Six Hundred"
  78. Case 7
  79. str = str + "Seven Hundred"
  80. Case 8
  81. str = str + "Eight Hundred"
  82. Case 9
  83. str = str + "Nine Hundred"
  84. Case 10
  85. str = str + "One Thousand"
  86.  
  87. End Select
  88.  
  89. If y / 10 >= 1 Then 'if there is a value in the tens place
  90.  
  91. Do While y / 10 >= 1 'loop to determine the value in the tens place
  92.  
  93. tencounter = tencounter + 1 'counter for tens place
  94. y = y - 10 'decrease the tens place by 1 for each iteration of loop
  95.  
  96. Loop
  97.  
  98. ElseIf y > 0 And x > 10 Then 'if the tens place is empty and the ones place is not
  99.  
  100. str = str + " and " 'add the and if the tens place is empty
  101.  
  102. End If
  103.  
  104. If tencounter = 1 Then 'if the ten value is in the teens
  105.  
  106. If hundredcounter >= 1 Then
  107.  
  108. str = str + " and " 'add the and if the number is over 100
  109.  
  110. End If
  111.  
  112. Select Case y
  113. Case 0 'case for 10 through 19
  114. total = total + 3 'add to the total
  115. str = str + "Ten" 'add to the string
  116. Case 1
  117. total = total + 6
  118. str = str + "Eleven"
  119. Case 2
  120. total = total + 6
  121. str = str + "Twelve"
  122. Case 3
  123. total = total + 8
  124. str = str + "Thirteen"
  125. Case 4
  126. total = total + 8
  127. str = str + "Fourteen"
  128. Case 5
  129. total = total + 7
  130. str = str + "Fifteen"
  131. Case 6
  132. total = total + 7
  133. str = str + "Sixteen"
  134. Case 7
  135. total = total + 9
  136. str = str + "Seventeen"
  137. Case 8
  138. total = total + 8
  139. str = str + "Eighteen"
  140. Case 9
  141. total = total + 8
  142. str = str + "Nineteen"
  143. End Select
  144.  
  145. y = 0 'set y to 0 as number should now be completed
  146.  
  147. End If
  148.  
  149. If tencounter = 2 Or tencounter = 3 Or tencounter = 8 Or tencounter = 9 Then 'determine value of 10s place
  150.  
  151. total = total + 6 'add to the total
  152.  
  153. ElseIf tencounter = 4 Or tencounter = 5 Or tencounter = 6 Then 'determine value of 10s place
  154.  
  155. total = total + 5
  156.  
  157. ElseIf tencounter = 7 Then 'determine value of 10s place
  158.  
  159. total = total + 7
  160.  
  161. If y = 0 Then 'determine if the ones place is 0
  162.  
  163. End If
  164.  
  165. End If
  166.  
  167. If y = 1 Then total = total + 3 'add to total for ones place remaining
  168. If y = 2 Then total = total + 3
  169. If y = 3 Then total = total + 5
  170. If y = 4 Then total = total + 4
  171. If y = 5 Then total = total + 4
  172. If y = 6 Then total = total + 3
  173. If y = 7 Then total = total + 5
  174. If y = 8 Then total = total + 5
  175. If y = 9 Then total = total + 4
  176.  
  177. If tencounter > 1 And hundredcounter > 0 Then 'add the and if the number is over 100 and the 10s place is not 0
  178. str = str + " and "
  179. End If
  180.  
  181. Select Case tencounter 'output for tens place
  182.  
  183. Case 2
  184. str = str + "Twenty "
  185. Case 3
  186. str = str + "Thirty "
  187. Case 4
  188. str = str + "Fourty "
  189. Case 5
  190. str = str + "Fifty "
  191. Case 6
  192. str = str + "Sixty "
  193. Case 7
  194. str = str + "Seventy "
  195. Case 8
  196. str = str + "Eighty "
  197. Case 9
  198. str = str + "Ninety "
  199.  
  200. End Select
  201.  
  202. Select Case y 'output for ones place
  203.  
  204. Case 1
  205. str = str + "One"
  206. Case 2
  207. str = str + "Two"
  208. Case 3
  209. str = str + "Three"
  210. Case 4
  211. str = str + "Four"
  212. Case 5
  213. str = str + "Five"
  214. Case 6
  215. str = str + "Six"
  216. Case 7
  217. str = str + "Seven"
  218. Case 8
  219. str = str + "Eight"
  220. Case 9
  221. str = str + "Nine"
  222.  
  223. End Select
  224.  
  225. MsgBox("There are " & total & " letters in the number " & str) 'output
  226.  
  227. Catch ex As Exception
  228.  
  229. MsgBox("Nice try Cam.", MsgBoxStyle.Critical) 'message box for when an error occurs
  230.  
  231. End Try
  232.  
  233. TextBox1.Text = "" 'clear text box after each iteration
  234.  
  235. End Sub
  236.  
  237. Private Sub EulerButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles EulerButton.Click
  238.  
  239. total = 0
  240. y = 0
  241. hundredcounter = 0
  242. tencounter = 0
  243. str = ""
  244.  
  245. For x As Integer = 1 To 1000 'loop for numbers 1 to 1000
  246.  
  247. y = x 'reinitialize variables for each iteration of the loop
  248. hundredcounter = 0
  249. tencounter = 0
  250. str = ""
  251.  
  252. If y = 0 Then total = 4 'if number is 0
  253.  
  254. If x / 100 >= 1 Then 'check to see if the number is over 100
  255.  
  256. Do While y / 100 >= 1 'perform loop to check the hundreds place
  257.  
  258. y = y - 100 'subtract 100 from y for each iteration of the loop
  259. hundredcounter = hundredcounter + 1 'add 1 to hundred counter to keep track of the hundred place
  260.  
  261. Loop
  262.  
  263. End If
  264.  
  265. If hundredcounter = 1 Or hundredcounter = 2 Or hundredcounter = 6 Then 'if number is 100,200,600
  266.  
  267. total = total + 13 'add 13 to the total numbers
  268. If y = 0 Then total = total - 3 'remove the "and" if the number is an even hundred
  269.  
  270. ElseIf hundredcounter = 4 Or hundredcounter = 5 Or hundredcounter = 9 Then 'if number is 400,500,900
  271.  
  272. total = total + 14 'add 14 to the total numbers
  273. If y = 0 Then total = total - 3 'remove the "and" if the number is an even hundred
  274.  
  275. ElseIf hundredcounter = 3 Or hundredcounter = 7 Or hundredcounter = 8 Then 'if number is 300,700,800
  276.  
  277. total = total + 15 'add 15 to the total numbers
  278. If y = 0 Then total = total - 3 'remove the "and" if the number is an even hundred
  279.  
  280. ElseIf hundredcounter = 10 Then 'if the number is 1000
  281. total = total + 11 'letter count for one thousand
  282.  
  283. End If
  284.  
  285. Select Case hundredcounter 'add to string depending on the hundreds place
  286.  
  287. Case 1
  288. str = str + "One Hundred"
  289. Case 2
  290. str = str + "Two Hundred"
  291. Case 3
  292. str = str + "Three Hundred"
  293. Case 4
  294. str = str + "Four Hundred"
  295. Case 5
  296. str = str + "Five Hundred"
  297. Case 6
  298. str = str + "Six Hundred"
  299. Case 7
  300. str = str + "Seven Hundred"
  301. Case 8
  302. str = str + "Eight Hundred"
  303. Case 9
  304. str = str + "Nine Hundred"
  305. Case 10
  306. str = str + "One Thousand"
  307.  
  308. End Select
  309.  
  310. If y / 10 >= 1 Then 'if there is a value in the tens place
  311.  
  312. Do While y / 10 >= 1 'loop to determine the value in the tens place
  313.  
  314. tencounter = tencounter + 1 'counter for tens place
  315. y = y - 10 'decrease the tens place by 1 for each iteration of loop
  316.  
  317. Loop
  318.  
  319. ElseIf y > 0 And x > 10 Then 'if the tens place is empty and the ones place is not
  320.  
  321. str = str + " and " 'add the and if the tens place is empty
  322.  
  323. End If
  324.  
  325. If tencounter = 1 Then 'if the ten value is in the teens
  326.  
  327. If hundredcounter >= 1 Then
  328. str = str + " and "
  329. End If
  330.  
  331.  
  332. Select Case y
  333. Case 0 'case for 10 through 19
  334. total = total + 3
  335. str = str + "Ten"
  336. Case 1
  337. total = total + 6
  338. str = str + "Eleven"
  339. Case 2
  340. total = total + 6
  341. str = str + "Twelve"
  342. Case 3
  343. total = total + 8
  344. str = str + "Thirteen"
  345. Case 4
  346. total = total + 8
  347. str = str + "Fourteen"
  348. Case 5
  349. total = total + 7
  350. str = str + "Fifteen"
  351. Case 6
  352. total = total + 7
  353. str = str + "Sixteen"
  354. Case 7
  355. total = total + 9
  356. str = str + "Seventeen"
  357. Case 8
  358. total = total + 8
  359. str = str + "Eighteen"
  360. Case 9
  361. total = total + 8
  362. str = str + "Nineteen"
  363. End Select
  364.  
  365. y = 0 'set y to 0
  366.  
  367. End If
  368.  
  369. If tencounter = 2 Or tencounter = 3 Or tencounter = 8 Or tencounter = 9 Then 'determine value of 10s place
  370.  
  371. total = total + 6
  372.  
  373. ElseIf tencounter = 4 Or tencounter = 5 Or tencounter = 6 Then 'determine value of 10s place
  374.  
  375. total = total + 5
  376.  
  377. ElseIf tencounter = 7 Then 'determine value of 10s place
  378.  
  379. total = total + 7
  380.  
  381. If y = 0 Then 'determine if the ones place is 0
  382.  
  383. End If
  384.  
  385. End If
  386.  
  387. If y = 1 Then total = total + 3 'add to total for ones place remaining
  388. If y = 2 Then total = total + 3
  389. If y = 3 Then total = total + 5
  390. If y = 4 Then total = total + 4
  391. If y = 5 Then total = total + 4
  392. If y = 6 Then total = total + 3
  393. If y = 7 Then total = total + 5
  394. If y = 8 Then total = total + 5
  395. If y = 9 Then total = total + 4
  396.  
  397. If tencounter > 1 Then
  398. str = str + " and "
  399. End If
  400.  
  401. Select Case tencounter 'output for tens place
  402.  
  403. Case 2
  404. str = str + "Twenty "
  405. Case 3
  406. str = str + "Thirty "
  407. Case 4
  408. str = str + "Fourty "
  409. Case 5
  410. str = str + "Fifty "
  411. Case 6
  412. str = str + "Sixty "
  413. Case 7
  414. str = str + "Seventy "
  415. Case 8
  416. str = str + "Eighty "
  417. Case 9
  418. str = str + "Ninety "
  419.  
  420. End Select
  421.  
  422. Select Case y 'output for ones place
  423.  
  424. Case 1
  425. str = str + "One"
  426. Case 2
  427. str = str + "Two"
  428. Case 3
  429. str = str + "Three"
  430. Case 4
  431. str = str + "Four"
  432. Case 5
  433. str = str + "Five"
  434. Case 6
  435. str = str + "Six"
  436. Case 7
  437. str = str + "Seven"
  438. Case 8
  439. str = str + "Eight"
  440. Case 9
  441. str = str + "Nine"
  442.  
  443. End Select
  444.  
  445. Next
  446.  
  447. MsgBox("The sum of all letters for numbers 1-1000 is: " & total) 'output
  448.  
  449. End Sub
  450. End Class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement