How can I use the any digit of the int type number? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

How can I use the any digit of the int type number?

How do I take the number from any digit of an int number and use it as an int or useful other forms?

19th Jun 2019, 2:16 PM
Emir Dalfidan
Emir Dalfidan - avatar
3 Answers
+ 2
Specific digits of a number can be counted like this (index starts at 1 at the end of the number): (n % 10 ^ i - n % 10 ^ i - 1...) / 10 ^ i - 1 (at the start, it ends when i reaches 0) Example: 576 First digit (6), index 1: (576 % 10) / 10 ^ 0 -> 6 / 1 -> 6 Second digit (7), index 2: (576 % 100 - 576 % 10) / 10 ^ 1 -> (76 - 6) / 10 -> 70 / 10 -> 7 Or, you could turn it into a string and use the charAt method or if it's treated like an array, you could just get the index, that's why you should specify what language ;)
19th Jun 2019, 2:34 PM
Airree
Airree - avatar
0
what language ? in python you can just do n = 123456 print([int(i) for i in str(n)]) [1, 2, 3, 4, 5, 6]
19th Jun 2019, 4:48 PM
Choe
Choe - avatar
0
C#
19th Jun 2019, 4:50 PM
Emir Dalfidan
Emir Dalfidan - avatar