I have a positive integer n , how can I display its numbers in reverse order?
- oneApparently, you tried to save this question and answer by overriding it again and giving the code and detailed comments. In other circumstances, I think it would be a good answer and a great closing, but ... you have chosen a not very suitable object for salvation. So do not like freeloader students who sit on the exam google answers or ask them live, photographing questions. I suggest you to concentrate on more appropriate questions and not to negative about the fact that for your good wishes they answered with minuses in reputation. - AK ♦
- one@AK I did not try to save that question, I just answered it and it immediately became clear that after a while the question would be automatically deleted. I just did not want to lose my answer, so I asked this question and it definitely will not be automatically deleted, regardless of rating (due to a check mark). As a result, among my questions this question ranks third in the number of views, in my opinion it is very cool. - user268670
|
2 answers
For example, using a while that looks like this:
while (условие) { // код, который будет выполняться пока условие истинно } Thus, a possible solution to the problem of writing the digits of a number in the reverse order:
int x; // получаем значение переменной x // например считаем с клавиатуры, вот так: // cin >> x; // выписываем цифры числа по одной, начиная с конца // в конце каждой итерации удаляем последнюю цифру числа while (x > 0) { // получаем последнюю цифру числа используя оператор взятия остатка от деления int last_number = x % 10; cout << last_number; // удаляем последнюю цифру числа используя оператор целочисленного деления x /= 10; } |
int x = ...; auto s = std::to_string(x); s.assign(s.rbegin(), s.rend()); std::cout << s << std::endl; |