
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Determine Number of Digits 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 a shorter method and finally another approach to find the digit count.
For example, given a single integer (which can be positive, negative, or zero):
//Example 1 Input: 12345 Output: 5 //Example 2 Input: -789 Output: 3 Note: The minus sign (if present) is not counted as a digit.
Determining how many digits there are in an integer
We can count the number of digits in an integer using different methods in C++. Here are the three approaches we cover:
- Using Loop to Determine the Digit Count
- Using log10() to Find the Number of Digits
- Using String Conversion to Count Digits
Using Loop to Determine the Digit Count
In this approach, we use a loop to divide the number by 10 until it becomes 0. Each division removes one digit from the number, so the total number of divisions equals the number of digits.
Example
Here's a complete C++ program where we count how many times the number can be divided by 10 by increasing a counter each time.
#include <iostream> using namespace std; int main() { int num = 2301; int count = 0; // handling negative numbers by making them positive num = abs(num); if (num == 0) count = 1; else { while (num > 0) { num /= 10; count++; } } // printing the number of digits in the number cout << "Digits in 2301: " << count << endl; return 0; }
The output below shows the total number of digits in the given integer using a loop.
Digits in 2301: 4
Time Complexity: O(d), where d is the number of digits in the input number.
Space Complexity: O(1) used constant amount of space.
Using log10() to Find the Number of Digits
In this approach, we use the log10() function, which gives the power of 10 just below the number. Adding 1 to this power gives the total number of digits, because 10 raised to that power defines the digit range.
Example
In this example, we first take the absolute value of the number and then apply log10() to it and add 1. We handle zero separately since log10(0) is undefined.
#include <iostream> #include <cmath> using namespace std; int main() { int num = 501; // making the number positive if it is negative num = abs(num); // calculating the number of digits using logarithm int digits = (num == 0) ? 1 : (int)log10(num) + 1; cout << "Digits in 501: " << digits << endl; return 0; }
Below is the output of the program that shows the number of digits in the given integer using the log10() function.
Digits in 501: 3
Time Complexity: O(1) because log10() is a single step operation.
Space Complexity: O(1) because only a few variables are used.
Using String Conversion to Count Digits
In this approach we convert the number to a string using to_string() then count the characters in that string. If the number is negative we subtract one to ignore the minus sign.
Example
Below is a complete C++ program where we count the number of digits in the given integer using string conversion.
#include <iostream> #include <string> using namespace std; int main() { int num = -98; // converting the number to a string string str = to_string(num); // counting digits, ignoring the minus sign int digits = (num < 0) ? str.length() - 1 : str.length(); cout << "Digits in -98: " << digits << endl; return 0; }
The output below shows the total number of digits in the given integer by ignoring the negative sign using string conversion.
Digits in -98: 2
Time Complexity: O(n) because the string conversion checks each digit.
Space Complexity: O(n) because we store the number as a string.