hanoi tower game
#include<stdio.h>
/*************************************************************************/
void towers(int n,char frompeg,char topeg,char auxpeg)
{
if(n==1)
{
printf("\n%s%c%s%c","move disk 1 from peg ",frompeg," to peg ",topeg);
return;
}
towers(n-1,frompeg,auxpeg,topeg);
printf("\n%s%d%s%c%s%c","move disk ",n," from peg ",frompeg,
"to peg",topeg);
towers(n-1,auxpeg,topeg,frompeg);
}
/************************************************************************/
void main()
{
int n;
scanf("%d",&n);
towers(n,'A','B','C');
}
amatallah
0
Newbie Poster
Micko
2
Junior Poster
manutd
2
Junior Poster
sharma tiptur
-4
Newbie Poster
Salem
commented:
ToH has three pegs, your post is 3 YEARS too late - coincidence?
-4
Nick Evan
4,005
Industrious Poster
Team Colleague
Featured Poster
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.