A community for technology geeks in Lebanon.

You are not logged in.

- Topics: Active • Unanswered

Pages: **1**

**transhumanist.liberation****Member**

Like if we have an integer

a = 1234

b = a % 10 #extracts the final digit 4

what about If I wanna extract the 2nd to last digit 3? Similarly for 1 & 2

I am using Python 3

*Last edited by transhumanist.liberation (September 14 2020)*

**m0ei****Member**

Loop, modulo 10 then divide 10, Keep doing it until you reach the "index" you want.

Other approaches exist, for example convert to an array/list... then you get the index you want.

*Last edited by m0ei (September 15 2020)*

**potato****Member**

Id use regex https://en.m.wikipedia.org/wiki/Regular_expression

**jsaade****Member**

in most languages -> int.Parse(nbr.ToString()[index])

or of course use %

*Last edited by jsaade (September 14 2020)*

**transhumanist.liberation****Member**

Thx everyone for your help.

However I am writing a python program that adds the digits of a factorial product like 5! = 120 , sum = 1 + 2 + 0 = 3.

Here is my code:

```
import math
num = 10
fac = math.factorial(num)
list = []
n = 0
sum = 0
while fac > 0:
digit = (fac // 10 ** (n-1)) % 10
n += 1
list.append(digit)
for i in list:
sum+=i
break
print(sum)
```

**m0ei****Member**

1) don't name a variable sum, you'll overshadow the python built-in sum function, same for list.

2) fac value is always greater than 0, fac value has to change if you intend to exit that loop.

3) break will exit the nested for loop on the first iteration. You're also stuck in an infinite loop, because you have no ways to exist the while loop, the value of fac will always be greater than 0.

4) you don't need to use more than 1 loop if it's an assignment or if you're learning.

5) I have no idea what you're doing here (fac // 10 ** (n-1)) % 10 ... to get the last digit, just do `number % 10`

To solve it easily, you can simply do `sum([int(digit) for digit in str(fac)])`, this will convert the `fac` variable to a string (strings are iterable in Python), then it will loop on every digit and convert it to an integer. The built-in sum function will then loop on the list and calculate the sum for you.

You shouldn't do it this way if you're learning.

To solve it correctly, create a variable to hold that sum, loop while fac > 0, get the current digit by simply calling modulo 10 on that number, add that digit to that accumulating variable, change the value of fac to fac minus the last digit, basically fac // 10.

I didn't give you the answer directly, hopefully you got it.

*Last edited by m0ei (September 14 2020)*

**transhumanist.liberation****Member**

I don't want to extract the last digit only, I want to extract every single digit ie 1234

**m0ei****Member**

I know. I already told you.

Here's a sample:

123 % 10 = 3

123 / 10 = 12

12 % 10 = 2

12 / 10 = 1

1 % 10 = 1

1 / 10 = 0 => you exit the loop

Everytime you modulo, you take that number and add it to a variable (the sum). Everytime you divide, you take that number and use it in the next modulo.

**Hybrid****Member**

You can run a recursive function with %10 to get every single digit

Or you can convert it to a string and read it character by character

Sorry I don't know python, so can't help with the code

**xterm****Moderator**

```
n = 98765
as_list = list(map(int, str(n)))
as_list[0] => 9
as_list[1] => 8
...
```

If this is homework, I doubt your professor will accept it.

**Joe****Member**

transhumanist.liberation wrote:

Like if we have an integer

a = 1234

b = a % 10 #extracts the final digit 4

what about If I wanna extract the 2nd to last digit 3? Similarly for 1 & 2

I am using Python 3

What's

**a % 100**?What's

**[(a % 100) - (a % 10)] / 10?**What's

**[(a % 1000) - (a % 100)] / 100?**What's

**[(a % 10**n) - (a % 10**(n-1))] / 10**(n-1)**

This should get you where you want, but it's a convoluted way of doing it.

What others suggested, transform your number into a string, and access each element of the array by position. Remember to cast it back to a number before you do math on it.

**DON'T DO THIS**

This is bad coding style, but it's fun and it works. Here's how I sum the digits of the factorial of a number **n**:

```
import math
def f(n):
return sum([int(d) for d in str(math.factorial(n))])
```

While it's bad style to write this code, it's important that you can read it and understand it. If something doesn't make sense, ask here.

*Last edited by Joe (September 19 2020)*

Pages: **1**