Answer the question
In order to leave comments, you need to log in
How to output values in reverse order?
Hello.
I am going through the material again according to the book of Deitels, there is a task: transfer from decimal to other number systems.
Translation from q10 to q2
int q = 2; // основание системы счисления
int N = 13; // число
int r = 0; // остаток
while (N > q) {
r = N % q;
N = N / q;
cout << r;
if (N < q) {
cout << N;
}
cout << flush;
}
Answer the question
In order to leave comments, you need to log in
For this, you must first find the maximum digit of the number in the specified number system and then output it in the desired order. And get used to giving meaningful names to variables, otherwise you yourself will get confused later.
int value = 13;
int base = 2;
int rank = 1;
while (rank*base <= value)
rank *= base;
while (rank) {
printf("%d", value/rank);
value %= rank;
rank /= base;
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question