0% found this document useful (0 votes)
43 views2 pages

Tower of Hanoi

This document describes an experiment to implement the Tower of Hanoi problem with a C program. The program takes the number of discs as input and uses a recursive function to print the steps to move the discs from the starting to ending peg. It successfully executes in the dev c++ software to solve the Tower of Hanoi problem.

Uploaded by

Sudip
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
43 views2 pages

Tower of Hanoi

This document describes an experiment to implement the Tower of Hanoi problem with a C program. The program takes the number of discs as input and uses a recursive function to print the steps to move the discs from the starting to ending peg. It successfully executes in the dev c++ software to solve the Tower of Hanoi problem.

Uploaded by

Sudip
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

Experiment 7

 AIM:
Write a program to implement Tower of Hanoi with n numbers of disc.

 Apparatus Required : dev c++ Software.

 Code:

#include<stdio.h>
void hanoi_tower(char,char,char,int);
void hanoi_tower(char peg1,char peg2, char peg3,int n){
if(n<=0){
printf("\n illegal entry");
}
if(n==1){
printf("\n move disk from %c to %c",peg1,peg3);
}
else{
hanoi_tower(peg1,peg3,peg2,n-1);
hanoi_tower(peg1,peg2,peg3,1);
hanoi_tower(peg2,peg1,peg3,n-1);
}
}

int main(){
int n;
printf("\n input the number of disc:");
scanf("%d",&n);
printf("\n tower of hanoi for %d disc",n);
hanoi_tower('1','2','3',n);
return 0;
}
 Output:

 Conclusion: The program written above was successfully executed in the dev c++ software.

You might also like