How to Count Number of Digits in a Number using Recursion

preview_player
Показать описание
In this tutorial, I have explained java program to count number of digits in a number using recursion.

How to count number of digits in a number using iterative approach.

Рекомендации по теме
Комментарии
Автор

correct approach should be
public static int countDigits(int n)
{

// in your example in case of zero this will simply return 0 where as it should be 1;
// so for zero it will give a wrong answer.
//hence we will use if(n<10) return 1;
if(n<10)
return 1;

return 1+countDigits(n/10);
}

adithyajain
Автор

if the number is given is 0, the output should be 1, but your output is 0

aloksingh-byux
join shbcf.ru