{"id":26110,"date":"2017-10-26T19:51:03","date_gmt":"2017-10-26T14:21:03","guid":{"rendered":"https:\/\/www.wikitechy.com\/technology\/?p=26110"},"modified":"2017-10-26T19:51:03","modified_gmt":"2017-10-26T14:21:03","slug":"c-program-tower-hanoi","status":"publish","type":"post","link":"https:\/\/www.wikitechy.com\/technology\/c-program-tower-hanoi\/","title":{"rendered":"C Program for Tower of Hanoi"},"content":{"rendered":"<p>Tower of Hanoi is a mathematical puzzle where we have three rods and n disks. The objective of the puzzle is to move the entire stack to another rod, obeying the following simple rules:<\/p>\n<p><span id=\"more-142434\"><\/span><br \/>\n1) Only one disk can be moved at a time.<br \/>\n2) Each move consists of taking the upper disk from one of the stacks and placing it on top of another stack i.e. a disk can only be moved if it is the uppermost disk on a stack.<br \/>\n3) No disk may be placed on top of a smaller disk.<\/p>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">C Program<\/span> <\/div> <pre class=\"language-c code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-c code-embed-code\">#include &lt;stdio.h&gt;<br\/> <br\/>\/\/ C recursive function to solve tower of hanoi puzzle<br\/>void towerOfHanoi(int n, char fromrod, char torod, char auxrod)<br\/>{<br\/>    if (n == 1)<br\/>    {<br\/>        printf(&quot;\\n Move disk 1 from rod %c to rod %c&quot;, fromrod, torod);<br\/>        return;<br\/>    }<br\/>    towerOfHanoi(n-1, fromrod, auxrod, torod);<br\/>    printf(&quot;\\n Move disk %d from rod %c to rod %c&quot;, n, fromrod, torod);<br\/>    towerOfHanoi(n-1, auxrod, torod, fromrod);<br\/>}<br\/> <br\/>int main()<br\/>{<br\/>    int n = 4; \/\/ Number of disks<br\/>    towerOfHanoi(n, &#039;A&#039;, &#039;C&#039;, &#039;B&#039;);  \/\/ A, B and C are names of rods<br\/>    return 0;<br\/>}<\/code><\/pre> <\/div>\n<p>&nbsp;<\/p>\n<p><strong>Output:<\/strong><\/p>\n<pre> Move disk 1 from rod A to rod B\r\n Move disk 2 from rod A to rod C\r\n Move disk 1 from rod B to rod C\r\n Move disk 3 from rod A to rod B\r\n Move disk 1 from rod C to rod A\r\n Move disk 2 from rod C to rod B\r\n Move disk 1 from rod A to rod B\r\n Move disk 4 from rod A to rod C\r\n Move disk 1 from rod B to rod C\r\n Move disk 2 from rod B to rod A\r\n Move disk 1 from rod C to rod A\r\n Move disk 3 from rod B to rod C\r\n Move disk 1 from rod A to rod B\r\n Move disk 2 from rod A to rod C\r\n Move disk 1 from rod B to rod C\r\n<\/pre>\n<p>&nbsp;<\/p>\n[ad type=&#8221;banner&#8221;]\n","protected":false},"excerpt":{"rendered":"<p>C Program for Tower of Hanoi &#8211; Mathematical Algorithms &#8211; Tower of Hanoi is a mathematical puzzle where we have three rods and n disks. The objective is to.<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[69866,1,74058,83614],"tags":[76969,76941,76982,76981,76956,76945,76973,76989,76986,76955,76959,76962,76944,76961,76978,76948,76947,76977,76975,70056,76983,76943,76957,76968,76963,76958,76970,76971,76949,76980,76967,76984,76974,76972,76987,76985,76950,76965,76951,76976,76954,76942,76946,76952,76966,76979,76953,76988,76964,76960],"class_list":["post-26110","post","type-post","status-publish","format-standard","hentry","category-c-programming","category-coding","category-mathematical-algorithms","category-tower-of-hanoi","tag-algorithm-for-tower-of-hanoi-in-c","tag-c-program-for-tower-of-hanoi","tag-c-programs-on-recursion","tag-cs-tower","tag-disk-tower","tag-hanoi","tag-hanoi-code","tag-hanoi-problem","tag-hanoi-puzzle","tag-hanoi-tower","tag-hanoi-tower-game","tag-hanoi-tower-puzzle","tag-hanoi-wiki","tag-hanois-tower","tag-java-code-for-tower-of-hanoi","tag-lucas-tower","tag-program-of-recursion-in-c","tag-program-of-tower-of-hanoi-in-c","tag-puzzles-like-tower-of-hanoi","tag-recursion","tag-recursion-c-programming","tag-top-of-hanoi","tag-tour-of-hanoi","tag-tower-c","tag-tower-hanoi","tag-tower-hanoi-game","tag-tower-of-brahma","tag-tower-of-brahma-puzzle","tag-tower-of-hanoi","tag-tower-of-hanoi-4-discs-solution","tag-tower-of-hanoi-4-pegs","tag-tower-of-hanoi-algorithm-in-c","tag-tower-of-hanoi-algorithm-in-data-structure","tag-tower-of-hanoi-algorithm-recursive","tag-tower-of-hanoi-c","tag-tower-of-hanoi-code","tag-tower-of-hanoi-game","tag-tower-of-hanoi-game-java-source-code","tag-tower-of-hanoi-in-c","tag-tower-of-hanoi-java-code-using-stacks","tag-tower-of-hanoi-problem-in-artificial-intelligence","tag-tower-of-hanoi-program-in-c","tag-tower-of-hanoi-puzzle","tag-tower-of-hanoi-recursive-solution","tag-tower-of-hanoi-simulation","tag-tower-of-hanoi-solution","tag-tower-of-hanoi-solution-3-discs","tag-tower-of-hanoi-solution-5-discs","tag-tower-of-hanoi-solution-for-5-disks","tag-tower-puzzle"],"_links":{"self":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/26110","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/comments?post=26110"}],"version-history":[{"count":0,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/26110\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/media?parent=26110"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/categories?post=26110"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/tags?post=26110"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}