{"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[pastacode lang=\u201dc\u201d manual=\u201d%23include%20%3Cstdio.h%3E%0A%20%0A%2F%2F%20C%20recursive%20function%20to%20solve%20tower%20of%20hanoi%20puzzle%0Avoid%20towerOfHanoi(int%20n%2C%20char%20fromrod%2C%20char%20torod%2C%20char%20auxrod)%0A%7B%0A%20%20%20%20if%20(n%20%3D%3D%201)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20printf(%22%5Cn%20Move%20disk%201%20from%20rod%20%25c%20to%20rod%20%25c%22%2C%20fromrod%2C%20torod)%3B%0A%20%20%20%20%20%20%20%20return%3B%0A%20%20%20%20%7D%0A%20%20%20%20towerOfHanoi(n-1%2C%20fromrod%2C%20auxrod%2C%20torod)%3B%0A%20%20%20%20printf(%22%5Cn%20Move%20disk%20%25d%20from%20rod%20%25c%20to%20rod%20%25c%22%2C%20n%2C%20fromrod%2C%20torod)%3B%0A%20%20%20%20towerOfHanoi(n-1%2C%20auxrod%2C%20torod%2C%20fromrod)%3B%0A%7D%0A%20%0Aint%20main()%0A%7B%0A%20%20%20%20int%20n%20%3D%204%3B%20%2F%2F%20Number%20of%20disks%0A%20%20%20%20towerOfHanoi(n%2C%20\u2019A\u2019%2C%20\u2019C\u2019%2C%20\u2019B\u2019)%3B%20%20%2F%2F%20A%2C%20B%20and%20C%20are%20names%20of%20rods%0A%20%20%20%20return%200%3B%0A%7D\u201d message=\u201dC Program\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n<p>\u00a0<\/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>\u00a0<\/p>\n[ad type=\u201dbanner\u201d]\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}]}}