mirror of
https://github.com/TheAlgorithms/C-Plus-Plus.git
synced 2026-05-08 06:43:14 +08:00
Update finding_number_of_Digits_in_a_Number.cpp
This commit is contained in:
@@ -1,15 +1,15 @@
|
||||
/**
|
||||
* @author [ANSHUMAAN](https://github.com/amino19)
|
||||
* @file [Program to count digits in an
|
||||
* integer](https://www.geeksforgeeks.org/program-count-digits-integer-3-different-methods)
|
||||
* @file
|
||||
*
|
||||
* @brief Finding number of Digits in a Number
|
||||
* @brief [Program to count digits
|
||||
* in an integer](https://www.geeksforgeeks.org/program-count-digits-integer-3-different-methods)
|
||||
* @details 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, lets say integer. There is also second method to do
|
||||
* it with, by using "K = floor(log10(N) + 1)", but its only applicable for
|
||||
* 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 Algorithms-Explanation
|
||||
* For more details, refer to the [Algorithms-Explanation](https://github.com/TheAlgorithms/Algorithms-Explanation/blob/master/en/Basic%20Math/Finding the number of digits in a number.md) repository.
|
||||
*/
|
||||
|
||||
#include <cassert> /// for assert
|
||||
@@ -20,7 +20,7 @@
|
||||
* @returns 0 on exit
|
||||
*/
|
||||
int main() {
|
||||
// Initialize 'n' & 'count' by 0
|
||||
// Initialize `n` & `count` by 0
|
||||
int n = 0;
|
||||
int count = 0;
|
||||
|
||||
@@ -30,18 +30,15 @@ int main() {
|
||||
std::cout << "Enter an integer: ";
|
||||
std::cin >> n;
|
||||
|
||||
/* iterate until n becomes 0
|
||||
* remove last digit from n in each iteration
|
||||
* increase count by 1 in each iteration */
|
||||
// iterate until `n` becomes 0
|
||||
// remove last digit from `n` in each iteration
|
||||
// increase `count` by 1 in each iteration
|
||||
while (n != 0) {
|
||||
// we can also use: n = n/10
|
||||
// we can also use `n = n / 10`
|
||||
n /= 10;
|
||||
/* each time while loop running, count will
|
||||
* be increment by 1.
|
||||
*/
|
||||
// each time the loop is running, `count` will be incremented by 1.
|
||||
++count;
|
||||
}
|
||||
|
||||
std::cout << "Number of digits: " << count;
|
||||
|
||||
return 0;
|
||||
|
||||
Reference in New Issue
Block a user