449,188 Members | 1,307 Online Need help? Post your question and get tips & solutions from a community of 449,188 IT Pros & Developers. It's quick & easy.

# picking off each digit of an integer

 P: n/a I am using the following code to pick off each digit of a number, from right to left. The number I am working with is 84357. So for the first iteration it should return the number 7 and for the second iteration it should return the number 5, and so on. But for some reason on the first iteration returns the expected results. Each subsequent iteration returns the number plus 1. In order words, when I run the program I am getting: 7, 6, 4, and 9. Instead of 7, 5, 3, 4, and 8. Can someone tell me what's wrong with my code? Sub Main() Dim input As Integer = 84357 Dim digit As Integer Dim decimalNumber As Integer Dim divisor As Integer = 1 While input >= 0 digit = (input / divisor) Mod 10 divisor *= 10 End While End Sub ' Main Oct 14 '06 #1
8 Replies

 P: n/a =?Utf-8?B?Q2FuZGFjZQ==?= I am using the following code to pick off each digit of a number, from right to left. The number I am working with is 84357. So for the first iteration it should return the number 7 and for the second iteration it should return the number 5, and so on. But for some reason on the first iteration returns the expected results. Each subsequent iteration returns the number plus 1. In order words, when I run the program I am getting: 7, 6, 4, and 9. Instead of 7, 5, 3, 4, and 8. Can someone tell me what's wrong with my code? Why don't you convert the number to a string then parse it out? It's probably easier that way. Oct 14 '06 #2

 P: n/a not sure what your really trying to do, sounds like a reverse parse int myVal = 84357; char[] reverse = myVal.ToString().ToCharArray() ; Array.Reverse(reverse); StringBuilder Sb = new StringBuilder(); foreach (char letter in reverse) { Sb.Append(letter); } -- -- Regards John Timney (MVP) VISIT MY WEBSITE: http://www.johntimney.com "Candace" I am using the following code to pick off each digit of a number, fromright to left. The number I am working with is 84357. So for the first iteration it should return the number 7 and for the second iteration it should return the number 5, and so on. But for some reason on the first iteration returns the expected results. Each subsequent iteration returns the number plus 1. In order words, when I run the program I am getting: 7, 6, 4, and 9. Instead of 7, 5, 3, 4, and 8. Can someone tell me what's wrong with my code? Sub Main() Dim input As Integer = 84357 Dim digit As Integer Dim decimalNumber As Integer Dim divisor As Integer = 1 While input >= 0 digit = (input / divisor) Mod 10 divisor *= 10 End While End Sub ' Main Oct 14 '06 #3

 P: n/a As the others said, there are easier ways to do this. But since you're just starting to learn how to program, let's take a look at what's going wrong in your existing code. Consider what your variables are at the beginning of the 2nd pass through your loop - where the problems begin: input = 84357 divisor = 10 And then we execute: digit = (input / divisor) Mod 10 Let's break that down: 1) (input / divisor) = 8435.7 2) 8435.7 Mod 10 = 5.7 3) digit = 5.7 But digit actually becomes 6, because it's an Integer and cannot hold the fraction. VB automatically rounds up or down to the closest whole number for you. One way to fix this is: digit = Int((input / divisor) Mod 10) Because Int always rounds down to the nearest whole number. Oh, and that's an infinite loop you have there. You never change the value of input, so it will never become zero. :) Oct 14 '06 #4

 P: n/a Candace wrote: I am using the following code to pick off each digit of a number, from right to left. The number I am working with is 84357. So for the first iteration it should return the number 7 and for the second iteration it should return the number 5, and so on. But for some reason on the first iteration returns the expected results. Each subsequent iteration returns the number plus 1. In order words, when I run the program I am getting: 7, 6, 4, and 9. Instead of 7, 5, 3, 4, and 8. Can someone tell me what's wrong with my code? Sub Main() Dim input As Integer = 84357 Dim digit As Integer Dim decimalNumber As Integer Dim divisor As Integer = 1 While input >= 0 digit = (input / divisor) Mod 10 divisor *= 10 End While End Sub ' Main You are not getting the digit that you expect, as the value of the division is a floating point number, and it is rounded before you isolate the digit. 84357 divided by 10 will give you the value 8435.7, and that is rounded to 8436 before the modulo 10 operation gives you the digit 6. What you could do is to first get the digit, then remove the digit from the input number. Dim input As Integer = 84357 Dim digit As Integer Dim decimalNumber As Integer Do digit = input Mod 10 input -= digit input \= 10 Loop Until input = 0 Note that the integer division operator(\) is used instead of the floating poing division operator (/). Oct 15 '06 #6

 P: n/a dim chrs as char() = input.tostring.tochararray -- Dennis in Houston "Candace" wrote: I am using the following code to pick off each digit of a number, from right to left. The number I am working with is 84357. So for the first iteration it should return the number 7 and for the second iteration it should return the number 5, and so on. But for some reason on the first iteration returns the expected results. Each subsequent iteration returns the number plus 1. In order words, when I run the program I am getting: 7, 6, 4, and 9. Instead of 7, 5, 3, 4, and 8. Can someone tell me what's wrong with my code? Sub Main() Dim input As Integer = 84357 Dim digit As Integer Dim decimalNumber As Integer Dim divisor As Integer = 1 While input >= 0 digit = (input / divisor) Mod 10 divisor *= 10 End While End Sub ' Main Oct 15 '06 #7

 P: n/a Hi I think that Candace thanks everbody those doing her homework. If would have given an answer it would have been as from Timney, because that is surely not the answer the teacher is expecting. It is a non written rule in this newsgroup not to help with obvious homeowrk. To get your homework done, using a womans name is the first trick because those get mostly a quicker response than man in these newsgroups, so use that. Secondly don't tell it and even if it is find a nice excuse.. Cor "Candace" I am using the following code to pick off each digit of a number, fromright to left. The number I am working with is 84357. So for the first iteration it should return the number 7 and for the second iteration it should return the number 5, and so on. But for some reason on the first iteration returns the expected results. Each subsequent iteration returns the number plus 1. In order words, when I run the program I am getting: 7, 6, 4, and 9. Instead of 7, 5, 3, 4, and 8. Can someone tell me what's wrong with my code? Sub Main() Dim input As Integer = 84357 Dim digit As Integer Dim decimalNumber As Integer Dim divisor As Integer = 1 While input >= 0 digit = (input / divisor) Mod 10 divisor *= 10 End While End Sub ' Main Oct 15 '06 #8

 P: n/a Cor Ligthert [MVP] wrote: To get your homework done, using a womans name is the first trick because those get mostly a quicker response than man in these newsgroups, so use that. Secondly don't tell it and even if it is find a nice excuse.. As Jack (Vincennes) would say, "she *is* Lana Turner"... B. Oct 16 '06 #9

### This discussion thread is closed

Replies have been disabled for this discussion. 