TheAlgorithms/C++ 1.0.0
All the 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 <cstdint>
#include <iostream>
Include dependency graph for finding_number_of_digits_in_a_number.cpp:

Go to the source code of this file.

Functions

uint64_t finding_number_of_digits_in_a_number (uint64_t n)
 for log calculation
 
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.

Definition in file finding_number_of_digits_in_a_number.cpp.

Function Documentation

◆ finding_number_of_digits_in_a_number()

uint64_t finding_number_of_digits_in_a_number ( uint64_t n)

for log calculation

for assert for IO operations

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

Definition at line 31 of file finding_number_of_digits_in_a_number.cpp.

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

◆ 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

Definition at line 54 of file finding_number_of_digits_in_a_number.cpp.

54 {
55 // log(0) is undefined
56 if (n == 0) {
57 return 0;
58 }
59
60 // to handle the negative numbers
61 if (n < 0) {
62 n = -n;
63 }
64
65 double count = floor(log10(n) + 1);
66
67 return count;
68}

◆ first_test()

static void first_test ( )
static

Self-test implementations.

Returns
void

Definition at line 74 of file finding_number_of_digits_in_a_number.cpp.

74 {
75 assert(finding_number_of_digits_in_a_number(5492) == 4);
77 assert(finding_number_of_digits_in_a_number(10000) == 5);
79 assert(finding_number_of_digits_in_a_number(100000) == 6);
82}
uint64_t finding_number_of_digits_in_a_number(uint64_t n)
for log calculation

◆ main()

int main ( void )

Main function.

Returns
0 on exit

Definition at line 97 of file finding_number_of_digits_in_a_number.cpp.

97 {
98 // run self-test implementations
99 first_test();
100 second_test();
101 std::cout << "All tests have successfully passed!\n";
102 return 0;
103}
static void first_test()
Self-test implementations.

◆ second_test()

static void second_test ( )
static

Definition at line 84 of file finding_number_of_digits_in_a_number.cpp.

84 {
92}
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)