Snippets Collections
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);
    }
}
//Time Complexity : O(d), where 'd' is the digits of number
import java.io.*;
import java.util.*;

public class CountDigits {

	static int countDigits(int num)
	{
		int count = 0;
    
		while(num > 0)
		{
			num = num / 10;
			count++;
		}	
		return count;
	}

	public static void main (String[] args) {
		
		int number = 789;

		System.out.println(countDigits(number));

	}
}
star

Sun Feb 06 2022 22:43:14 GMT+0000 (Coordinated Universal Time) https://practice.geeksforgeeks.org/problems/count-total-digits-in-a-number/1/?track=DSASP-Recursion&batchId=190

#java #gfg #geeksforgeeks #recursion #countdigits
star

Sun Feb 06 2022 02:25:06 GMT+0000 (Coordinated Universal Time) https://www.geeksforgeeks.org/program-count-digits-integer-3-different-methods/

#java #mathematics #lecture #gfg #geeksforgeeks #countdigits

Save snippets that work with our extensions

Available in the Chrome Web Store Get Firefox Add-on Get VS Code extension