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++
#include <bits/stdc++.h>
using namespace std;
int countDigit(long long n)
{
int count = 0;
while (n != 0) {
n = n / 10;
++count;
}
return count;
}
int main(void)
{
long long n = 345289467;
cout << "Number of digits : "
<< countDigit(n);
return 0;
}
|
C
#include <stdio.h>
int countDigit(long long n)
{
int count = 0;
while (n != 0) {
n = n / 10;
++count;
}
return count;
}
int main(void)
{
long long n = 345289467;
printf("Number of digits : %d",
countDigit(n));
return 0;
}
|
Java
class GFG {
static int countDigit(long n)
{
int count = 0;
while (n != 0) {
n = n / 10;
++count;
}
return count;
}
public static void main(String[] args)
{
long n = 345289467;
System.out.print("Number of digits : " + countDigit(n));
}
}
|
Python3
def countDigit(n):
count = 0
while n != 0:
n //= 10
count+= 1
return count
n = 345289467
print ("Number of digits : % d"%(countDigit(n)))
|
C#
using System;
class GFG {
static int countDigit(long n)
{
int count = 0;
while (n != 0) {
n = n / 10;
++count;
}
return count;
}
public static void Main()
{
long n = 345289467;
Console.WriteLine("Number of"
+ " digits : " + countDigit(n));
}
}
|
PHP
<?php
function countDigit($n)
{
$count = 0;
while ($n != 0)
{
$n = round($n / 10);
++$count;
}
return $count;
}
$n = 345289467;
echo "Number of digits : "
. countDigit($n);
?>
|
Output :
Number of digits : 9
Recursive Solution:
C++
#include <bits/stdc++.h>
using namespace std;
int countDigit(long long n)
{
if (n == 0)
return 0;
return 1 + countDigit(n / 10);
}
int main(void)
{
long long n = 345289467;
cout << "Number of digits :" << countDigit(n);
return 0;
}
|
C
#include <stdio.h>
int countDigit(long long n)
{
if (n == 0)
return 0;
return 1 + countDigit(n / 10);
}
int main(void)
{
long long n = 345289467;
printf("Number of digits : %d",
countDigit(n));
return 0;
}
|
Java
import java.util.*;
class GFG {
static int countDigit(long n)
{
if (n == 0)
return 0;
return 1 + countDigit(n / 10);
}
public static void main(String[] args)
{
long n = 345289467;
System.out.print("Number of digits : " + countDigit(n));
}
}
|
Python3
def countDigit(n):
if n == 0:
return 0
return 1 + countDigit(n // 10)
n = 345289467
print ("Number of digits : % d"%(countDigit(n)))
|
C#
using System;
class GFG {
static int countDigit(long n)
{
if (n == 0)
return 0;
return 1 + countDigit(n / 10);
}
public static void Main()
{
long n = 345289467;
Console.WriteLine("Number of "
+ "digits : " + countDigit(n));
}
}
|
PHP
<?php
function countDigit($n)
{
if ($n == 0)
return 0;
return 1 + countDigit((int)($n / 10));
}
$n = 345289467;
print ("Number of digits : " .
(countDigit($n)));
?>
|
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
#include <math.h>
#include <stdio.h>
int countDigit(long long n)
{
return floor(log10(n) + 1);
}
int main(void)
{
long long n = 345289467;
printf("Number of digits : %d",
countDigit(n));
return 0;
}
|
Java
import java.util.*;
class GFG {
static int countDigit(long n)
{
return (int)Math.floor(Math.log10(n) + 1);
}
public static void main(String[] args)
{
long n = 345289467;
System.out.print("Number of digits : " + countDigit(n));
}
}
|
Python3
import math
def countDigit(n):
return math.floor(math.log(n, 10)+1)
n = 345289467
print ("Number of digits : % d"%(countDigit(n)))
|
C#
using System;
class GFG {
static int countDigit(long n)
{
return (int)Math.Floor(Math.Log10(n) + 1);
}
public static void Main()
{
long n = 345289467;
Console.WriteLine("Number of digits : "
+ countDigit(n));
}
}
|
PHP
<?php
function countDigit($n)
{
return floor(log10($n)+1);
}
$n = 345289467;
echo "Number of digits : ",
countDigit($n);
?>
|
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.
def count_digits(n):
n = str(n)
return len(n)
n = 456533457776
print(count_digits(n))
|
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.
Recommended Posts:
- Count even and odd digits in an Integer
- Program to count vowels, consonant, digits and special characters in string.
- Count of numbers between range having only non-zero digits whose sum of digits is N and number is divisible by M
- Count numbers in given range such that sum of even digits is greater than sum of odd digits
- Print first k digits of 1/n where n is a positive integer
- Reverse digits of an integer with overflow handled
- Count consonants in a string (Iterative and recursive methods)
- Count of integers in a range which have even number of odd digits and odd number of even digits
- Count the number of holes in an integer
- Python String Methods | Set 2 (len, count, center, ljust, rjust, isalpha, isalnum, isspace & join)
- Count of m digit integers that are divisible by an integer n
- Java program to check palindrome (using library methods)
- Count Distinct Non-Negative Integer Pairs (x, y) that Satisfy the Inequality x*x + y*y < n
- Count digits in a factorial | Set 1
- Count digits in a factorial | Set 2