Practice Real Company Assessment Patterns • Latest 2025–2026 Mock Tests

Programming Exercises

Write a PHP script to implement the Tower of Hanoi algorithm recursively.

Medium

Problem Statement

Write a PHP script to implement the Tower of Hanoi algorithm recursively.

Sample Input

3 disks

Sample 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

Limits

Time: 2s · Memory: 256 MB

Code Editor
00:00
<?php function towerOfHanoi($n, $from, $to, $aux) { // Your code here } ?>
Log in or sign up to run or submit your code.Log inSign up
Input
3 disks
Expected 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