Example: input 1234, and get back 4321.
In C++ using Cin and Cout how can I input a 4 digit number and have it ouput that number reversed?
Put the number into a character array after receiving it throught the Cin (you may have to convert it to string first, although I think any item received from Cin comes in as a string), then iterate through it, starting at the back and moving forward, saving each item to another array. For example,
newCharAry(0) = origCharAry(3) etc.
Reply:Let's call your number is n
the method is as bellow
int n;
cin %26gt;%26gt; n;
cout %26lt;%26lt; a % 10;
a -= a % 10;
cout %26lt;%26lt; a % 100;
a -= a % 100;
cout %26lt;%26lt; a % 1000;
a -= a % 1000;
cout %26lt;%26lt; a % 10000;
Reply:when you recieve the 4 digit number convert it into a string.There is a function called strrev or something like that. C++ has a built in function to do that. You will have to import the string header. Then convert that value back into a number and cout %26lt;%26lt; int %26lt;%26lt;endl;
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment