Jump to content



Welcome to AstaHost - Dear Guest , Please Register here to get Your own website. - Ask a Question / Express Opinion / Reply w/o Sign-Up!

Toggle shoutbox Shoutbox Open the Shoutbox in a popup

@  agyat : (23 May 2013 - 01:23 AM) Wow! Mr. Sb Back Home.
@  OpaQue : (23 May 2013 - 12:44 AM) Ting
@  OpaQue : (24 April 2013 - 02:44 PM) I guess, Time to run Mycent script.
@  OpaQue : (24 April 2013 - 02:43 PM) wow.. not much spam. except habatt posting lot of links.. :P
@  yordan : (23 April 2013 - 01:04 PM) You're welcome, agyat. Nice to have been helpful. Second lesson: try full words, "you" instead of "EW".
@  agyat : (23 April 2013 - 05:03 AM) @YORDAN: tHANK EW FOR YOUR FIRST LESSON.   :D
@  yordan : (22 April 2013 - 09:43 PM) @agyat : "why don't you help me", or "please help me", or "please teach us"
@  yordan : (22 April 2013 - 09:42 PM) welcome back, velma
@  velma : (22 April 2013 - 07:51 AM) **yawns** Good to be back, wonder what is going on here :)
@  agyat : (22 April 2013 - 03:50 AM) Oh! so, why don't help me learn english..
@  yordan : (21 April 2013 - 08:38 PM) The goal mentioned by shiu : "learning english, learning computer"
@  agyat : (21 April 2013 - 06:31 PM) WHAT GOAL?
@  yordan : (20 April 2013 - 10:39 AM) yes, that's our goal. simultaneouly learning English and teaching/learning computer using.
@  shiyu : (20 April 2013 - 07:30 AM) learning english,learning computer
@  yordan : (19 April 2013 - 01:11 PM) Oh, I see, it's just a trick in order to force people looking at your texte. Somehow smart, maybe.
@  agyat : (19 April 2013 - 02:54 AM) And of course I know it is not SEO friendly.
@  agyat : (19 April 2013 - 02:52 AM) There may be two possible answers for that ....


1) Shout was posted using mobile keypad.

2) To force people read content carefully and/or with more concentration.
@  agyat : (19 April 2013 - 02:49 AM) There may be two possible answers for that ....
@  yordan : (18 April 2013 - 09:35 PM) however, why this mixing of capital letters in the middle of your text?
@  agyat : (18 April 2013 - 11:10 AM) false feelings.

Photo
- - - - -

A Small Project: Splitting An Integer Into Digits


6 replies to this topic

#1 derouge

derouge

    Advanced Member

  • Members
  • 111 posts

Posted 29 December 2005 - 10:58 PM

I need to know how to "split" a number up into seperae numbers, using VB. For example, "54321". I need to split that into 5 seperate numbers - "5," "4," "3," "2," "1," and assign them to a variable, if possible.

This next part isn't important, but if you're curious why I need to know ...
Eventually what I need to do is split the number into these individual parts and add them together. With that new number I need to again split it and take the last number subtracted from 10. That new number will then be placed at the end of the beginning number.

Example:
5+4+3+2+1 = 15
15 = 1 & 5, take the last number ..
10-5 = 5
New number = "543215"

The thing is I have no clue how to split this number into the seperate parts. Once I get past that I'm 99% sure I can wrap up. Anyone able to help? If so, Thanks! :D

Edited by miCRoSCoPiC^eaRthLinG, 30 December 2005 - 03:48 AM.


#2 miCRoSCoPiC^eaRthLinG

miCRoSCoPiC^eaRthLinG

    PsYcheDeLiC dR3aMeR

  • [MODERATOR]
  • 2,248 posts
  • Gender:Male
  • Location:Bangkok, Thailand
  • Interests:Photography, Magic Tricks, Numismatics & Philately to some extent, Being a nuisance in general (that's my favourite)
  • myCENTs:NEGATIVE[-21.50]

Posted 30 December 2005 - 03:48 AM

That should be fairly easy with the String manipulation routines that accompany VB. There are infact two methods you can follow.

Method 1:
[tab][/tab]To start with, you can simple convert the number into a string - and then use the substring function to find out each character. To achieve this we'll have to find the length of the string - which is 5, in your example above - and then iterate through each position of the string, picking up individual characters as we go.
'Say my number is stored in a variable called Num
Dim Num As Integer = 54321

'First I declare two variables - actually one Array to hold each character
'We just assing a random number of elements to the array - having 32
'will help us deal with numbers as long as 32 digits long
Dim Digits (32) As Integer

'And one variable to hold the length of the string
Dim Length As Integer

'I convert it to a String first
Dim NumString As String = CType ( Num, String )

'This NULL-Check is implemented so that if you use an empty string here, the code
'won't crash when it comes to checking for the substring - but instead would
'gracefully skip this part altogether
If NumString <> "" Then
  
    'Now I find the length of the string -  
    Length = NumString.Length

    'Iterate through the string, picking up each character
    For Index As Integer = 0 To Length - 1
        Digits ( Index ) = CType ( NumString.SubString ( Index, 1 ), Integer )
    Next

End If

That's it - the above approach will give you an array called Digits() whose positions indexed by 0 through Length - 1 will contain the individual digits of your number.

[hr=noshade][/hr]
Method 2:
[tab][/tab]The second method essentially involves the same principle as above - but computationally is a little faster - as the algorithm is based on numerical computations. A few words on the algorithm first.

[tab][/tab]Take for example, your own number, 54321 - can you think of a concrete method of stripping the number off the first digit "5" - leaving behind only "4321" ??
What would give you 5 as a result, if you divide 54321 by it ? You'd instantly arrive on the figure 10,000.
So: 54321 / 10000 = 5
This is all fine - you've got your first digit. But then how do you get the rest of the number together ? Arithmatically, if you divide 54321 by 10000 - we'd have a remainder of 4321 - that's exactly what we need. So how do you find the remainder. Here's where an arithmatic operator called Mod comes into play. It's known as Mod in terms of Visual Basic - and mostly represented as a % symbol in languages like C/C++, Java etc.
So: 54321 Mod 10000 = 4321

There - we have our first digit extracted, leaving behind the last four. Now if you repeat the above steps, except that you divide and do the Mod with 1,000 instead of 10,000 - you'd yet extract one more digit leaving behind the last 3. Next you repeat the steps again with 100 and then with 10 - and you'd have all your digits separated out.

How do we implement this ? Here's the code:
'Say Num contains the number
Dim Num As Integer = 54321

'Once again - we find the length of the number - which gives us the number of
'digits in it, and then we declare an array based on that number
Dim Length As Integer
If Str ( Num ) <> "" Then
    Length = Str ( Num ).Length
End If

'Next we declare two variables and an array
Dim Divisor As Integer = 10 ^ ( Length - 1 )
Dim Index As Integer = 0
Dim Digits ( Length - 1 ) As Integer

'Here we start the Loop
While ( Divisor > 0 )
    
    'Extract the first digit
    Digits ( Index ) = Int ( Num / Divisor )
    
    'Extract remainder number - and store it back in Num
    Num = Num Mod Divisor

    'Decrease Divisor's value by 1/10th units
    Divisor /= 10

    'Increment Index
    Index += 1

End While

Even in this case, your array Digits() will contain the extracted digits starting from position 0 through Length - 1.

[tab][/tab]Try out both and let me know - both should work just fine, giving your the same output. Personally, I find the second method more ELEGANT. The first one is a little blunt - doing things by brute force, although is much easier to understand. However, as I mentioned earlier, the second one computes way faster.

All the best :D
m^e

#3 hatim

hatim

    Advanced Member

  • Members
  • 196 posts
  • Location:Topi,Swabi,NWFP,Pakistan
  • Interests:Linux,Unix,Open Source,Programming, C++,Java,PHP,Technology,News,General Knowldge,Travel

Posted 30 December 2005 - 07:03 AM

Well it would be relevant if you posted which language you wanted this solution for. I think converting a number into string and then converting into numbers again is an easy solution. In C++ probably the shift operatros would do the trick.

#4 miCRoSCoPiC^eaRthLinG

miCRoSCoPiC^eaRthLinG

    PsYcheDeLiC dR3aMeR

  • [MODERATOR]
  • 2,248 posts
  • Gender:Male
  • Location:Bangkok, Thailand
  • Interests:Photography, Magic Tricks, Numismatics & Philately to some extent, Being a nuisance in general (that's my favourite)
  • myCENTs:NEGATIVE[-21.50]

Posted 30 December 2005 - 07:21 AM

Oh yeah - that reminds me, since you made the post in VB.NET forum - the solution provided was in that same language too.

Also hatim - if you notice the solution I provided in Method 1 does exactly the same - conversion to string, extraction and then back to integer - and the second method entirely eliminates the need to do that, thus making the algorithm far more efficient.

#5 derouge

derouge

    Advanced Member

  • Members
  • 111 posts

Posted 03 January 2006 - 03:28 AM

Since I hate to leave people hanging ..

I went with the second method, and it works excellent! Much thanks. :D

#6 miCRoSCoPiC^eaRthLinG

miCRoSCoPiC^eaRthLinG

    PsYcheDeLiC dR3aMeR

  • [MODERATOR]
  • 2,248 posts
  • Gender:Male
  • Location:Bangkok, Thailand
  • Interests:Photography, Magic Tricks, Numismatics & Philately to some extent, Being a nuisance in general (that's my favourite)
  • myCENTs:NEGATIVE[-21.50]

Posted 03 January 2006 - 04:04 AM

Awesome. Glad that helped :D That second method was a chance discovery while I was experimenting with coding a bunch of numerical manipulations quite sometime back - in relation to this cryptographic routine that I was trying to come up with.

#7 Guest_(G)willie_*

Guest_(G)willie_*
  • Guests

Posted 28 September 2010 - 07:44 AM

split and suming numbers in javaA Small Project: Splitting An Integer Into Digits

how do I split up a four digit number in java and sum up the individual numbers to get their sum?

-reply by willie

 





Reply to this topic



  


0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users