Working with numbers is an integral part of programming. Every programming language provides support to manipulate numbers in many different ways. In this article, you'll learn how to find the total number of digits in an integer using iterative, log-based, and string-based approaches.

Problem Statement

You're given a number num. You need to count and print the total number of digits in num.

Example 1: Let num = 123456

Total number of digits in 123456 = 6

Thus, the output is 6.

Example 2: Let num = 325

Total number of digits in 325 = 3

Thus, the output is 3.

Iterative Approach to Count the Total Number of Digits in a Given Number

C++ Program to Count The Total Number of Digits in a Given Number

Below is the C++ program to count the total number of digits in a given number using iteration:

        // C++ program to count the total number of digits in an integer
#include <iostream>
using namespace std;

int countTotalDigits(int num)
{
    int result = 0;
    while (num != 0)
    {
        num = num / 10;
        ++result;
    }
    return result;
}

int main()
{
    int num1 = 123456;
    cout << "Total number of digits in " << num1 << ": " << countTotalDigits(num1) << endl;

    int num2 = 325;
    cout << "Total number of digits in " << num2 << ": " << countTotalDigits(num2) << endl;
    return 0;
}

Output:

        Total number of digits in 123456: 6
Total number of digits in 325: 3

Python Program to Count the Total Number of Digits in a Given Number

Below is the Python program to count the total number of digits in a given number using iteration:

        # Python program to count the total number of digits in an integer

def countTotalDigits(num):
    result = 0
    while num != 0:
        num //= 10
        result += 1
    return result


num1 = 123456
print("Total number of digits in", num1, ":", countTotalDigits(num1))

num2 = 325
print("Total number of digits in", num2, ":", countTotalDigits(num2))

Output:

        Total number of digits in 123456: 6
Total number of digits in 325: 3

JavaScript Program to Count The Total Number of Digits in a Given Number

Below is the JavaScript program to count the total number of digits in a given number using iteration:

        // JavaScript program to count the total number of digits in an integer

function countTotalDigits(num) {
    var result = 0;
    while (num != 0) {
        num = Math.floor(num / 10);
        ++result;
    }
    return result;
}


var num1 = 123456;
document.write("Total number of digits in " + num1 + ": " + countTotalDigits(num1) + "
");

var num2 = 325;
document.write("Total number of digits in " + num2 + ": " + countTotalDigits(num2) + "
");

Output:

        Total number of digits in 123456: 6
Total number of digits in 325: 3

Log-Based Approach to Count the Total Number of Digits in a Given Number

C++ Program to Count the Total Number of Digits in a Given Number

Below is the C++ program to count the total number of digits in a given number using a log-based approach:

        // C++ program to count total number of digits in an integer
#include <bits/stdc++.h>
using namespace std;

int countTotalDigits(int num)
{
    return floor(log10(num) + 1);
}

int main()
{
    int num1 = 123456;
    cout << "Total number of digits in " << num1 << ": " << countTotalDigits(num1) << endl;

    int num2 = 325;
    cout << "Total number of digits in " << num2 << ": " << countTotalDigits(num2) << endl;
    return 0;
}

Output:

        Total number of digits in 123456: 6
Total number of digits in 325: 3

Related: How to Find the Sum of All Elements in an Array

Python Program to Count the Total Number of Digits in a Given Number

Below is the Python program to count the total number of digits in a given number using a log-based approach:

        # Python program to count the total number of digits in an integer
import math

def countTotalDigits(num):
    return math.floor(math.log10(num)+1)


num1 = 123456
print("Total number of digits in", num1, ":", countTotalDigits(num1))

num2 = 325
print("Total number of digits in", num2, ":", countTotalDigits(num2))

Output:

        Total number of digits in 123456: 6
Total number of digits in 325: 3

JavaScript Program to Count the Total Number of Digits in a Given Number

Below is the JavaScript program to count the total number of digits in a given number using a log-based approach:

        // JavaScript program to count the total number of digits in an integer

function countTotalDigits(num) {
    return Math.floor(Math.log10(num) + 1);
}


var num1 = 123456;
document.write("Total number of digits in " + num1 + " : " + countTotalDigits(num1) + "
");

var num2 = 325;
document.write("Total number of digits in " + num2 + " : " + countTotalDigits(num2) + "
");

Output:

        Total number of digits in 123456: 6
Total number of digits in 325: 3

String-Based Approach to Count the Total Number of Digits in a Given Number

C++ Program to Count the Total Number of Digits in a Given Number

Below is the C++ program to count the total number of digits in a given number using a string-based approach:

        // C++ program to count the total number of digits in an integer
#include <bits/stdc++.h>
using namespace std;

int countTotalDigits(int num)
{
    string str = to_string(num);
    return str.size();
}

int main()
{
    int num1 = 123456;
    cout << "Total number of digits in " << num1 << ": " << countTotalDigits(num1) << endl;

    int num2 = 325;
    cout << "Total number of digits in " << num2 << ": " << countTotalDigits(num2) << endl;
  return 0;
}

Output:

        Total number of digits in 123456: 6
Total number of digits in 325: 3

Python Program to Count the Total Number of Digits in a Given Number

Below is the Python program to count the total number of digits in a given number using a string-based approach:

        # Python program to count the total number of digits in an integer

def countTotalDigits(num):
    myStr = str(num)
    return len(myStr)


num1 = 123456
print("Total number of digits in", num1, ":", countTotalDigits(num1))

num2 = 325
print("Total number of digits in", num2, ":", countTotalDigits(num2))

Output:

        Total number of digits in 123456: 6
Total number of digits in 325: 3

Related: How to Find the LCM and GCD of Two Numbers in Multiple Languages

JavaScript Program to Count the Total Number of Digits in a Given Number

Below is the JavaScript program to count the total number of digits in a given number using a string-based approach:

        // JavaScript program to count the total number of digits in an integer

function countTotalDigits(num) {
    let str = num.toString();
    return str.length;
}


var num1 = 123456;
document.write("Total number of digits in " + num1 + " : " + countTotalDigits(num1) + "
");

var num2 = 325;
document.write("Total number of digits in " + num2 + " : " + countTotalDigits(num2) + "
");

Output:

        Total number of digits in 123456: 6
Total number of digits in 325: 3

Related: How to Create a Digital Clock Using HTML, CSS, and JavaScript

Develop Projects to Solidify Your Concepts

If you're a beginner programmer, it would behoove you to develop some beginner-level projects to solidify your programming concepts. You can develop projects like to-do list apps, calculators, digital clocks, simple games, weight conversion tools, etc. Choose something that sparks your interest and get down to business; happy coding!