Yes or No While Loops

I need help with the following assignment:

Write a program which takes a positive integer as input and outputs its digits in reverse. Use integer arithmetic (/ and %) to accomplish this. Always assume the input can fit within the type you use. Give the user an option to repeat the exercise.
Here is a sample run:

Enter a positive integer: 38475
This integer in reverse is 57483.
Would you like to do this again? (y/n) y
Enter a positive integer: 9584
This integer in reverse is 4859.
Would you like to do this again? (y/n) n
Thank you!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
using namespace std;

int main()
{
	int num, i = 10;
	cout << "Enter a positive integer: ";
	cin >> num;
	cout << " This integer in reverse is ";

	do
	{
		cout << (num%i) / (i / 10);
		i *= 10;
	} while ((num * 10) / i != 0);

	return 0;
}


So far I am able to reverse the digits of any positive integer. I just don't know how to loop it upon a "yes" or "no" request. Please help me with this assignment.
Just put everything inside another do-while loop, like so:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <iostream>
using namespace std;

int main()
{
       
    char answer;
       
    do{
           int num, i = 10;
	   cout << "Enter a positive integer: ";
	   cin >> num;
 	   cout << " This integer in reverse is ";
	
	    do{
		 cout << (num%i) / (i / 10);
		 i *= 10;
	   }while ((num * 10) / i != 0);
	
	   cout << "\nWould you like to do this again? (Y/N) ";
	   cin >> answer;
    }while(toupper(answer) == 'Y');
    
    cout << "Program terminated...." << endl;
    
    
	return 0;
}


The char variable 'answer' is declared outside the loop so it can be in scope for the final while condition.
Last edited on
Topic archived. No new replies allowed.