Hey,
I was just thinking and came across this idea and wanted to know if it could be done, say if the user enters 321, my program would output 123, 450, 054 etc etc

Could you please show me how this could be achieved thanks.

Dani AI

Generated

Nice thought exercise. As noted, you can either treat the input as text or as a number. The key issue you ran into is overwriting the original value and then trying to print the parts, plus that stray comma operator in your cout chain. Also note: if the user expects 450 -> 054, printing an integer will drop the leading 0; you either need to pad the output or work with strings. Avoid conio.h/getch() here; they are non‑standard and unnecessary.

If you want the numeric reverse and to keep the same digit width (so 450 becomes 054), accumulate into a new variable instead of reusing the source:

#include <iostream>
#include <iomanip>
#include <string>

unsigned long long reverse_digits(unsigned long long n) {
    unsigned long long r = 0;
    do { r = r * 10 + (n % 10); n /= 10; } while (n);
    return r;
}

int main() {
    long long x;
    std::cout << "Enter number: ";
    if (!(std::cin >> x)) return 0;

    bool neg = x < 0;
    unsigned long long u = neg ? static_cast<unsigned long long>(-x)
                               : static_cast<unsigned long long>(x);

    std::string digits = std::to_string(u);              // width to preserve
    unsigned long long rev = reverse_digits(u);

    if (neg) std::cout << '-';
    std::cout << std::setw(digits.size()) << std::setfill('0') << rev << '\n';
}

If you want an exact character mirror (preserving whatever the user typed, including any leading zeros), read a string and reverse the characters. This avoids integer parsing entirely and side‑steps overflow for long inputs:

#include <iostream>
#include <string>

int main() {
    std::string s;
    std::cout << "Enter number: ";
    if (!(std::cin >> s)) return 0;

    std::string mirrored(s.rbegin(), s.rend());
    std::cout << mirrored << '\n';
}

’s divide/mod hint is spot on; just funnel those digits into a result instead of clobbering your input.

Recommended Answers

All 15 Replies

>wanted to know if it could be done
If you find yourself asking if "<insert something here> can be done in C++", just go ahead and assume the answer is yes. It probably is.

>Could you please show me how this could be achieved thanks.
How about I tell you and you try it out on your own?

Solution 1: Convert the number into a string and reverse the string.
Solution 2: Use number % base to extract the least significant digit and print it until the number is 0. This method gives you the digits in reverse by default.

A hint, divide a number by 10. The quotient would be the number except for the least significant digit and the remainder would be the least significant digit. Use these two in combination to get your answer

>about how this could be achieved i meant solutions
I gave you two solutions. The code is trivial.

>for "number % base" would i change them to my variable names
If you want it to work. In particular, if I have an integer called value and I want the least significant base 10 digit, I would say value % 10 .

Why don't you actually try these things instead of look for existing code and expecting handouts? You'll learn a lot more that way.

[edit]
It's terribly rude to replace your post with something completely different after submitting it.
[/edit]

I am not 100% sure on your solutions, so far i've just made this :

#include <conio.h>
#include <iostream>

using namespace std;

int main()
{
    unsigned int number, number2, number3;
    
    cout << "Enter number to reverse : ";
    cin >> number;
    
    number  = number % 10;
    number2 = number % 100;
    number3 = number % 1000;
    cout << endl << number, number2, number3;
    
    getch();
}

If you enter 123 it only outputs 3, am i on the right lines?

No you are not. The base of the numbers is 10 not 100 or 1000. And you have used cout the wrong way

cout << endl << number<< number2<<number3;

Sorry about cout, i've altered the %'s to be 10, but i get how i get the last digit, it's just the other two i'm not sure how to get even with your solutions i'm not very good

number1  = number % 10;
number=number/10;
number2 = number % 10;
number=number/10;
number3 = number % 10;

I edited my code and used hammerheads code, and when i inputted 123 it outputted 000.

#include <conio.h>
#include <iostream>

using namespace std;

int main()
{
    unsigned int number, number2, number3;
    
    cout << "Enter number to reverse : ";
    cin >> number;
    
    number  = number % 10;
    number  = number/10;
    number2 = number % 10;
    number  = number/10;
    number3 = number % 10;
    
    cout << endl << number << number2 << number3;
    
    getch();
}

You are not using your head and instead simply copy pasting the code. If you had read any of the posts above the mistake in my code would have been clear to you.

I'm really sorry but i cannot see what i have to do, i've tried "tweaking" things around and still get 000

sol1

#include <iostream>
#include <string>
using namespace std;

int main() {
	string num;
	cout << "Enter number: ";
	cin >> num;
	reverse(num.begin(), num.end());
	cout << num;
	return 0;
}

sol.2

#include <iostream>
using namespace std;

int main() {
	int num;
	cout << "Enter number: ";
	cin >> num;
	while(num){
		cout << num%10;
		num/=10;
	}
	return 0;
}

Wow, just wow. Here's the code you wanted, not that I think it'll help you any:

#include <iostream>

int main()
{
  int value;

  std::cout<<"Enter a number: ";

  if ( std::cin>> value ) {
    // The loop stops on 0, so
    // treat it as a special case
    if ( value == 0 )
      std::cout<< value <<'\n';
    else {
      while ( value != 0 ) {
        std::cout<< value % 10 <<'\n';
        value /= 10;
      }
    }
  }
}

Thank you all! I'm just sorry i was no help :(

>Thank you all!
Don't confuse the desire to help with the desire to shut you up. Personally, I don't see you getting very far with programming if you can't work these kinds of things out even after being told exactly how to do it.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.