dimanche 27 février 2022

how to return how many times has a recursive function been called in C++?

void funct(int n)
{
    int steps = 0;
    if(n != 1){
        steps++;
        if((n % 2) == 0){
            funct(n/2);
        }
        else if((n % 2) != 0){
            funct((3*n + 1));
        }
        cout << steps << " ";

    }
    
}

I tried this code, but it only returns 1 in a few times and i want my code to return how many times has 1 been returned.

Aucun commentaire:

Enregistrer un commentaire