Answer to Question #19181 in C++ for abadi

Question #19181
Write a program to reverse the digits of a positive integer number. For example, if the number 8735 is entered, the number displayed should be 5378. (hint: Use a do statement and continuously strip off and display the units digit of the number. If the variable num initially contains the number entered, the units digit is obtained as (num % 10). After a units digit is displayed, dividing the number by 10 sets up the number for the next iteration. Thus, (8735 % 10) is 5 and (8735 / 10) is 873. The do statement should continue as long as the remaining number is not zero.
Expert's answer

#include <iostream>
using namespace std;

int reverse(int,int);

int main()
int number;
int n;

cout << " Enter number to reverse." << endl;
cin >> number;
cout << reverse(number % 10,0);

return 0;
}//end main

void reverse(int number){
if(number == 0) //base/basic case i.e if number is zero the problem is already
solved, nothing to do, so simply return
cout << number % 10; // print that last digit, e.g 103 == 3
reverse(number/10); //solve the same problem but with smaller number, i.e make
the problem smaller by dividing it by 10, initially we had 103, now 10

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!


No comments. Be first!

Leave a comment

Ask Your question

New on Blog