Count Total Digits in a Number

PHOTO EMBED

Sun Feb 06 2022 22:43:14 GMT+0000 (Coordinated Universal Time)

Saved by @Uttam #java #gfg #geeksforgeeks #recursion #countdigits

class Solution
{
    public static int countDigits(int n)
    {
        if(n<10)
            return 1;
        else
            // recursively count the digits of n
            return 1+countDigits(n/10);
    }
}
content_copyCOPY

Count Total Digits in a Number You are given a number n. You need to find the count of digits in n. Example 1: Input: n = 1 Output: 1 Explanation: Number of digit in 1 is 1. Example 2: Input: n = 99999 Output: 5 Explanation:Number of digit in 99999 is 5 Your Task: You don't need to read input or print anything. Your task is to complete the function countDigits() that takes n as parameter and returns the count of digits in n. Expected Time Complexity: O(Logn). Expected Auxiliary Space: O(Logn). Constraints: 0 <= n <= 10^7

https://practice.geeksforgeeks.org/problems/count-total-digits-in-a-number/1/?track=DSASP-Recursion&batchId=190