picking off each digit of an integer

G

Guest

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
 
G

Guest

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.
 
J

John Timney \(MVP\)

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
 
T

teslar91

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. :)
 
A

_AnonCoward

:
: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

The root problem is you are dividing by 10, creating a floating point
value in the process, then implicitly coverting it back to an integer.
Implicit conversions are generally a bad idea and your sample code
here is a good example of this. The "Option Strict" statement was put
into place to address that and in my opinion it significantly improved
the VB language. I strongly recommend you use it unless you have a
good reason not to.

Here is a simpler bit of code you can use to achieve your desired
result:

'Option Strict means you must declare your variables by name and
'type before you can use it. This is a good idea in almost all cases

Option Strict

Public Module [module]
Sub Main()

'this sample only needs one variable because we will be modifying
'it with each pass and working on the resulting value

Dim input As Integer = 84357

'your original code never modified the value of "input" and
'so it was always ">= 0". This would have resulted in an
'endless loop. Here, we are testing for any value greater
'than 0. Since we reduce the value of "input" by 10 in each
'pass, we eventually reduce it to 0 and thereby exit the loop

While input > 0

'we'll use the MOD function you originally had as it served
'your purpose quite well

System.Console.WriteLine(input Mod 10)

'now we'll do "Integer" division (using the back slash
'operator instead of the forward slash). This returns an
'integer value instead of a floating point. For example,
'compare these results:
'
' 84357 \ 10 = 8435
' 84357 / 10 = 8435.7
'
'In your original code, you got back a floating point (single)
'value which you then performed the MOD operation on. In that
'case, you generated the following values behind the scenes:
'
' 84357 / 10 = 8435.7
' 8435.7 MOD 10 = 5.70000000000073
'
'This final value (5.70000000000073) was then changed back to
'an integer value behind the scenes by the compiler (that is,
'implicitly) in order to store it in the 'digit' integer
'variable. Such an implicit conversion automatically invokes
'the built in rounding behavior of VB which rounds 5.7 up
'to 6. This was compounded with each pass.
'
'In contrast, by integer dividing by 10 as shown below, we
'effectively strip off the trailing digit with each pass without
'the implicit conversion or the rounding. Integer division is
'very efficient and can be used whenever you specifically don't
'need or want to retain the digits to the right of the decimal
'point after you've completed the division operation.

Input = input \ 10

End While
End Sub
End Module


Ralf
--
AA #2250
-------------------------------------------------------------
* ^~^ ^~^ *
* _ {| |} {| |} _ *
* /_``>*< >*<''_\ *
* (\--_)++) (++(_--/) *
-------------------------------------------------------------
Nature is the canvas of creation and evolution but one of
the brushes. Religion points to the mind of God; Science
reveals its unfolding. The subjective apprehends knowledge
while the objective facilitates understanding.

In all things, yin and yang - ever flowing, one into the
other; always overtaking, always overtaken.
 
G

Guest

Candace said:
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 (/).
 
C

Cor Ligthert [MVP]

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
 
B

Branco Medeiros

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..
<snip>

As Jack (Vincennes) would say, "she *is* Lana Turner"...

B.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top