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.
1
Expert's answer
2012-11-21T09:42:32-0500
#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 return; else{ 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 } }
Numbers and figures are an essential part of our world, necessary for almost everything we do every day. As important…
APPROVED BY CLIENTS
Finding a professional expert in "partial differential equations" in the advanced level is difficult.
You can find this expert in "Assignmentexpert.com" with confidence.
Exceptional experts! I appreciate your help. God bless you!
Comments
Leave a comment