Skip to content

Commit 341ed50

Browse files
aminoxixPanquesito7github-actions
authored
feat: Finding no. of digits in a Number (#1497)
* Finding no. of digits in a Number * Initialize n * Initialize n as int * Changes done * Changes done with codes by adding more comments * Changes done with codes by adding name as md * Modified comments * add void * remove void & update comments * Set some changes to pass Awesome CI Workflow * add return 0 & file name in lower case * Changes done.. * Update finding_number_of_Digits_in_a_Number.cpp * Update finding_number_of_Digits_in_a_Number.cpp * Update finding_number_of_Digits_in_a_Number.cpp * formatting filenames 0ec45e3 * updating DIRECTORY.md * clang-format and clang-tidy fixes for 0ec45e3 * clang-format and clang-tidy fixes for 9c0a437 * updating DIRECTORY.md * Wrote test, needs review * [fix/docs]: Fix tests/code and add documentation Co-authored-by: David Leal <[email protected]> Co-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com>
1 parent 289ebc5 commit 341ed50

File tree

4 files changed

+75
-4
lines changed

4 files changed

+75
-4
lines changed

DIRECTORY.md

+1
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,7 @@
170170
* [Fibonacci Large](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/math/fibonacci_large.cpp)
171171
* [Fibonacci Matrix Exponentiation](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/math/fibonacci_matrix_exponentiation.cpp)
172172
* [Fibonacci Sum](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/math/fibonacci_sum.cpp)
173+
* [Finding Number Of Digits In A Number](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/math/finding_number_of_digits_in_a_number.cpp)
173174
* [Gcd Iterative Euclidean](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/math/gcd_iterative_euclidean.cpp)
174175
* [Gcd Of N Numbers](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/math/gcd_of_n_numbers.cpp)
175176
* [Gcd Recursive Euclidean](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/math/gcd_recursive_euclidean.cpp)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/**
2+
* @author [aminos 🇮🇳](https://github.com/amino19)
3+
* @file
4+
*
5+
* @brief [Program to count digits
6+
* in an
7+
* integer](https://www.geeksforgeeks.org/program-count-digits-integer-3-different-methods)
8+
* @details It is a very basic math of finding number of digits in a given
9+
* number i.e, we can use it by inputting values whether it can be a
10+
* positive/negative value, let's say: an integer. There is also a second
11+
* method: by using "K = floor(log10(N) + 1)", but it's only applicable for
12+
* numbers (not integers).
13+
* For more details, refer to the
14+
* [Algorithms-Explanation](https://github.com/TheAlgorithms/Algorithms-Explanation/blob/master/en/Basic%20Math/Finding
15+
* the number of digits in a number.md) repository.
16+
*/
17+
18+
#include <cassert> /// for assert
19+
#include <iostream> /// for IO operations
20+
21+
/**
22+
* @brief The main function that checks
23+
* the number of digits in a number.
24+
* @param n the number to check its digits
25+
* @returns the digits count
26+
*/
27+
uint64_t finding_number_of_digits_in_a_number(uint64_t n) {
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+
}
42+
43+
/**
44+
* @brief Self-test implementations
45+
* @returns void
46+
*/
47+
static void test() {
48+
assert(finding_number_of_digits_in_a_number(5492) == 4);
49+
assert(finding_number_of_digits_in_a_number(-0) == 0);
50+
assert(finding_number_of_digits_in_a_number(10000) == 5);
51+
assert(finding_number_of_digits_in_a_number(9) == 1);
52+
assert(finding_number_of_digits_in_a_number(100000) == 6);
53+
assert(finding_number_of_digits_in_a_number(13) == 2);
54+
assert(finding_number_of_digits_in_a_number(564) == 3);
55+
56+
std::cout << "All tests have successfully passed!\n";
57+
}
58+
59+
/**
60+
* @brief Main function
61+
* @returns 0 on exit
62+
*/
63+
int main() {
64+
test(); // run self-test implementations
65+
return 0;
66+
}

search/floyd_cycle_detection_algo.cpp

+4-2
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,14 @@ namespace cycle_detection {
3535
*/
3636
template <typename T>
3737
int32_t duplicateNumber(const std::vector<T> &in_arr, const uint32_t &n) {
38-
if (n == 0 || n == 1) { // to find duplicate in an array its size should be atleast 2
38+
if (n == 0 ||
39+
n == 1) { // to find duplicate in an array its size should be atleast 2
3940
return -1;
4041
}
4142
uint32_t tortoise = in_arr[0]; // variable tortoise is used for the longer
4243
// jumps in the array
43-
uint32_t hare = in_arr[0]; // variable hare is used for shorter jumps in the array
44+
uint32_t hare =
45+
in_arr[0]; // variable hare is used for shorter jumps in the array
4446
do {
4547
tortoise = in_arr[tortoise];
4648
hare = in_arr[in_arr[hare]];

sorting/dnf_sort.cpp

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
/**
22
* @file
33
* @brief Implementation of the [DNF
4-
* sort](https://www.geeksforgeeks.org/sort-an-array-of-0s-1s-and-2s/) implementation
4+
* sort](https://www.geeksforgeeks.org/sort-an-array-of-0s-1s-and-2s/)
5+
* implementation
56
* @details
67
* C++ program to sort an array with 0, 1 and 2 in a single pass(DNF sort).
78
* Since one traversal of the array is there hence it works in O(n) time
@@ -22,7 +23,8 @@ namespace sorting {
2223
/**
2324
* @namespace dnf_sort
2425
* @brief Functions for the [DNF
25-
* sort](https://en.wikipedia.org/wiki/Dutch_national_flag_problem) implementation
26+
* sort](https://en.wikipedia.org/wiki/Dutch_national_flag_problem)
27+
* implementation
2628
*/
2729
namespace dnf_sort {
2830
/**

0 commit comments

Comments
 (0)