From e9464caee9e9c4ef24615222c77c0cba3c706101 Mon Sep 17 00:00:00 2001 From: Vraj Patel <56442375+vrajp3301@users.noreply.github.com> Date: Thu, 15 Oct 2020 13:09:31 +0530 Subject: [PATCH] Added Tower of Hanoi --- toh.cpp | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 toh.cpp diff --git a/toh.cpp b/toh.cpp new file mode 100644 index 0000000..814c813 --- /dev/null +++ b/toh.cpp @@ -0,0 +1,24 @@ +/*Implementation of Tower of Hanoi(Mathematical Puzzle)*/ + +#include + +void toh(int n, char rod1,char rod2,char rod3) +{ + if(n==1) + { + printf("\n Move disk 1 from rod %c to rod %c ",rod1,rod2); + return; + } + toh(n-1,rod1,rod3,rod2); + printf("\n Move disk %d from %c to rod %c",n,rod1,rod3); + toh(n-1,rod3,rod2,rod1); +} +int main() +{ + int n; + printf("Enter number of disks: ",n); + scanf("%d",&n); + toh(n,'A','B','C'); + return 0; + +}