C++ Program to Count the Digits in an Integer

count digits

C++ programs to count the digits in an integer have been shown here. For example, if a number is 9876, the total number of digits is 4. The algorithm, pseudocode and time complexity of the programs have also been covered below.






1. Algorithm to count the digits in an integer


1. Take a number x as input.

2. Initialize a counter to 0 say c = 0.

3. Perform x = x / 10 and Increment c by 1 i.e. c = c + 1

4. If x = 0 stop the process and display c as output else go to step 3.




2. Pseudocode to count the digits in an integer


Input : A number $n$

Output : Total no of digits in $n$

1. Procedure countDigits($n$):

2. $c \leftarrow 0$

3. If $n == 0$:

4. $c \leftarrow 1$

5. Else:

6. Repeat until $n \neq 0$

7. $n \leftarrow n / 10$

8. $c \leftarrow c + 1$

9. Return $c$

10. End Procedure





3. Time complexity to count the digits in an integer


Time Complexity: O(log(n))

Where n is the input number.





4. Program & output to count the digits in an integer




4.1. C++ Program & output to count the digits in an integer using iteration

Code has been copied
/******************************************
            alphabetacoder.com
 C++ program to count the number of digits 
 in an integer using iterative approach
*******************************************/

#include <iostream>

using namespace std;

int main() {
    // declare variables
    int num, count = 0;

    // take input of the number
    cout << "Enter the integer = ";
    cin >> num;

    //count the digits
    if (num == 0)
        count = 1;
    else {
        while (num != 0) {
            num = num / 10;
            count++;
        }
    }

    // display result
    cout << "Number of digits = " << count << endl;

    return 0;
}

Output


Enter the integer = 9080706

Number of digits = 7





4.2. C++ Program & output to count the digits in an integer using recursion

Code has been copied
/******************************************
            alphabetacoder.com
 C++ program to count the number of digits 
 in an integer using recursive approach
*******************************************/

#include <iostream>

using namespace std;

// recursive function to calculate
// no of digits in an integer
int count_digit(int num) {
    if (num / 10 == 0)
        return 1;
    else
        // call function 
        return 1 + count_digit(num / 10);
}

int main() {
    // declare variables
    int num, count;

    // take input of the number
    cout << "Enter the integer = ";
    cin >> num;

    //count the digits by calling function
    count = count_digit(num);

    // display result
    cout << "Number of digits = " << count << endl;

    return 0;
}

Output


Enter the integer = 1111

Number of digits = 4





4.3. C++ Program & output to count the digits in an integer using logarithm

Code has been copied
/************************************
        alphabetacoder.com
 C++ program to count the number of
 digits in an integer using logarithm
**************************************/

#include <iostream>
#include <cmath>

using namespace std;

int main() {
    // declare variables
    int num;

    // take input of the number
    cout << "Enter the integer = ";
    cin >> num;

    if (num == 0) // if input number = 0
        cout << "Number of digits = 1" << endl;
    else {
        // calculate result using log10
        cout << "Number of digits = " << (int) floor(log10(num) + 1) << endl;
    }

    return 0;
}

Output


Enter the integer = 12345

Number of digits = 5