|
||||||
| C and C++ C and C++ forum for discussing all forms of C except for C#. These languages are powerful low level languages used for creating Operating Systems, Device Drivers, compilers and much more. |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Display Modes |
|
|||
|
I want to be able to extract separate digits from a numbe. Lets say the year is 1981 then i want to be able to put the digit 9 in a variable, then I also want to be able to put the whole number in an variable.
How can I do this? |
| Sponsored Links |
|
|
|
|||
|
Use the modulus base ten of the number after removing the preceding digits.
C++ Code:
To make it more generic, so that you can extract hex digits, just replace the tens with a variable called radix that'll be passed in as an argument. |
|
|||||
|
Adding to dargueta's response: the key to extracting digits is using integer and modular division. You could directly access the 9 in 1981 with (1981 / 100) % 10. The reason this works is that 1981/100 = 19, and 19 % 10 = 9.
__________________
CodeCall Blog | CodeCall Wiki | Shareware | Linux Forum Programming is a branch of mathematics. |
![]() |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | Search this Thread |
| Display Modes | |
|
|
| WingedPanther | ........ | 2753.6 |
| Xav | ........ | 2704 |
| Brandon W | ........ | 1702.32 |
| John | ........ | 1207.73 |
| marwex89 | ........ | 1175.24 |
| morefood2001 | ........ | 966.05 |
| dcs | ........ | 655.75 |
| Steve.L | ........ | 475.59 |
| orjan | ........ | 418.58 |
| Aereshaa | ........ | 383.54 |