mirror of
https://hub.njuu.cf/TheAlgorithms/C-Plus-Plus.git
synced 2023-10-11 13:05:55 +08:00
Finding no. of digits in a Number
This commit is contained in:
parent
a9d33871a9
commit
6f2e30014a
36
math/finding_number_of_Digits_in_a_Number.cpp
Normal file
36
math/finding_number_of_Digits_in_a_Number.cpp
Normal file
@ -0,0 +1,36 @@
|
||||
/**
|
||||
* @author amino19
|
||||
* @file
|
||||
*
|
||||
* \brief Finding number of Digits in a Number
|
||||
* \details Basically, its a very basic math of
|
||||
* finding number of digits in a number (input),
|
||||
* For more details, refer Algorithms-Explanation
|
||||
*/
|
||||
|
||||
|
||||
/* Log based C++ program to count number of
|
||||
* digits in a number/integer */
|
||||
|
||||
#include <cassert>
|
||||
#include <iostream>
|
||||
|
||||
int main()
|
||||
{
|
||||
long long n;
|
||||
int count = 0;
|
||||
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 */
|
||||
|
||||
while (n != 0)
|
||||
{
|
||||
n /= 10; // n = n/10
|
||||
++count;
|
||||
}
|
||||
|
||||
std::cout << "Number of digits: " << count;
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user