I'm trying to find the n
th digit of an integer of an arbitrary length. I was going to convert the integer to a string and use the character at index n...
char Digit = itoa(Number).at(n);
...But then I realized the itoa
function isn't standard. Is there any other way to do this?
To find first digit of a number we divide the given number by 10 until number is greater than 10. At the end we are left with the first digit.
Approach: The idea is to skip (N-1) digits of the given number in base B by dividing the number with B, (N – 1) times and then return the modulo of the current number by the B to get the Nth digit from the right.
Python String isdigit() Method The isdigit() method returns True if all the characters are digits, otherwise False. Exponents, like ², are also considered to be a digit.
(number/intPower(10, n))%10
just define the function intPower
.
You can also use the % operator and / for integer division in a loop. (Given integer n >= 0, n % 10 gives the units digit, and n / 10 chops off the units digit.)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With