Algorithms_in_C++ 1.0.0
Set of algorithms implemented in C++.
finding_number_of_digits_in_a_number.cpp File Reference

[Program to count digits in an integer](https://www.geeksforgeeks.org/program-count-digits-integer-3-different-methods) More...

#include <cassert>
#include <iostream>
Include dependency graph for finding_number_of_digits_in_a_number.cpp:

Functions

uint64_t finding_number_of_digits_in_a_number (uint64_t n)
 for IO operations More...
 
static void test ()
 Self-test implementations. More...
 
int main ()
 Main function. More...
 

Detailed Description

[Program to count digits in an integer](https://www.geeksforgeeks.org/program-count-digits-integer-3-different-methods)

Author
aminos 🇮🇳

It is a very basic math of finding number of digits in a given number i.e, we can use it by inputting values whether it can be a positive/negative value, let's say: an integer. There is also a second method: by using "K = floor(log10(N) + 1)", but it's only applicable for numbers (not integers). For more details, refer to the Algorithms-Explanation repository.

Function Documentation

◆ finding_number_of_digits_in_a_number()

uint64_t finding_number_of_digits_in_a_number ( uint64_t  n)

for IO operations

for assert

The main function that checks the number of digits in a number.

Parameters
nthe number to check its digits
Returns
the digits count

< the variable used for the digits count

27 {
28 uint64_t count = 0; ///< the variable used for the digits count
29
30 // iterate until `n` becomes 0
31 // remove last digit from `n` in each iteration
32 // increase `count` by 1 in each iteration
33 while (n != 0) {
34 // we can also use `n = n / 10`
35 n /= 10;
36 // each time the loop is running, `count` will be incremented by 1.
37 ++count;
38 }
39
40 return count;
41}

◆ main()

int main ( void  )

Main function.

Returns
0 on exit
63 {
64 test(); // run self-test implementations
65 return 0;
66}
static void test()
Self-test implementations.
Definition: finding_number_of_digits_in_a_number.cpp:47
Here is the call graph for this function:

◆ test()

static void test ( )
static

Self-test implementations.

Returns
void
47 {
48 assert(finding_number_of_digits_in_a_number(5492) == 4);
50 assert(finding_number_of_digits_in_a_number(10000) == 5);
52 assert(finding_number_of_digits_in_a_number(100000) == 6);
55
56 std::cout << "All tests have successfully passed!\n";
57}
uint64_t finding_number_of_digits_in_a_number(uint64_t n)
for IO operations
Definition: finding_number_of_digits_in_a_number.cpp:27
Here is the call graph for this function: