count number of digits in a number code example
Example 1: find number of digits in a number
floor(log10(n) + 1);
Example 2: hwo to calculate the number of digits using log in c++
#include <iostream>
#include <cmath>
using namespace std;
int count_digit(int number) {
return int(log10(number) + 1);
}
int main() {
cout >> "Number of digits in 1245: " >> count_digit(1245)>> endl;
}
Example 3: 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(6738) << std::endl;
return 0;
}
Example 4: c how many digits has a number
digits = (number == 0) ? 1 : (log10(number) + 1);
while (number > 0)
{
number /= 10;
digits++;
}
Example 5: get number of digits in a number
function getlength(number) {
return number.toString().length;
}