94.0k views
1 vote
Write a recursive C++ function that writes the digits of a positive decimal integer in reverse order.

I need the recursive diagram I don't know how to do it.

1 Answer

3 votes

Answer:

// here is code in c++.

#include <bits/stdc++.h>

using namespace std;

// recursive function to print digit of number in revers order

void rev_dig(int num)

{

if(num==0)

return;

else

{

cout<<num%10<<" ";

// recursive call

rev_dig(num/10);

}

}

// driver function

int main()

{

int num;

cout<<"enter a number:";

// read the number from user

cin>>num;

cout<<"digits in revers order: ";

// call the function with number parameter

rev_dig(num);

return 0;

}

Step-by-step explanation:

Read the input from user and assign it to variable "num". Call the function "rev_dig" with "num" parameter.In this function it will find the last digit as num%10 and print it. Then call the function itself with "num/10". This will print the second last digit. Similarly it will print all the digits in revers order.

Output:

enter a number:1234

digits in revers order: 4 3 2 1

Diagram :

Write a recursive C++ function that writes the digits of a positive decimal integer-example-1
User Sanjeev Sangral
by
5.6k points