number of digits in a number python code example

Example 1: how to find how many digits a number has in c++

#include <iostream>
#include <cmath>

unsigned int getNumberOfDigits (int i)
{
    return i > 0 ? log10((double) i) + 1 : 1;
}

int main()
{
	std::cout << "Number of digits: " << getNumberOfDigits(/*Example*/6738) << std::endl;  
	return 0;
}

Example 2: number of digits in a number python

n = 1234 //Any Random Number
digits = len(str(n)) //Saves the number of digits of n into the variable digits

Example 3: how to find length of number in python

len(str(133))

Example 4: python count number of digits in integer

import math
digits = int(math.log10(n))+1

Tags:

Cpp Example