U
U
Username2015-07-12 22:44:38
C++ / C#
Username, 2015-07-12 22:44:38

How to compare each digit in a number character by character?

Let's start with an example to understand what I mean. For example, there is a number 978560 and I want to compare each digit with each other and if the number on the left is less than on the right, then I swap them.
We go from the end of the number (6 < 0) (no), (5 < 6) (yes, we swap places) = 978650 and. etc.
How to implement this?

Answer the question

In order to leave comments, you need to log in

4 answer(s)
D
Dmitry Kovalsky, 2015-07-12
@dmitryKovalskiy

Calculate the remainder after dividing by 10. Get an array of digits from the end. Well, then compare neighbors and swap

O
one pavel, 2015-07-12
@onepavel

translate the number into a string,
the string is an array of charms ,
drive the array with any algorithms

D
Dmitry, 2015-07-13
@ubuntuman

Support: you can easily convert a number to a string (even with a dot) and work with it as with an array of single digits and separator characters. And the value of the number, again, can be obtained at any time from number[i]-'0' ('1' - '0' will be 1, '6'-'0' will be 6, etc.)

F
FloorZ, 2015-07-13
@FloorZ

Book Algorithms "Kormen" second chapter. A simple sorting algorithm. Exactly what you need.
Not hard to find in pdf format. At the top of the search immediately.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question