How can I make it print the last digits in reverse order of input?
I can't figure it out.
This is what I have so far:
#include %26lt;iostream%26gt;
using namespace std;
int main () {
int n;
cin %26gt;%26gt; n;
while (n%26lt;=0 %26amp;%26amp; n%26gt;=100) {
exit(0);
cout %26lt;%26lt; "Enter a positive number that is as most 100: ";
cin %26gt;%26gt; n;
}
for (int i=1; i%26lt;=n; i++) {
cout %26lt;%26lt; n;
n--;
}
return 0;
}
C++ Help Needed!?
instead of:
for (int i=1; i%26lt;=n; i++)
{
cout %26lt;%26lt; n;
n--;
}
..... say .....
while (n)
{
cout %26lt;%26lt; n%10;
n/=10;
}
You're a good bloke for doing most of the work yourself, and trying hard, so here's your treat:
#include %26lt;iostream%26gt;
using namespace std;
int main ()
{
int n=0;
while (n%26lt;1 || n%26gt; 10000)
{
cout %26lt;%26lt; "Enter a positive number that is at most 10000: ";
cin %26gt;%26gt; n;
}
cout %26lt;%26lt; endl;
while (n)
{
cout %26lt;%26lt; n%10;
n/=10;
}
cout %26lt;%26lt; endl;
return 0;
}
// The best thanks you could give me is not 'thank you' or even 'best answer', but wrapping your head around this code to understand why it works. I've made numerous changes...
Reply:First, does the value entered have to be converted to an integer, or can it remain a string of characters? If it can, print out the characters starting at the right end and stopping at the left.
If it has to be an integer, extract the right-most digit of the integer and print that, then work your way to the left. Hint: ( n % 10 ) should get it for you.
Hope that helps.
Reply:print out the characters starting at the right end and stopping at the left.
If it has to be an integer, extract the right-most digit of the integer and print that, then work your way to the left.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment