recursive function to count the sum of digits

i made an attempt at making a recursive function to count the digits of an integer number with a recursive function and have a few question:
1)is it faster or same as a do while loop that will do the same thing ?
2)is it possible to optimize it more than i already did?

the code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
  #include<iostream>

int sumOfDigits(int num);

int main() {
	std::cout << sumOfDigits(0) << std::endl; //change 0 to any number
	return 0;
}

int sumOfDigits(int num) {
	static int digit_counter{1};
	if ((num>=0)&&(num<=9)) return digit_counter;
	else { ++digit_counter; sumOfDigits(num / 10); }
} 
would changing the function to:

1
2
3
4
5
6
int sumOfDigits(int num) {
	static int digit_counter{1};
	num /= 10;
	if (num != 0) { ++digit_counter; sumOfDigits(num); }
	else return digit_counter;
} 


optimize the function a bit more or stay the same ?
Last edited on
Firstly, what you are finding is the number of digits, not sum of digits. The name of your function should reflect what it does.

Secondly, using recursion and static variables just to find the number of digits is an overly-complex way of solving the problem. Why not just use a simple while loop within a normal function?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
  #include<iostream>

int NumberOfDigits(int num);

int main() {
	std::cout << NumberOfDigits(123456) << std::endl; 
	return 0;
}

int NumberOfDigits(int num) {
    int digit_counter = 0;
	while(num > 0)
	{
	    num /= 10;
	    digit_counter++;
	}
	
	return digit_counter;
} 
Last edited on
Firstly, what you are finding is the number of digits, not sum of digits. The name of your function should reflect what it does.


doesn't sum mean addition of something ?
NOTE:my english isn't that good.

Secondly, using recursion and static variables just to find the number of digits is an overly-complex way of solving the problem. Why not just use a simple while loop within a normal function?


i know its possible to do it in a loop my version is:
1
2
3
4
5
6
7
8
int sumOfDigits(int num){
    int digit_counter{};
    do{
        num/=10;
        ++digit_counter;
    }while(num!=0);
    return digit_counter;
}


my point was to practice recursion and make the recursive function as effective as possible
doesn't sum mean addition of something ?
NOTE:my english isn't that good.


Yes it does, but here you are not adding up the digits, simply counting them.

my point was to practice recursion and make the recursive function as effective as possible


Then take a look at this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
  #include<iostream>

int NumberOfDigits(int num);

int main() {
	std::cout << NumberOfDigits(123456) << std::endl; //change 0 to any number
	return 0;
}

int NumberOfDigits(int num) {
	num /= 10;
	if(num == 0) return 1;
     return 1 + NumberOfDigits(num);
} 


Much simpler, and concise.
Last edited on
> is it faster or same as a do while loop that will do the same thing ?

More often than not, recursion is more expensive than iteration. Recursion is certainly very expensive if the programmer does not implement it properly and use tail recursion (Many compilers will actually recognize tail recursion and optimize it though).

But, recursion is clearer in many cases and there is a number of problems that are better modeled and more easily implemented with recursion than iteration. Try implementing Fibonacci sequences or tree traversal with both recursion and iteration and just see how natural recursion looks compared to iteration. Some problems solved with iteration may look complicated so despite the extra cost, people would prefer recursion.

There are even some problems that are actually less expensive when implemented with recursion although I can't really think of any examples right now but so I've read.

If you are like most programmers, you are probably more comfortable with iteration than recursion, but at least know which problems are better solved with recursion and learn it. Problems that can be broken down into multiple smaller problems or problems that require backtracking are good candidates for recursion.
Last edited on
Topic archived. No new replies allowed.