Write a C function count digits(int num); where num is a positive integer. The function counts how many times each of the digits 0..9 appears in num, and prints the results in 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: c how many digits has a number

digits = (number == 0) ? 1 : (log10(number) + 1);
//or
while (number > 0)
{
	number /= 10;
	digits++;
}
//see: https://ideone.com/P1h8Ne

Tags:

Cpp Example