Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

The Tower of Hanoi puzzle was invented by the French mathematician Edouard Lucas

ID: 3722803 • Letter: T

Question

The Tower of Hanoi puzzle was invented by the French mathematician Edouard Lucas in 1883. We are given a tower of five disks (you can ask user for the number of disks), initially stacked in increasing size on one of three pegs. The objective is to transfer the entire tower to one of the other pegs, moving only one disk at a time and never a larger one onto a smaller. Write a C++ program to solve the puzzle. You must use recursion. Be creative and use your imagination for the output display. (such as 3D display…)

Please give solution different from 3d display. something different please

Explanation / Answer

#include <stdio.h>

// C recursive function to solve tower of hanoi puzzle

void towerOfHanoi(int n, char from_rod, char to_rod, char aux_rod)

{

    if (n == 1)

    {

        printf(" Move disk 1 from rod %c to rod %c", from_rod, to_rod);

        return;

    }

    towerOfHanoi(n-1, from_rod, aux_rod, to_rod);

    printf(" Move disk %d from rod %c to rod %c", n, from_rod, to_rod);

    towerOfHanoi(n-1, aux_rod, to_rod, from_rod);

}

int main()

{

    int n = 5; // Number of disks

    towerOfHanoi(n, 'A', 'C', 'B'); // A, B and C are names of rods

    return 0;

}

#include <stdio.h>

// C recursive function to solve tower of hanoi puzzle

void towerOfHanoi(int n, char from_rod, char to_rod, char aux_rod)

{

    if (n == 1)

    {

        printf(" Move disk 1 from rod %c to rod %c", from_rod, to_rod);

        return;

    }

    towerOfHanoi(n-1, from_rod, aux_rod, to_rod);

    printf(" Move disk %d from rod %c to rod %c", n, from_rod, to_rod);

    towerOfHanoi(n-1, aux_rod, to_rod, from_rod);

}

int main()

{

    int n = 5; // Number of disks

    towerOfHanoi(n, 'A', 'C', 'B'); // A, B and C are names of rods

    return 0;

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote