I
I
igoodmood2015-09-10 14:37:31
Programming
igoodmood, 2015-09-10 14:37:31

How to break any three-digit number (entered from the keyboard) into three separate digits?

How can you decompose any three-digit number into three separate digits?

Answer the question

In order to leave comments, you need to log in

6 answer(s)
K
Kirill Romanov, 2015-09-10
@Djaler

Either convert the number to a string and take the individual characters, or use the remainder of division by 10 and division by 10

Z
Zelimkhan Beltoev, 2015-09-10
@Beltoev

Something like this, if pseudocode:

число = 123
первое_число = число div 100
второе_число = (число div 10) mod 10
третье_число = число mod 10

A
angru, 2015-09-10
@angru

this is python, but for all other languages ​​it's almost the same

a = 368

for x in [100, 10, 1]:
    print(a // x)  # целочисленное деление
    a %= x  # остаток от деления

version for arbitrary positive number:
from math import log10


a = 1234567890

assert a >= 0, "только для положительных значений"

for x in range(int(log10((a // 10 * 10) or 1)), -1, -1):
    x = 10 ** x
    print(a // (x))
    a %= x

D
Daniil Smirnov, 2015-09-18
@antonsosnitzkij

There is a rather interesting method for solving the problem: double dabble .
We turn the binary code into BCD: each digit of a number is represented as 4 bits. The solution may not seem the most optimal, but it is definitely original and, moreover, does not require the hardware to use the division operation and use the division remainder function. The article itself has code in both VHDL and C.

V
Vladimir Martyanov, 2015-09-10
@vilgeforce

The tag "iron" has nothing to do with it. Your task is solved using string functions, see what your language has to select a substring.

P
pashacompany, 2016-02-07
@pashacompany

It's simple:
'this is a comment
For I = 1 To 3 ' loop. 3ka is the capacity of the divisible number. At least 100 bits.
x = Number Mod 10 ' The number must be a byte without commas!
Print x ' this is where the last digit of the number appears
Number = Number / 10 ' here the number loses the last digit
Next
'For understanding: other options:
Dim Digit(4) As Byte ' an array of bytes (integers without a comma!)
Digit(4) = 123 'divide 123 by 1,2,3
Digit(1) = Digit(4) / 100 '123/100=1
Digit(3) = Digit(4) Mod 10 'modulo 123 divided by 10 = 3
Digit(4) = Digit(4) / 10 ' 123/10=12
Digit(2) = Digit(4) mod 10 ' modulo 12 divided by 10 =2
Print Digit(1) ; Digit(2) ; Digit(3) ' 1 2 3 print our three digits
' Second option:
Dim I As Byte , Digit(4) As Byte
Digit(4) = 123 'split 123 into 1,2,3
For I = 3 To 1 ' loop . 3ka is the capacity of the divisible number. At least 100 bits. But the array must be the same
Digit(i) = Digit(4) Mod 10
Digit(4) = Digit(4) / 10
Next
Print Digit(1) ; Digit(2) ; Digit(3)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question