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.

>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.

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 …

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;``

## 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.

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 learning and sharing knowledge.