From 18f633b42e3ff8509a8a1a7abefc5cd24a54dd6f Mon Sep 17 00:00:00 2001 From: Vatsalya Gupta <64428075+vatsalya-gupta@users.noreply.github.com> Date: Fri, 2 Oct 2020 15:28:32 +0530 Subject: [PATCH] Create towerOfHanoi.cpp --- towerOfHanoi.cpp | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 towerOfHanoi.cpp diff --git a/towerOfHanoi.cpp b/towerOfHanoi.cpp new file mode 100644 index 0000000..9486c4a --- /dev/null +++ b/towerOfHanoi.cpp @@ -0,0 +1,35 @@ +//Problem-Tower of Hanoi +// Tower of hanoi is a problem in which we have 3 rods and n disks. +// The objective of the problem is to move the entire stack to another rod, by following the below rules:- +// 1)Only one disk can be moved at a time. +// 2)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. +// 3)No disk may be placed on top of a smaller disk + + +#include +using namespace std; + +void towerOfHanoi(int n, char source,char destination, char intermediate) +{ + if (n == 1) + { + cout << "Move disk 1 from rod " << source <<" to rod " << destination<>n; + towerOfHanoi(n, 'S', 'I', 'D'); // S,I,D represents source ,intermediate and destination rods. + return 0; +}