๐Ÿš€ Day 18 – DSA Problem Solving

๐Ÿ“Œ Q1 :- Count Digits in a Number

Write a function that returns the number of digits in an integer n.
Example: For input -12345, the output should be 5.

๐Ÿ’ก Approach:

To count digits in a number:

Check if the number is 0 โ†’ return …


This content originally appeared on DEV Community and was authored by King coder

๐Ÿ“Œ Q1 :- Count Digits in a Number

Write a function that returns the number of digits in an integer n.

Example: For input -12345, the output should be 5.

๐Ÿ’ก Approach:

To count digits in a number:

  1. Check if the number is 0 โ†’ return 1 (since 0 has one digit).
  2. Convert the number to a positive value using Math.abs(n) to handle negative inputs.
  3. Initialize a counter (count = 0).
  4. Use a loop:
    • While n > 0:
      • Divide n by 10 using Math.floor(n / 10).
      • Increment count.
  5. Return count.

๐Ÿงช Corner Cases:

๐Ÿ› ๏ธ Always write ๐Ÿงช Corner Cases: when solving any DSA problem.

It helps anticipate edge cases and ensures your solution handles all scenarios robustly.

Case Description
n = 0 Return 1 (since 0 has 1 digit)
Negative numbers (n = -123) Use Math.abs() to count digits correctly
Single-digit numbers Should return 1
Very large number Loop should handle large inputs efficiently
Non-integer inputs Not handled here, but worth validating in real-world cases

๐Ÿ“ฅ Input:

n = -12345

๐Ÿ“ค Output:

n = 5

JavaScript Solution:


/**
 * Counts the number of digits in a number
 * @param {number} n - The input integer
 * @returns {number} - Number of digits
 */
function countDigits(n) {
    if (n === 0) return 1;

    n = Math.abs(n); // Handle negative numbers
    let count = 0;

    while (n > 0) {
        n = Math.floor(n / 10);
        count++;
    }

    return count;
}

// Test Case
console.log("Digit count for -12345:", countDigits(-12345)); // Output: 5



This content originally appeared on DEV Community and was authored by King coder


Print Share Comment Cite Upload Translate Updates
APA

King coder | Sciencx (2025-07-24T04:43:43+00:00) ๐Ÿš€ Day 18 – DSA Problem Solving. Retrieved from https://www.scien.cx/2025/07/24/%f0%9f%9a%80-day-18-dsa-problem-solving/

MLA
" » ๐Ÿš€ Day 18 – DSA Problem Solving." King coder | Sciencx - Thursday July 24, 2025, https://www.scien.cx/2025/07/24/%f0%9f%9a%80-day-18-dsa-problem-solving/
HARVARD
King coder | Sciencx Thursday July 24, 2025 » ๐Ÿš€ Day 18 – DSA Problem Solving., viewed ,<https://www.scien.cx/2025/07/24/%f0%9f%9a%80-day-18-dsa-problem-solving/>
VANCOUVER
King coder | Sciencx - » ๐Ÿš€ Day 18 – DSA Problem Solving. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2025/07/24/%f0%9f%9a%80-day-18-dsa-problem-solving/
CHICAGO
" » ๐Ÿš€ Day 18 – DSA Problem Solving." King coder | Sciencx - Accessed . https://www.scien.cx/2025/07/24/%f0%9f%9a%80-day-18-dsa-problem-solving/
IEEE
" » ๐Ÿš€ Day 18 – DSA Problem Solving." King coder | Sciencx [Online]. Available: https://www.scien.cx/2025/07/24/%f0%9f%9a%80-day-18-dsa-problem-solving/. [Accessed: ]
rf:citation
» ๐Ÿš€ Day 18 – DSA Problem Solving | King coder | Sciencx | https://www.scien.cx/2025/07/24/%f0%9f%9a%80-day-18-dsa-problem-solving/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.