How to usea While Loop and Integer Division in Javascript

In this approach, we repeatedly divide the number by 10 until it becomes 0. We count the number of divisions performed which corresponds to the number of digits in the original number.

Example: Here, we have used while loop and integer division to Count Digits of a Given Number.

JavaScript
function countDigits(number) {
    let count = 0;
    while (number !== 0) {
        number = Math.floor(number / 10);
        count++;
    }
    return count;
}

// Example
const number = 123456;
console.log("Number of digits:", countDigits(number));

Output
Number of digits: 6


JavaScript Program to Count Digits of a Given Number

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:

Table of Content

  • Using a Reduce Method
  • Using for Loop
  • Using String Conversion
  • Using Math Log10
  • Using Recursion
  • Using a While Loop and Integer Division

Similar Reads

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,...

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....

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....

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....

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....

Approach 6: Using a While Loop and Integer Division

In this approach, we repeatedly divide the number by 10 until it becomes 0. We count the number of divisions performed which corresponds to the number of digits in the original number....

Contact Us