Page 421 - Computer science 868 Class 12
P. 421
The Java code to implement the above is given below:
import java.util.*;
class TowerHanoi {
int disk;
void accept()
{ Scanner sc=new Scanner(System.in);
System.out.println("Enter number of disks");
disk=sc.nextInt();
}
void shift(int top, char start, char mid, char end) {
if (top == 1)
{
System.out.println("Disk 1 from " + start + " to " + end);
} else {
shift(top - 1, start, end, mid);
System.out.println("Disk " + top + " from " + start + " to " + end);
shift(top - 1, mid, start, end);
}
}
void display()
{ shift(disk,’A’,’B’,’C’);}
public static void main() {
TowerHanoi ob=new TowerHanoi();
ob.accept();
ob.display();
}}
The output of the preceding program is as follows:
419
Recursion 419

