Question

Write a C++ program that reverses the digits of a given number using recursion

Write a C++ program that reverses the digits of a given number using recursion

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include <iostream>

#include <cstdlib>

using namespace std;

//Recursive Function Declaration

int reverse_rec (int number, int m) ;

int main(){

      int number;

      int m=0;

      cout << "Enter number to reverse: ";

      //Getting the input number from user

      cin >> number;

      //Calling the recursive function to reverse the digits

      cout <<"Reverse of the "<< number<<": "<< reverse_rec(number,0) << endl; //calling the function

      return 0;

}

//Recursive function to reverse the digits

int reverse_rec(int number,int m){

      if(number==0)

            return m; //base (exit condition)

      m*=10;

      m+=number%10;

      return reverse_rec(number/10,m); //recursive function called here

}

Output:

Enter number to reverse: 1234567 Reverse of the 1234567: 7654321 execution time 5.310 s Process returned 0 (exe) Press anv ke

Add a comment
Know the answer?
Add Answer to:
Write a C++ program that reverses the digits of a given number using recursion
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT