Open In App

JavaScript Program to Count Digits of a Given Number

Last Updated : 17 Jan, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

Count Digits of a Given Number refers to determining the total number of individual digits present in a given numerical value. This involves analyzing each digit within the number and calculating how many digits are present in total.

Examples:

Input : 12345
Output : Total number of digits in 12345 is : 5

Input : 64674345
Output : Total number of digits in 12345 is : 8

There are several methods that can be used to Count Digits of a Given Number:

Approach 1: Using a Reduce Method

In this approach, we are using the reduce function. By converting the number to a string and splitting it into individual digits, the function accumulates the count using the reduce method,

Syntax:

function counting(num) {
return String(num).split('').reduce(
(count, digit) => count + 1, 0);
}

Example: Here, the counting function splits numbers into digits, then reduces the array to count digits.

Javascript




function counting(num) {
    return String(num).split('').reduce(
        (count, digit) => count + 1, 0);
}
 
let num1 = 12345;
let num2 = 987654321;
let result1 = counting(num1);
let result2 = counting(num2);
 
console.log("Number of digits in " + num1 + ": " + result1);
console.log("Number of digits in " + num2 + ": " + result2);


Output

Number of digits in 12345: 5
Number of digits in 987654321: 9

Approach 2: Using for Loop

In this approach we are using for loop, we traverse the digits of the number sequentially using a for loop. In each iteration, we divide the number by 10 (removing the last digit) and increment a counter. This process continues until the number becomes zero, allowing us to accurately tally the digits in the provided number.

Syntax:

function counting(number) {
let count = 0;
for (let n = number; n > 0; n = Math.floor(n / 10)) {
count++;
}
return count;
}

Example: Here, we are using the above-explained approach.

Javascript




function counting(number) {
    let count = 0;
    for (let n = number; n > 0; n = Math.floor(n / 10)) {
        count++;
    }
    return count;
}
 
let num = 12345;
let result = counting(num);
console.log(`Number of digits in ${num}: ${result}`);


Output

Number of digits in 12345: 5

Approach 3: Using String Conversion

In this approach we are using String Conversion” method converts the number to a string, splits it into characters, then calculates the character count.

Syntax:

function counting(num) {
return String(num).length;
}

Example: Here, we are using the above-explained approach.

Javascript




function counting(num) {
    return String(num).length;
}
 
let num1 = 12345;
let num2 = 9876543;
console.log("Number of digits in 12345 :", counting(num1));
console.log("Number of digits in 12345 :", counting(num2));


Output

Number of digits in 12345 : 5
Number of digits in 12345 : 7

Approach 4: Using Math Log10

In this approach we are using the Math Log10, calculate the logarithm base 10 of the number, and add 1, to determine the total digit count. Accurately calculates digits present.

Syntax:

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

Example: Here, the counting function uses Math Log10 to calculate and return the digit count of a given number.

Javascript




function counting(num) {
    return Math.floor(Math.log10(num) + 1);
}
 
const num1 = 12345;
console.log("Total Number of digits in 12345 is :", counting(num1));


Output

Total Number of digits in 12345 is : 5

Approach 5: Using Recursion

Recursion is a programming technique in which a function calls itself directly or indirectly.  Using a recursive algorithm, certain problems can be solved quite easily. Examples of such problems are Towers of Hanoi (TOH), Inorder/Preorder/Postorder Tree Traversals, DFS of Graph, etc. Recursion is a technique in which we reduce the length of code and make it easy to read and write. A recursive function solves a problem by calling its own function and also calling for the smaller subproblem.

Example: Here, we have used recursion to Count Digits of a Given Number

Javascript




function countDigitsWithRecursion(number) {
    if (Math.abs(number) < 1) {
        return 0;
    } else {
        return 1 + countDigitsWithRecursion(Math.floor(Math.abs(number) / 10));
    }
}
 
// Example usage:
let myNumber = 12345;
let digitsCount3 = countDigitsWithRecursion(myNumber);
console.log(`Digits Count (Recursion): ${digitsCount3}`);


Output

Digits Count (Recursion): 5


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads