Write in a c++ recursion function to prints the digits in reverse order using a recursion function. Input: 23567 Output: 76532
Answer:
Here is the c++ code as per your requirement
Raw code:
//header
#include <iostream>
//namespace standard
using namespace std;
//functin declaration
int reverseDigits(int num);
int main() {
//declaring variables
int num,reverse_number;
//get input from user
cin>>num;
//calling the function
reverse_number=reverseDigits(num);
//print the reversed number.
cout<<reverse_number<<endl;
}
//declaring variables
int sum=0,rem;
//function declaration
int reverseDigits(int num){
//positive number
if(num){
//get the remainder of it
rem=num%10;
sum=sum*10+rem;
//get the remaining sum
//send the remaining digts to recusrsive function
reverseDigits(num/10);
}
//else return sum as 0
else{
return sum;
}
return sum;
}
Editor:
output:
Hope this helps you! If you still have any doubts or queries please feel free to comment in the comment section.
"Please refer to the screenshot of the code to understand the indentation of the code".
Thank you! Do upvote.
Get Answers For Free
Most questions answered within 1 hours.