{"id":7862,"date":"2026-11-08T00:01:08","date_gmt":"2026-11-08T08:01:08","guid":{"rendered":"https:\/\/c-for-dummies.com\/blog\/?p=7862"},"modified":"2026-11-07T14:43:47","modified_gmt":"2026-11-07T22:43:47","slug":"counting-without-looping-solution","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=7862","title":{"rendered":"Counting Without Looping &#8211; Solution"},"content":{"rendered":"<p>The challenge for <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=7844\">this month&#8217;s Exercise<\/a> is to code a program that loops <em>n<\/em> times without using a looping keyword or statement. As you can probably guess, the solution involves recursion.<br \/>\n<!--more--><br \/>\nFor my approach, I created the recursive function <em>count()<\/em>. It has two arguments, <code>n<\/code> and <code>a<\/code>. Variable <code>n<\/code> is the number to count to with variable <code>a<\/code> keeping track of the iterations. The function handles the output and has no return value:<\/p>\n<h3><a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2026_11-Exercise.c\" rel=\"noopener\" target=\"_blank\">2026_11-Exercise.c<\/a><\/h3>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n\r\nvoid count(int n,int a)\r\n{\r\n    printf(\"%d\\n\",a);\r\n    if( a==n )\r\n        return;\r\n    else\r\n        count(n,a+1);\r\n}\r\n\r\nint main()\r\n{\r\n    int n;\r\n\r\n    <span class=\"comments\">\/* obtain input *\/<\/span>\r\n    printf(\"Loop count = \");\r\n    scanf(\"%d\",&amp;n);\r\n\r\n    <span class=\"comments\">\/* test for bad input *\/<\/span>\r\n    if( n&lt;1 )\r\n    {\r\n        puts(\"Enter a value larger than zero\");\r\n        return -1;\r\n    }\r\n\r\n    <span class=\"comments\">\/* start at zero *\/<\/span>\r\n    count(n,1);\r\n\r\n    return 0;\r\n}<\/pre>\n<p>The <em>main()<\/em> function handles input. Only positive integers are accepted. The value input is passed to the <em>count()<\/em> function with a second argument of one as the starting point. Here&#8217;s a sample run:<\/p>\n<pre>Loop count = 10\r\n1\r\n2\r\n3\r\n4\r\n5\r\n6\r\n7\r\n8\r\n9\r\n10<\/pre>\n<p>The recursion in the <em>count()<\/em> function works by having the function call itself as long as arguments <code>a<\/code> and <code>n<\/code> are unequal. Along the way, the value of variable <code>a<\/code> is output:<\/p>\n<p><code>printf(\"%d\\n\",a);<\/code><\/p>\n<p>With the values unequal, the recursive call is made by adding one to the value of <code>a<\/code>:<\/p>\n<p><code>count(n,a+1);<\/code><\/p>\n<p>Eventually, <code>n<\/code> and <code>a<\/code> will equal with each other, at which point the <em>return<\/em> statement is called and the recursion unwinds.<\/p>\n<p>I&#8217;ll confess that some of the more interesting solutions to this puzzle are possible only in C++. I am a total noob when it comes to C++, though here is a solution I found on the interwebs:<\/p>\n<h3><a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2026_11-1108.cpp\" rel=\"noopener\" target=\"_blank\">2026_11-1108.cpp<\/a><\/h3>\n<pre class=\"screen\">\r\n#include &lt;iostream&gt;\r\nusing namespace std;\r\n#define N 10\r\n\r\nint main()\r\n{\r\n\tstatic int i = 1;\r\n\tif( i &lt;= N && cout &lt;&lt; i++ &lt;&lt; \"\\n\" ) {\r\n\t\tmain();\r\n\t}\r\n\r\n\treturn 0;\r\n}<\/pre>\n<p>The output mirrors my solution, but without the prompt for input. This solution also uses recursion, but with the <em>main()<\/em> function &mdash; which I hear is scary. Regardless, it works.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The challenge for this month&#8217;s Exercise is to code a program that loops n times without using a looping keyword or statement. As you can probably guess, the solution involves recursion.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[],"class_list":["post-7862","post","type-post","status-publish","format-standard","hentry","category-solution"],"_links":{"self":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/7862","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=7862"}],"version-history":[{"count":5,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/7862\/revisions"}],"predecessor-version":[{"id":7894,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/7862\/revisions\/7894"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=7862"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=7862"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=7862"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}