{"id":6239,"date":"2025-05-30T20:03:58","date_gmt":"2025-05-30T14:33:58","guid":{"rendered":"https:\/\/namastedev.com\/blog\/?p=6239"},"modified":"2025-05-30T20:03:58","modified_gmt":"2025-05-30T14:33:58","slug":"count-the-number-of-digits-in-an-integer","status":"publish","type":"post","link":"https:\/\/namastedev.com\/blog\/count-the-number-of-digits-in-an-integer\/","title":{"rendered":"Count the Number of Digits in an Integer"},"content":{"rendered":"\n<!-- Prism.js CSS and JS -->\n<link href=\"https:\/\/cdn.jsdelivr.net\/npm\/prismjs@1.29.0\/themes\/prism-tomorrow.css\" rel=\"stylesheet\" \/>\n<script src=\"https:\/\/cdn.jsdelivr.net\/npm\/prismjs@1.29.0\/prism.js\"><\/script>\n<script src=\"https:\/\/cdn.jsdelivr.net\/npm\/prismjs@1.29.0\/plugins\/autoloader\/prism-autoloader.min.js\"><\/script>\n\n<style>\n  .wp_blog_code-tabs-container {\n    font-family: \"Segoe UI\", sans-serif !important;\n    max-width: 900px !important;\n    margin: 2rem auto !important;\n    border: 1px solid #ddd !important;\n    border-radius: 8px !important;\n    overflow: hidden !important;\n    background-color: white !important;\n  }\n\n  .wp_blog_code-tabs-header {\n    background: #f7f7f7 !important;\n    display: flex !important;\n    border-bottom: 1px solid #ddd !important;\n  }\n\n  .wp_blog_code-tab-button {\n    flex: 1 !important;\n    padding: 10px 15px !important;\n    border: none !important;\n    background: transparent !important;\n    cursor: pointer !important;\n    font-weight: bold !important;\n    transition: background 0.2s !important;\n    color: #242B33 !important;\n  }\n\n  .wp_blog_code-tab-button.active {\n    background: white !important;\n    border-bottom: 3px solid #0073aa !important;\n  }\n\n  .wp_blog_code-tab-content {\n    display: none !important;\n    padding: 20px !important;\n    background: #242B33 !important;\n  }\n\n  .wp_blog_code-tab-content > pre {\n    background: #242B33 !important;\n  }\n\n  .wp_blog_code-tab-content.active {\n    display: block !important;\n  }\n\n  .wp_blog_code-tab-content pre {\n    margin: 0 !important;\n    overflow-x: auto !important;\n  }\n\n  .wp_blog_explanation {\n    max-width: 900px !important;\n    margin: 2rem auto !important;\n    font-family: \"Segoe UI\", sans-serif !important;\n    line-height: 1.6 !important;\n    background: white !important;\n    color: black !important;\n    padding: 1rem !important;\n    border-radius: 8px !important;\n  }\n\n  .wp_blog_explanation h2 {\n    color: #0073aa !important;\n    font-size: 1.5rem !important;\n    margin-bottom: 0.5rem !important;\n  }\n\n  .wp_blog_explanation code {\n    background: #f1f1f1 !important;\n    padding: 2px 6px !important;\n    border-radius: 4px !important;\n    font-family: monospace !important;\n  }\n<\/style>\n\n<!-- Explanation Block -->\n<div class=\"wp_blog_explanation\">\n  <p>Write a function <code>countDigits(n)<\/code> that takes an integer <code>n<\/code> and returns how many digits it contains.<\/p>\n\n  <h2>Requirements<\/h2>\n  <ul>\n    <li>Handles both positive and negative integers.<\/li>\n    <li>Return 1 if <code>n<\/code> is 0 (since 0 is a single-digit number).<\/li>\n  <\/ul>\n\n  <h2>Constraints<\/h2>\n  <ul>\n    <li>Time Complexity: <code>O(log\u2081\u2080(n))<\/code> \u2014 Each division reduces one digit.<\/li>\n    <li>Space Complexity: <code>O(1)<\/code> \u2014 Only a few variables are used.<\/li>\n  <\/ul>\n\n  <h2>Examples<\/h2>\n  <pre><code>Input: 259\nOutput: 3\n\nInput: -1035\nOutput: 4\n\nInput: 0\nOutput: 1<\/code><\/pre>\n\n  <h2>Step-by-Step Approach<\/h2>\n  <ul>\n    <li><strong>Handle Zero:<\/strong> If <code>n == 0<\/code>, return 1 directly.<\/li>\n    <li><strong>Convert to Positive:<\/strong> Use <code>abs(n)<\/code> to ignore sign.<\/li>\n    <li><strong>Initialize a counter:<\/strong> Set <code>count = 0<\/code>.<\/li>\n    <li><strong>Loop:<\/strong> While <code>n > 0<\/code>:\n      <ul>\n        <li>Divide <code>n<\/code> by 10 using integer division.<\/li>\n        <li>Increment <code>count<\/code>.<\/li>\n      <\/ul>\n    <\/li>\n    <li><strong>Return:<\/strong> The <code>count<\/code> after the loop finishes.<\/li>\n  <\/ul>\n<\/div>\n\n<!-- Code Tabs -->\n<div class=\"wp_blog_code-tabs-container\">\n  <div class=\"wp_blog_code-tabs-header\">\n    <button class=\"wp_blog_code-tab-button active\" data-lang=\"js\">JavaScript<\/button>\n    <button class=\"wp_blog_code-tab-button\" data-lang=\"c\">C<\/button>\n    <button class=\"wp_blog_code-tab-button\" data-lang=\"cpp\">C++<\/button>\n    <button class=\"wp_blog_code-tab-button\" data-lang=\"java\">Java<\/button>\n    <button class=\"wp_blog_code-tab-button\" data-lang=\"py\">Python<\/button>\n  <\/div>\n\n  <div class=\"wp_blog_code-tab-content active\" data-lang=\"js\">\n    <pre><code class=\"language-javascript\">\nfunction countDigits(n) {\n  if (n === 0) return 1;\n  n = Math.abs(n);\n  let count = 0;\n  while (n > 0) {\n    n = Math.floor(n \/ 10);\n    count++;\n  }\n  return count;\n}\n\nconsole.log(countDigits(259)); \/\/ 3\n    <\/code><\/pre>\n  <\/div>\n\n  <div class=\"wp_blog_code-tab-content\" data-lang=\"c\">\n    <pre><code class=\"language-c\">\n#include &lt;stdio.h&gt;\n#include &lt;stdlib.h&gt;\n\nint countDigits(int n) {\n  if (n == 0) return 1;\n  n = abs(n);\n  int count = 0;\n  while (n > 0) {\n    n \/= 10;\n    count++;\n  }\n  return count;\n}\n\nint main() {\n  int num = 259;\n  int result = countDigits(num);\n  printf(\"%d\\n\", result); \/\/ Output: 3\n  return 0;\n}\n    <\/code><\/pre>\n  <\/div>\n\n  <div class=\"wp_blog_code-tab-content\" data-lang=\"cpp\">\n    <pre><code class=\"language-cpp\">\n#include &lt;iostream&gt;\n#include &lt;cstdlib&gt;\nusing namespace std;\n\nint countDigits(int n) {\n  if (n == 0) return 1;\n  n = abs(n);\n  int count = 0;\n  while (n > 0) {\n    n \/= 10;\n    count++;\n  }\n  return count;\n}\n\nint main() {\n  int num = 259;\n  int result = countDigits(num);\n  cout << result << endl; \/\/ Output: 3\n  return 0;\n}\n    <\/code><\/pre>\n  <\/div>\n\n  <div class=\"wp_blog_code-tab-content\" data-lang=\"java\">\n    <pre><code class=\"language-java\">\npublic class Main {\n  public static int countDigits(int n) {\n    if (n == 0) return 1;\n    n = Math.abs(n);\n    int count = 0;\n    while (n > 0) {\n      n \/= 10;\n      count++;\n    }\n    return count;\n  }\n\n  public static void main(String[] args) {\n    int num = 259;\n    int result = countDigits(num);\n    System.out.println(result); \/\/ Output: 3\n  }\n}\n    <\/code><\/pre>\n  <\/div>\n\n  <div class=\"wp_blog_code-tab-content\" data-lang=\"py\">\n    <pre><code class=\"language-python\">\ndef count_digits(n):\n    if n == 0:\n        return 1\n    n = abs(n)\n    count = 0\n    while n > 0:\n        n \/\/= 10\n        count += 1\n    return count\n\nnum = 259\nresult = count_digits(num)\nprint(result)  # Output: 3\n    <\/code><\/pre>\n  <\/div>\n<\/div>\n\n<!-- Tab Switch Script -->\n<script>\n  document.addEventListener('DOMContentLoaded', function () {\n    const buttons = document.querySelectorAll('.wp_blog_code-tab-button');\n    const contents = document.querySelectorAll('.wp_blog_code-tab-content');\n\n    buttons.forEach(button => {\n      button.addEventListener('click', () => {\n        const lang = button.getAttribute('data-lang');\n\n        buttons.forEach(btn => btn.classList.remove('active'));\n        button.classList.add('active');\n\n        contents.forEach(content => {\n          content.classList.toggle('active', content.getAttribute('data-lang') === lang);\n        });\n      });\n    });\n  });\n<\/script>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Write a function countDigits(n) that takes an integer n and returns how many digits it contains. Requirements Handles both positive and negative integers. Return 1 if n is 0 (since 0 is a single-digit number). Constraints Time Complexity: O(log\u2081\u2080(n)) \u2014 Each division reduces one digit. Space Complexity: O(1) \u2014 Only a few variables are used.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[260,811,810,174,172,173],"tags":[],"class_list":{"0":"post-6239","1":"post","2":"type-post","3":"status-publish","4":"format-standard","6":"category-c-c-plus-plus","7":"category-data-structures-and-algorithms","8":"category-dsa","9":"category-java","10":"category-javascript","11":"category-python"},"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/posts\/6239","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/comments?post=6239"}],"version-history":[{"count":3,"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/posts\/6239\/revisions"}],"predecessor-version":[{"id":6252,"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/posts\/6239\/revisions\/6252"}],"wp:attachment":[{"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/media?parent=6239"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/categories?post=6239"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/tags?post=6239"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}