number of digits in a number c++ code example
Example 1: 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 2: 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 3: count digits c++
template <class T>
T countDigits(T number)
{
return T(log10(number) + 1);
}
Example 4: c++ get digits of integer
int iNums = 12345;
int iNumsSize = 5;
for (int i=iNumsSize-1; i>=0; i--) {
int y = pow(10, i);
int z = iNums/y;
int x2 = iNums / (y * 10);
printf("%d-",z - x2*10 );
}