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

#include <stdio.h>
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);
}

#include <iostream>
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 << endl;
}

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);
}
}

n = int(input(“Enter a number : “))
count = 0
while(n != 0):
n = int(n/10)
count = count + 1
print(“Number of digits :”,count)

Program to count number of digits in an integer using recursion

#include <stdio.h>
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;
}

#include <iostream>
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_of_digits(n);
cout << endl;
}

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));
}
}

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))

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

#include <stdio.h>
#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;
}

#include <iostream>
#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_of_digits(n);
cout << endl;
}

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));
}
}

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))

count number of digits in an integer