Hanoi towers function10/26/2022 Printf("The sequence of moves involved in the Tower of Hanoi are :\n") Int n // defined to store the number disc It is almost similar to the above one, except that in this code, you can input the number of discs. #Hanoi towers function codeSample OutputĪlternative C source code for Tower of Hanoi puzzle is given below. Copy the source code in Code::Blocks and run it. There is nothing to be provided as input in this program. In this C program for Tower of Hanoi, the objective of defining n is to store numbers of discs, and the other character variables fr, tr and ar stand for from rod, to rod and auxiliary rod, respectively. “ n” is of integer data type and the other three variables are of character data type. In the program source code, hanoifun () is the recursive function with four arguments, namely – n, fr, tr and ar. The aforementioned source code of this puzzle is the outcome of application of recursive function. Hanoifun(n, 'A', 'C', 'B') // A, B and C are the name of rod Int n = 4 // n immplies the number of discs Printf("\n Move disk %d from rod %c to rod %c", n, fr, tr) Printf("\n Move disk 1 from rod %c to rod %c", fr, tr) Void hanoifun(int n, char fr, char tr, char ar)//fr=from rod,tr =to rod, ar=aux rod – Transfer n-1 discs from rod B to C so that they sit over disc n Tower of Hanoi in C Using Recursion: /* C program for Tower of Hanoi*/ – Move n-1 discs from rod A to B which causes disc n alone in on the rod A The algorithm or the working procedure, which is to be repeated for a finite number of steps, is illustrated below:Ī, B and C are rods or pegs and n is the total number of discs, 1 is the largest disk and 5 is the smallest one. So, the key to solving this puzzle is to break the problem down into a number of smaller problems and further break these into even smaller ones, so that it is made a typical best-suited problem for the application of the recursive function. The source code for solving Tower of Hanoi in C is based on recursion.
0 Comments
Leave a Reply.AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |