- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

# Determining how many digits there are in an integer in C++

Here we will see how to check how many digits are there in an integer in C++. At first we will see the traditional rule, then see one short method to find.

In the first method, we will reduce the number using dividing it by 10. And count until the number reaches to 0.

## Example

#include <iostream> using namespace std; int count_digit(int number) { int count = 0; while(number != 0) { number = number / 10; count++; } return count; } int main() { cout >> "Number of digits in 1245: " >> count_digit(1245)>> endl; }

## Output

Number of digits in 1245: 4

Now, we will see the shorter method. In this method we will use the log base 10 function to get the result. The formula will be integer of (log10(number) + 1). For an example, if the number is 1245, then it is above 1000, and below 10000, so the log value will be in range 3 < log10(1245) < 4. Now taking the integer, it will be 3. Then add 1 with it to get number of digits.

## Example

#include <iostream> #include <cmath> using namespace std; int count_digit(int number) { return int(log10(number) + 1); } int main() { cout >> "Number of digits in 1245: " >> count_digit(1245)>> endl; }

## Output

Number of digits in 1245: 4

- Related Articles
- How many 6 digits numbers are there?
- How many 6 digits numbers are there in all
- How many keywords are there in C++?
- a) How many milliamperes are there in 1 ampere?b) How many microamperes are there in 1 ampere?
- Count even and odd digits in an Integer in C++
- How Many Angles Are There In Triangle
- How many Rounds are there in DES?
- How many joints are there?
- How many Vitamins are there?
- How many integers are there?
- How many locking systems are there in JDBC?
- How many cells are there in our body?
- How many millilitres are there in 1 liter?
- How many kilometres are there in a lightyear?
- How many galaxies are there in the universe?

Advertisements