Count the number of digits in a long integer entered by a user.

Simple Iterative Solution
The integer entered by the user is stored in variable n. Then the while loop is iterated until the test expression n != 0 is evaluated to 0 (false).
- After first iteration, the value of n will be 345 and the count is incremented to 1.
- After second iteration, the value of n will be 34 and the count is incremented to 2.
- After third iteration, the value of n will be 3 and the count is incremented to 3.
- At the start of fourth iteration, the value of n will be 0 and the loop is terminated.
Then the test expression is evaluated to false and the loop terminates.
C++
// Iterative C++ program to count // number of digits in a number #include <bits/stdc++.h> using namespace std; int countDigit(long long n) { int count = 0; while (n != 0) { n = n / 10; ++count; } return count; } // Driver code int main(void) { long long n = 345289467; cout << "Number of digits : " << countDigit(n); return 0; } // This code is contributed // by Akanksha Rai |
C
// Iterative C program to count number of // digits in a number #include <stdio.h> int countDigit(long long n) { int count = 0; while (n != 0) { n = n / 10; ++count; } return count; } // Driver code int main(void) { long long n = 345289467; printf("Number of digits : %d", countDigit(n)); return 0; } |
Java
// JAVA Code to count number of // digits in an integer class GFG { static int countDigit(long n) { int count = 0; while (n != 0) { n = n / 10; ++count; } return count; } /* Driver program to test above function */ public static void main(String[] args) { long n = 345289467; System.out.print("Number of digits : " + countDigit(n)); } } // This code is contributed by Arnav Kr. Mandal. |
Python3
# Iterative Python program to count # number of digits in a number def countDigit(n): count = 0 while n != 0: n //= 10 count+= 1 return count # Driver Code n = 345289467print ("Number of digits : % d"%(countDigit(n))) # This code is contributed by Shreyanshi Arun |
C#
// C# Code to count number of // digits in an integer using System; class GFG { static int countDigit(long n) { int count = 0; while (n != 0) { n = n / 10; ++count; } return count; } /* Driver program to test above function */ public static void Main() { long n = 345289467; Console.WriteLine("Number of" + " digits : " + countDigit(n)); } } // This code is contributed by anuj_67. |
PHP
<?php // Iterative PHP program to count // number of digits in a number function countDigit($n) { $count = 0; while ($n != 0) { $n = round($n / 10); ++$count; } return $count; } // Driver code $n = 345289467; echo "Number of digits : " . countDigit($n); //This code is contributed by mits ?> |
Output :
Number of digits : 9
Recursive Solution:
C++
// Recursive C++ program to count number of // digits in a number #include <bits/stdc++.h> using namespace std; int countDigit(long long n) { if (n == 0) return 0; return 1 + countDigit(n / 10); } // Driver code int main(void) { long long n = 345289467; cout << "Number of digits :" << countDigit(n); return 0; } // This code is contributed by Mukul Singh. |
C
// Recursive C program to count number of // digits in a number #include <stdio.h> int countDigit(long long n) { if (n == 0) return 0; return 1 + countDigit(n / 10); } // Driver code int main(void) { long long n = 345289467; printf("Number of digits : %d", countDigit(n)); return 0; } |
Java
// JAVA Code to count number of // digits in an integer import java.util.*; class GFG { static int countDigit(long n) { if (n == 0) return 0; return 1 + countDigit(n / 10); } /* Driver program to test above function */ public static void main(String[] args) { long n = 345289467; System.out.print("Number of digits : " + countDigit(n)); } } // This code is contributed by Arnav Kr. Mandal. |
Python3
# Recursive Python program to count # number of digits in a number def countDigit(n): if n == 0: return 0 return 1 + countDigit(n // 10) # Driver Code n = 345289467print ("Number of digits : % d"%(countDigit(n))) # This code is contributed by Shreyanshi Arun |
C#
// C# Code to count number of // digits in an integer using System; class GFG { static int countDigit(long n) { if (n == 0) return 0; return 1 + countDigit(n / 10); } /* Driver program to test above function */ public static void Main() { long n = 345289467; Console.WriteLine("Number of " + "digits : " + countDigit(n)); } } // This code is contributed by anuj_67. |
PHP
<?php // Recursive PHP program to count // number of digits in a number function countDigit($n) { if ($n == 0) return 0; return 1 + countDigit((int)($n / 10)); } // Driver Code $n = 345289467; print ("Number of digits : " . (countDigit($n))); // This code is contributed by mits ?> |
Output :
Number of digits : 9
Log based Solution:
We can use log10(logarithm of base 10) to count the number of digits of positive numbers (logarithm is not defined for negative numbers).
Digit count of N = upper bound of log10(N).
C++
// Log based C++ program to count number of // digits in a number #include <bits/stdc++.h> using namespace std; int countDigit(long long n) { return floor(log10(n) + 1); } // Driver code int main(void) { long long n = 345289467; cout << "Number of digits : " << countDigit(n); return 0; } // This code is contributed by shivanisinghss2110 |
C
// Log based C program to count number of // digits in a number #include <math.h> #include <stdio.h> int countDigit(long long n) { return floor(log10(n) + 1); } // Driver code int main(void) { long long n = 345289467; printf("Number of digits : %d", countDigit(n)); return 0; } |
Java
// JAVA Code to count number of // digits in an integer import java.util.*; class GFG { static int countDigit(long n) { return (int)Math.floor(Math.log10(n) + 1); } /* Driver program to test above function */ public static void main(String[] args) { long n = 345289467; System.out.print("Number of digits : " + countDigit(n)); } } // This code is contributed by Arnav Kr. Mandal. |
Python3
# Log based Python program to count number of # digits in a number # function to import ceil and log import math def countDigit(n): return math.floor(math.log(n, 10)+1) # Driver Code n = 345289467print ("Number of digits : % d"%(countDigit(n))) # This code is contributed by Shreyanshi Arun |
C#
// C# Code to count number of // digits in an integer using System; class GFG { static int countDigit(long n) { return (int)Math.Floor(Math.Log10(n) + 1); } /* Driver program to test above function */ public static void Main() { long n = 345289467; Console.WriteLine("Number of digits : " + countDigit(n)); } } // This code is contributed by anuj_67.. |
PHP
<?php // Log based php program to // count number of digits // in a number function countDigit($n) { return floor(log10($n)+1); } // Driver code $n = 345289467; echo "Number of digits : ", countDigit($n); // This code is contributed by ajit ?> |
Output :
Number of digits : 9
Method 4:
We can convert the number into string and then find the length of the string to get the number of digits in the original number.
C++
#include <bits/stdc++.h> using namespace std; // To count the no. of digits in a number void count_digits(int n) { // converting number to string using // to_string in C++ string num = to_string(n); // calculate the size of string cout << num.size() << endl; } // driver function int main() { // number int n = 345; count_digits(n); return 0; } // This code is contributed by Shashank Pathak |
Java
import java.util.*; public class GFG { // To count the no. of digits in a number static void count_digits(int n) { // converting number to string using // to_string in C++ String num = Integer.toString(n); // calculate the size of string System.out.println(+ num.length()); } // Driver code public static void main(String args[]) { // number int n = 345; count_digits(n); } } // Code is contributed by shivanisinghss2110 |
Python3
# Python3 implementation of the approach def count_digits(n): n = str(n) return len(n) # Driver code n = 456533457776print(count_digits(n)) |
C#
// C# implementation of the above approach using System; using System.Collections.Generic; class GFG { // To count the no. of digits in a number static void count_digits(int n) { // converting number to string using // to_string in C# string num = Convert.ToString(n); // calculate the size of string Console.WriteLine(+ num.Length); } // Driver Code public static void Main(string[] args) { // number int n = 345; count_digits(n); } } // This code is contributed by shivanisinghss2110 |
This article is contributed by Vishal Kumar Gupta. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- Minimum integer that can be obtained by swapping adjacent digits of different parity
- Numbers of Length N having digits A and B and whose sum of digits contain only digits A and B
- Minimum digits to be removed to make either all digits or alternating digits same
- Count even and odd digits in an Integer
- Different methods to reverse a string in C/C++
- Analysis of Different Methods to find Prime Number in Python
- Count different numbers that can be generated such that there digits sum is equal to 'n'
- Count different numbers possible using all the digits their frequency times
- Program to check if a given number is Lucky (all digits are different)
- Count of numbers between range having only non-zero digits whose sum of digits is N and number is divisible by M
- Count of integers in a range which have even number of odd digits and odd number of even digits
- Count numbers in given range such that sum of even digits is greater than sum of odd digits
- Count of numbers upto N digits formed using digits 0 to K-1 without any adjacent 0s
- Minimum decrements to make integer A divisible by integer B
- Different ways for Integer to String Conversions In Java
- Print first k digits of 1/n where n is a positive integer
- Reverse digits of an integer with overflow handled
- Partition the digits of an integer such that it satisfies a given condition
- Sum of digits written in different bases from 2 to n-1
- Count of binary strings of length N having equal count of 0's and 1's and count of 1's ≥ count of 0's in each prefix substring

