Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 22 additions & 18 deletions DataStructureLabPrograms/TowerOfHanoi.c
Original file line number Diff line number Diff line change
@@ -1,24 +1,28 @@
#define _CRT_SECURE_NO_WARNINGS
/*
* C program to simulate the tower of hanoi.
* Takes as input the number of disks, and generates
* the sequence of steps to move the disks from 'S' to 'D'.
*
*/

#include <stdio.h>
void TowerOfHanoi(int a, char from, char aux, char to)
{
if (a == 1)
{
printf("\nMove disc %d from %c to %c \n", a, from, to);

void towerOfHanoi(int a, char from, char aux, char to) {
if (a == 1) {
printf("Move disc %d from %c to %c\n", a, from, to);
return;
}
else
{
TowerOfHanoi(a - 1, from, to, aux);
printf("\nMove disc %d from %c to %c \n", a, from, to);
TowerOfHanoi(a - 1, aux, from, to);
}

towerOfHanoi(a - 1, from, to, aux);
printf("Move disc %d from %c to %c\n", a, from, to);
towerOfHanoi(a - 1, aux, from, to);
}
void main()
{

int main() {
int n;
printf("\nTower of Hanoi\n");
printf("\nEnter number of discs: ");

printf("Tower of Hanoi\n");
printf("Enter number of discs: ");
scanf("%d", &n);
TowerOfHanoi(n, 'S', 'A', 'D');
}
towerOfHanoi(n, 'S', 'A', 'D');
}