Algorithms_in_C++ 1.0.0
Set of algorithms implemented in C++.
Loading...
Searching...
No Matches
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 <cmath>
#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
 
double finding_number_of_digits_in_a_number_using_log (double n)
 This function finds the number of digits in constant time using logarithmic function TC: O(1)
 
static void first_test ()
 Self-test implementations.
 
static void second_test ()
 
int main ()
 Main function.
 

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). The code for that is also included (finding_number_of_digits_in_a_number_using_log). 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 for log calculation

The main function that checks the number of digits in a number. TC : O(number of digits)

Parameters
nthe number to check its digits
Returns
the digits count

< the variable used for the digits count

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

◆ finding_number_of_digits_in_a_number_using_log()

double finding_number_of_digits_in_a_number_using_log ( double n)

This function finds the number of digits in constant time using logarithmic function TC: O(1)

Parameters
nthe number to check its digits
Returns
the digits count
53 {
54 // log(0) is undefined
55 if (n == 0) {
56 return 0;
57 }
58
59 // to handle the negative numbers
60 if (n < 0) {
61 n = -n;
62 }
63
64 double count = floor(log10(n) + 1);
65
66 return count;
67}
T floor(T... args)
T log10(T... args)

◆ first_test()

static void first_test ( )
static

Self-test implementations.

Returns
void
73 {
74 assert(finding_number_of_digits_in_a_number(5492) == 4);
76 assert(finding_number_of_digits_in_a_number(10000) == 5);
78 assert(finding_number_of_digits_in_a_number(100000) == 6);
81}
uint64_t finding_number_of_digits_in_a_number(uint64_t n)
for IO operations
Definition finding_number_of_digits_in_a_number.cpp:30
Here is the call graph for this function:

◆ main()

int main ( void )

Main function.

Returns
0 on exit
96 {
97 // run self-test implementations
98 first_test();
99 second_test();
100 std::cout << "All tests have successfully passed!\n";
101 return 0;
102}
static void first_test()
Self-test implementations.
Definition finding_number_of_digits_in_a_number.cpp:73
Here is the call graph for this function:

◆ second_test()

static void second_test ( )
static
83 {
91}
double finding_number_of_digits_in_a_number_using_log(double n)
This function finds the number of digits in constant time using logarithmic function TC: O(1)
Definition finding_number_of_digits_in_a_number.cpp:53