#Help to reverse output
21 messages · Page 1 of 1 (latest)
When your question is answered use !solved to mark the question as resolved.
Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question run !howto ask.
You’re starting with the last digit (least significant) and printing it
If you wanted it “in order”, you can print all the digits in reverse order in a separate loop
Also, you’re not incrementing i
It breaks when i increment i
#include <bits/stdc++.h>
using namespace std;
void digits_array(int number, int digits[]) {
int i = 0;
while (number > 0) {
digits[i] = number % 10;
number /= 10;
cout << digits[i] << " ";
}
}
int main() {
int number;
cin >> number;
int digits[0];
digits_array(number, digits);
return 0;
Full code
int reverse(int og_value)
{
int new_value=0,current_digit;
while(og_value>0)
{
current_digit=og_value%10;
new_value=new_value*10+current_digit;
og_value/=10;
}
return new_value;
}
Why?
An unhealthy habit of coding in C++
What exactly bad? My code works slower or what?
A Computer Science portal for geeks. It contains well written, well thought and well explained computer science and programming articles, quizzes and practice/competitive programming/company interview Questions.
The code won't work on compilers other than the GNU C++ compilers. That's all I know
It could compile slower and pollute the global namespace (potentially)
Also it doesn’t work on all compilers, very easy fix
TY
@dense steeple Has your question been resolved? If so, run !solved :)
!solved