How to recursively count the digits of a number in C#

1 Answer

0 votes
using System;

class Program
{
    static int counter = 0;
    
    static int count_digits(int n) {
        if (n != 0) {
            counter++;
            count_digits(n/10);
        }
        
        return counter;
    }
    
    static void Main()
    {
        int n = 987237;

        Console.Write(count_digits(n));
    }
}


/*
run:

6

*/

 



answered Apr 5, 2019 by avibootz

Related questions

1 answer 148 views
1 answer 163 views
1 answer 158 views
1 answer 157 views
1 answer 174 views
1 answer 151 views
1 answer 581 views
...