{"id":5485,"date":"2022-08-20T00:01:24","date_gmt":"2022-08-20T07:01:24","guid":{"rendered":"https:\/\/c-for-dummies.com\/blog\/?p=5485"},"modified":"2022-08-13T10:47:08","modified_gmt":"2022-08-13T17:47:08","slug":"counting-the-digits","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=5485","title":{"rendered":"Counting the Digits"},"content":{"rendered":"<p>I am not a math genius, as any math genius who reviews my work is happy to tell me. Even so, when programming I&#8217;m often faced with math challenges where a math education would benefit. My most recent puzzle is how to count the number of digits in a value without first converting the value into a string.<br \/>\n<!--more--><br \/>\nIf you visit this blog regularly, yes, this Lesson hints at an upcoming Exercise challenge. Still, the problem intrigued me: Does some mathematical trick exist that determines how a value such as 12345 contains five digits? Rather than research such a solution, I just started to code. Typical.<\/p>\n<p>Given the Power of the Computer, I devised a brute-force method to determine the number of digits in a value by subtracting successive powers of 10 from a given number. To ensure that large values are properly processed, I use <em>long<\/em> integer types in the following code:<\/p>\n<h3><a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2022_08_20-Lesson-a.c\" rel=\"noopener\" target=\"_blank\">2022_08_20-Lesson-a.c<\/a><\/h3>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n\r\nint main()\r\n{\r\n    long d;\r\n    int x,l;\r\n    long value[10] = {\r\n        1, 12, 123, 1234, 12345,\r\n        123456, 1234567, 12345678,\r\n        123456789, 1234567890\r\n    };\r\n\r\n    for( x=0; x&lt;10; x++ )\r\n    {\r\n        l=d=1;\r\n        while( d&lt;=10000000000 )\r\n        {\r\n            if( value[x]&lt;d )\r\n            {\r\n                printf(\"%ld is %d digits long\\n\",value[x],l-1);\r\n                break;\r\n            }\r\n            d*=10;\r\n            l++;\r\n        }\r\n    }\r\n\r\n    return(0);\r\n}<\/pre>\n<p>The <code>value[]<\/code> array holds ten numbers progressing from 1 through 1234567890. A <em>for<\/em> loop churns through each of the values.<\/p>\n<p>Within the <em>for<\/em> loop, a <em>while<\/em> loop compares <code>value[x]<\/code> with successive powers of 10. The looping value <code>d<\/code> starts at one, and multiplies itself by 10 each iteration until its value equals 10,000,000,000.<\/p>\n<p>Within the <em>while<\/em> loop, an <em>if<\/em> statement compares the value submitted, <code>value[x]<\/code>, with <code>d<\/code>, the power of 10. When the value is less, then digit counter variable <code>l<\/code> (little L) determines the number of digits. Here is the output:<\/p>\n<p><code>1 is 1 digits long<br \/>\n12 is 2 digits long<br \/>\n123 is 3 digits long<br \/>\n1234 is 4 digits long<br \/>\n12345 is 5 digits long<br \/>\n123456 is 6 digits long<br \/>\n1234567 is 7 digits long<br \/>\n12345678 is 8 digits long<br \/>\n123456789 is 9 digits long<br \/>\n1234567890 is 10 digits long<\/code><\/p>\n<p>This test works fine, but what I truly adore is generating random numbers to try the code. The problem here is that random values are typically and uniformly huge, so they must be pared down to a smaller length. Such an exercise requires more math magic, at which I confess to being a poor conjurer. Still, I took a stab at it in this update to the code:<\/p>\n<h3><a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2022_08_20-Lesson-b.c\" rel=\"noopener\" target=\"_blank\">2022_08_20-Lesson-b.c<\/a><\/h3>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n#include &lt;stdlib.h&gt;\r\n#include &lt;time.h&gt;\r\n#include &lt;math.h&gt;\r\n\r\nint main()\r\n{\r\n    long d;\r\n    int l,r;\r\n    long value;\r\n\r\n    <span class=\"comments\">\/* seed the randomizer *\/<\/span>\r\n    srand( (unsigned)time(NULL) );\r\n\r\n    <span class=\"comments\">\/* obtain the random value *\/<\/span>\r\n    value = rand();\r\n\r\n    <span class=\"comments\">\/* obtain a random value length *\/<\/span>\r\n    r = rand() % 11;\r\n\r\n    <span class=\"comments\">\/* truncate the value *\/<\/span>\r\n    value %= (int)pow(10,r);\r\n\r\n    <span class=\"comments\">\/*\r\n        now count the digits, which will end\r\n        up being the same value as 'r', but\r\n        what-the-hey\r\n    *\/<\/span>\r\n    l=d=1;\r\n    while( d&lt;=10000000000 )\r\n    {\r\n        if( value&lt;d )\r\n        {\r\n            printf(\"%ld is %d digits long\\n\",value,l-1);\r\n            break;\r\n        }\r\n        d*=10;\r\n        l++;\r\n    }\r\n\r\n    return(0);\r\n}<\/pre>\n<p>Two random values are required to generate a random number of a random length. The first is the value itself, <code>value<\/code> in the above code. The second is the truncating value, <code>r<\/code> above.<\/p>\n<p>At Line 22, <code>value<\/code> is truncated by taking <code>r<\/code> to the power of 10, typecasting it as an integer, and using the mod assignment operator, <code>%=<\/code>. This expression reduces the random <code>value<\/code> down to a digit range equal to the value of <code>r<\/code> &mdash; which is what the remainder of the code attempts to discover. Still, the point is to try the technique on a random value, and it works:<\/p>\n<p><code>36398 is 5 digits long<br \/>\n9070012 is 7 digits long<br \/>\n99086819 is 8 digits long<br \/>\n599137240 is 9 digits long<br \/>\n4 is 1 digits long<\/code><\/p>\n<p>I would bet a whole donut that a better way exists to count the digits in a number. If not a better way, perhaps even a different way that makes more sense. If you know of one, please let me know. I&#8217;m not desperate, just curious.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Does a way exist to count the number of digits in a value without converting the value into a string? <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=5485\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[],"class_list":["post-5485","post","type-post","status-publish","format-standard","hentry","category-main"],"_links":{"self":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/5485","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=5485"}],"version-history":[{"count":4,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/5485\/revisions"}],"predecessor-version":[{"id":5495,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/5485\/revisions\/5495"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=5485"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=5485"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=5485"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}