### Select Articles

Count Number of Digits in an Integer

Program to count number of digits in an integer is discussed here. An integer is given as input and the total number of digits in that integer is displayed as output. Finding the number of digits in an integer can be done using loops, recursion, and a log-based solution can be also used.

Program to count number of digits in an integer using a loop

C

#include

int main()

{

int n;

int count = 0;

printf(“\nEnter the number: “);

scanf(“%d”, &n);

while(n != 0)

{

n = n/10;

++count;

}

printf(“\nNumber of digits: %d\n”, count);

}

C++

#include

using namespace std;

int main()

{

int n;

int count = 0;

cout << “\nEnter the number: “;

cin >> n;

while(n != 0)

{

n = n/10;

++count;

}

cout << “\nNumber of digits: ” << count>

}

JAVA

import java.util.*;

public class Main

{

public static void main(String args[])

{

int n,count = 0;

System.out.print(“\nEnter the number : “);

Scanner sc = new Scanner(System.in);

n = sc.nextInt();

while(n != 0)

{

n = n/10;

count++;

}

System.out.println(“Number of digits : ” + count);

}

}

PYTHON 3

n = int(input(“Enter a number : “))

count = 0

while(n != 0):

n = int(n/10)

count = count + 1

print(“Number of digits :”,count)

Output

Input- Enter the number:1 2 3 4 5 6 Output- Number of digits:6

Program to count number of digits in an integer using recursion

C

#include

int number_of_digits(int n)

{

if(n == 0)

return 0;

while(n != 0)

{

return 1 + number_of_digits(n/10);

}

}

int main()

{

int n;

printf(“\nEnter the number: “);

scanf(“%d”, &n);

printf(“\nNumber of digits : “);

printf(“%d\n”, number_of_digits(n));

return 0;

}

C++

#include

using namespace std;

int number_of_digits(int n)

{

if(n == 0)

return 0;

while(n != 0)

{

return 1 + number_of_digits(n/10);

}

}

int main()

{

int n;

int count = 0;

cout << “\nEnter the number: “;

cin >> n;

cout << “\nNumber of digits: ” << number>

cout << endl>

}

JAVA

import java.util.*;

public class Main

{

static int count = 0;

static int number_of_digits(int n)

{

if(n == 0)

return 0;

return 1 + number_of_digits(n/10);

}

public static void main(String args[])

{

int n;

System.out.print(“\nEnter the number : “);

Scanner sc = new Scanner(System.in);

n = sc.nextInt();

System.out.println(“Number of digits : ” + number_of_digits(n));

}

}

PYTHON 3

def number_of_digits(n):

if(n == 0):

return 0;

return 1 + number_of_digits(int(n/10))

n = int(input(“Enter a number : “))

count = 0

print(“Number of digits :”,number_of_digits(n))

Output

Input- Enter the number:1 2 3 4 5 6 Output- Number of digits:6

Program to count number of digits in an integer using log based approach

C

#include

#include <math.h>

int number_of_digits(int n)

{

return floor(log10(n)+1);

}

int main()

{

int n;

printf(“\nEnter the number: “);

scanf(“%d”, &n);

printf(“\nNumber of digits : “);

printf(“%d\n”, number_of_digits(n));

return 0;

}

C++

#include

#include <math.h>

using namespace std;

int number_of_digits(int n)

{

return floor(log10(n)+1);

}

int main()

{

int n;

int count = 0;

cout << “\nEnter the number: “;

cin >> n;

cout << “\nNumber of digits: ” << number>

cout << endl>

}

JAVA

import java.util.*;

public class Main

{

static int number_of_digits(int n)

{

return (int)Math.floor(Math.log10(n)+1);

}

public static void main(String args[])

{

int n;

System.out.print(“\nEnter the number : “);

Scanner sc = new Scanner(System.in);

n = sc.nextInt();

System.out.println(“Number of digits : ” + number_of_digits(n));

}

}

PYTHON 3

import math

def number_of_digits(n):

return math.floor(math.log(n, 10)+1)

n = int(input(“Enter a number : “))

count = 0

print(“Number of digits :”,number_of_digits(n))

Output

Input- Enter the number:1 2 3 4 5 6 Output- Number of digits:6