Home > Mobile >  How can I get numbers from number (like this : 731===>[7,3,1]) with recursive function
How can I get numbers from number (like this : 731===>[7,3,1]) with recursive function

Time:12-04

Here I can use down method to do this

Get a whole number (192)============> (1,9,2)

#include <iostream> // I Know This way

using namespace std;

int argam(int n);
int main()
{
    int a;
    cout << "Please enter num : ";
    cin >> a;
    argam(a);
}

int argam(int n)
{

    do
    {
        cout << n % 10 << "\n";
        n /= 10;
    } while (n > 0);
}
5
4
3

I want to get same answer with recursive function.

CodePudding user response:

It's even shorter when done recursively:

void argam(int n)
{
    if (n == 0) return; // end condition for the recursion
    cout << n % 10 << "\n"; // print 1 digit
    argam(n / 10); // call the same function again without that digit
}
  •  Tags:  
  • c
  • Related