Author |
Message |
Blade
|
Posted: Sun Mar 30, 2003 10:52 am Post subject: Manipulate Integers |
|
|
how can you get the digits from integers... what i want to do is take a number... get the last digit, delete it then subtract it from the whole number... |
|
|
|
|
|
Sponsor Sponsor
|
|
|
azndragon
|
Posted: Sun Mar 30, 2003 11:29 am Post subject: (No subject) |
|
|
This is used with the () after calling the variable. You do this:
code: | var number : int
var othernumber : string
var newnumber : int
put "Enter a number"
get number
othernumber := intstr (number)
othernumber := othernumber (length (othernumber) - 1 .. length (othernumber) - 1)
newnumber := strint (othernumber)
number := number - newnumber
put number |
Basically, this program converts the input into a string, takes the last letter, converts it back to an integer, and subtracts it from the original number. |
|
|
|
|
|
Blade
|
Posted: Sun Mar 30, 2003 11:39 am Post subject: (No subject) |
|
|
yeah, thats pretty much what i did... i thought there was a way i could do it without converting it to a string... but i guess not eh.. |
|
|
|
|
|
Tony
|
Posted: Sun Mar 30, 2003 2:00 pm Post subject: (No subject) |
|
|
well if its just the last number, you can divide the whole number by 10 then subtract same number rounded down then multiply back by 10...
since the line above doesnt make sence even to myself, I'll just show you the code:
code: |
lastDigit := ( (number/10) - floor(number/10) ) * 10
|
|
Tony's programming blog. DWITE - a programming contest. |
|
|
|
|
azndragon
|
Posted: Sun Mar 30, 2003 2:13 pm Post subject: (No subject) |
|
|
Yeah, Tony's suggestion works well. I just don't know the shortcuts in programming, but then again, I've only been programming for about 4 months now, maybe experience allows you to learn the good shortcuts. |
|
|
|
|
|
Tony
|
Posted: Sun Mar 30, 2003 3:37 pm Post subject: (No subject) |
|
|
well its more of being able to look at problem from different perspectives...
while in azndragon's solution he used turings ablitity to turn integer into a string (which he is able to manipulate well)... I used mathimatical approach.
its all about "thinking outside the box" and trying out new ways of doing things. I suppose that comes with experimenting or having a creative mind to begin with. (Note: you'll not get it unless you try it... such as experience is nothing without experimenting) |
Tony's programming blog. DWITE - a programming contest. |
|
|
|
|
azndragon
|
Posted: Sun Mar 30, 2003 4:03 pm Post subject: (No subject) |
|
|
tony wrote: well its more of being able to look at problem from different perspectives...
while in azndragon's solution he used turings ablitity to turn integer into a string (which he is able to manipulate well)... I used mathimatical approach.
its all about "thinking outside the box" and trying out new ways of doing things. I suppose that comes with experimenting or having a creative mind to begin with. (Note: you'll not get it unless you try it... such as experience is nothing without experimenting)
It's kinda ironic that I didn't take the mathematical approach, because if you know me, I'm all math (which is pretty much all programmers should be ) |
|
|
|
|
|
|