You are on page 1of 2

Bangladesh University of Business and Technology

Lab Report: 9

Dept. name: Computer Science and Engineering

Course name: Data Structure Lab

Course code: CSE 232

Submitted By: Rafsan Samin


22234103208

Submitted To: Md. Mahbub-Or-Rashid


Assistant professor,
Dept. of Computer Science and Engineering

Submission Date: 11-12-2023


Tower of Hanoi with recursion

#include <iostream>

void towerOfHanoi(int n, char source, char auxiliary, char destination) {


if (n == 1) {
std::cout << "Move disk 1 from " << source << " to " << destination << std::endl;
return;
}

towerOfHanoi(n - 1, source, destination, auxiliary);


std::cout << "Move disk " << n << " from " << source << " to " << destination <<
std::endl;
towerOfHanoi(n - 1, auxiliary, source, destination);
}

int main() {
int n = 3; // Number of disks
towerOfHanoi(n, 'A', 'B', 'C');
return 0;
}

Output:
Move disk 1 from A to C
Move disk 2 from A to B
Move disk 1 from C to B
Move disk 3 from A to C
Move disk 1 from B to A
Move disk 2 from B to C
Move disk 1 from A to C

You might also like