SlideShare a Scribd company logo
1 of 11
Prepaid by:
Hina S. Murdhani
Enr: 150030107062
Div : bx3
Tower of Hanoi (TOH)
• In tower of Hanoi we have N disks in tower
A.
• We are suppose to go all N disks from
tower A to C in a such way that smaller
disk always appears on larger disk.
• To solve this problem we have give the
extra tower B.
Rules for Tower of Hanoi
• Only one disk can be moved at a time.
• No larger disk can appears on a smaller
disk.
• Only one top most disk can be moved
from one tower and it can appear only at
the top most position of another tower.
• You are to transfer all disk to the right
hand tower in the fewest possible.
Algorithm for TOH
• // N : no of disks
• // A,B,C : towers
if (n>=1)
{
TOH (n-1,A,C,B)
print (A->C)
TOH (n-1,B,A,C)
}
No of movements in TOH
No of disks movement
1 1
2 3
3 7
4 15
5 31
Due to this we can say that no of
movement=2^n -1
#include<stdio.h>
#include<conio.h>
void TOH(int,char,char,char);
int count=0;
void main()
{
int n;
clrscr();
printf("enter no of disks");
scanf("%d",&n);
TOH(n,'A','B','C');
printf("total no of movement=%d",count);
getch();
}
void TOH(int n,char a, char b,char c)
{
if(n>=1)
{
TOH(n-1,a,c,b);
printf("n move %c -> %c",a,c);
Programming for TOH
Tower of Hanoi Programming Solution

More Related Content

What's hot (20)

Tower of Hanoi using AI technique - means ends analysis
Tower of Hanoi using AI technique - means ends analysisTower of Hanoi using AI technique - means ends analysis
Tower of Hanoi using AI technique - means ends analysis
 
SINGLE SOURCE SHORTEST PATH.ppt
SINGLE SOURCE SHORTEST PATH.pptSINGLE SOURCE SHORTEST PATH.ppt
SINGLE SOURCE SHORTEST PATH.ppt
 
trees in data structure
trees in data structure trees in data structure
trees in data structure
 
Red black tree
Red black treeRed black tree
Red black tree
 
Heap tree
Heap treeHeap tree
Heap tree
 
Heap and heapsort
Heap and heapsortHeap and heapsort
Heap and heapsort
 
Branch and bound
Branch and boundBranch and bound
Branch and bound
 
Sorting
SortingSorting
Sorting
 
Graphs bfs dfs
Graphs bfs dfsGraphs bfs dfs
Graphs bfs dfs
 
introduction to_trees
introduction to_treesintroduction to_trees
introduction to_trees
 
Linked list implementation of Queue
Linked list implementation of QueueLinked list implementation of Queue
Linked list implementation of Queue
 
9. chapter 8 np hard and np complete problems
9. chapter 8   np hard and np complete problems9. chapter 8   np hard and np complete problems
9. chapter 8 np hard and np complete problems
 
Coin Change : Greedy vs Dynamic Programming
Coin Change : Greedy vs Dynamic ProgrammingCoin Change : Greedy vs Dynamic Programming
Coin Change : Greedy vs Dynamic Programming
 
Heap sort
Heap sort Heap sort
Heap sort
 
Red black tree
Red black treeRed black tree
Red black tree
 
SINGLE-SOURCE SHORTEST PATHS
SINGLE-SOURCE SHORTEST PATHS SINGLE-SOURCE SHORTEST PATHS
SINGLE-SOURCE SHORTEST PATHS
 
Red black tree in data structure
Red black tree in data structureRed black tree in data structure
Red black tree in data structure
 
Tower of Hanoi
Tower of HanoiTower of Hanoi
Tower of Hanoi
 
Algorithms - "Chapter 2 getting started"
Algorithms - "Chapter 2 getting started"Algorithms - "Chapter 2 getting started"
Algorithms - "Chapter 2 getting started"
 
Prim's algorithm
Prim's algorithmPrim's algorithm
Prim's algorithm
 

Viewers also liked

Tower of hanoi problem
Tower of hanoi problemTower of hanoi problem
Tower of hanoi problemTyler Murphy
 
towers of hanoi
towers of hanoitowers of hanoi
towers of hanoistudent
 
Algorithmic puzzles - #83 restricted tower of hanoi
Algorithmic puzzles - #83  restricted tower of hanoiAlgorithmic puzzles - #83  restricted tower of hanoi
Algorithmic puzzles - #83 restricted tower of hanoiNicholas Chen
 
Artificial intelligence - TSP
Artificial intelligence - TSP Artificial intelligence - TSP
Artificial intelligence - TSP Tung Le
 
Araling Panlipunan Grade 8 - First Quarter Module
Araling Panlipunan Grade 8 - First Quarter ModuleAraling Panlipunan Grade 8 - First Quarter Module
Araling Panlipunan Grade 8 - First Quarter ModuleJhing Pantaleon
 

Viewers also liked (8)

Tower of hanoi
Tower of hanoiTower of hanoi
Tower of hanoi
 
Tower of hanoi problem
Tower of hanoi problemTower of hanoi problem
Tower of hanoi problem
 
towers of hanoi
towers of hanoitowers of hanoi
towers of hanoi
 
Tower of Hanoi
Tower of HanoiTower of Hanoi
Tower of Hanoi
 
Algorithmic puzzles - #83 restricted tower of hanoi
Algorithmic puzzles - #83  restricted tower of hanoiAlgorithmic puzzles - #83  restricted tower of hanoi
Algorithmic puzzles - #83 restricted tower of hanoi
 
Artificial intelligence - TSP
Artificial intelligence - TSP Artificial intelligence - TSP
Artificial intelligence - TSP
 
AP G8/G9 lm q1
AP G8/G9 lm q1AP G8/G9 lm q1
AP G8/G9 lm q1
 
Araling Panlipunan Grade 8 - First Quarter Module
Araling Panlipunan Grade 8 - First Quarter ModuleAraling Panlipunan Grade 8 - First Quarter Module
Araling Panlipunan Grade 8 - First Quarter Module
 

Recently uploaded

Full Stack Web Development Course for Beginners
Full Stack Web Development Course  for BeginnersFull Stack Web Development Course  for Beginners
Full Stack Web Development Course for BeginnersSabitha Banu
 
Types of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptxTypes of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptxEyham Joco
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsanshu789521
 
What is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERPWhat is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERPCeline George
 
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfEnzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfSumit Tiwari
 
Gas measurement O2,Co2,& ph) 04/2024.pptx
Gas measurement O2,Co2,& ph) 04/2024.pptxGas measurement O2,Co2,& ph) 04/2024.pptx
Gas measurement O2,Co2,& ph) 04/2024.pptxDr.Ibrahim Hassaan
 
Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17Celine George
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxNirmalaLoungPoorunde1
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTiammrhaywood
 
Historical philosophical, theoretical, and legal foundations of special and i...
Historical philosophical, theoretical, and legal foundations of special and i...Historical philosophical, theoretical, and legal foundations of special and i...
Historical philosophical, theoretical, and legal foundations of special and i...jaredbarbolino94
 
Roles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceRoles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceSamikshaHamane
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...Marc Dusseiller Dusjagr
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxmanuelaromero2013
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxpboyjonauth
 
Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...Jisc
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
CELL CYCLE Division Science 8 quarter IV.pptx
CELL CYCLE Division Science 8 quarter IV.pptxCELL CYCLE Division Science 8 quarter IV.pptx
CELL CYCLE Division Science 8 quarter IV.pptxJiesonDelaCerna
 

Recently uploaded (20)

Full Stack Web Development Course for Beginners
Full Stack Web Development Course  for BeginnersFull Stack Web Development Course  for Beginners
Full Stack Web Development Course for Beginners
 
Types of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptxTypes of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptx
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha elections
 
What is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERPWhat is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERP
 
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfEnzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
 
Gas measurement O2,Co2,& ph) 04/2024.pptx
Gas measurement O2,Co2,& ph) 04/2024.pptxGas measurement O2,Co2,& ph) 04/2024.pptx
Gas measurement O2,Co2,& ph) 04/2024.pptx
 
Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptx
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
 
Historical philosophical, theoretical, and legal foundations of special and i...
Historical philosophical, theoretical, and legal foundations of special and i...Historical philosophical, theoretical, and legal foundations of special and i...
Historical philosophical, theoretical, and legal foundations of special and i...
 
ESSENTIAL of (CS/IT/IS) class 06 (database)
ESSENTIAL of (CS/IT/IS) class 06 (database)ESSENTIAL of (CS/IT/IS) class 06 (database)
ESSENTIAL of (CS/IT/IS) class 06 (database)
 
9953330565 Low Rate Call Girls In Rohini Delhi NCR
9953330565 Low Rate Call Girls In Rohini  Delhi NCR9953330565 Low Rate Call Girls In Rohini  Delhi NCR
9953330565 Low Rate Call Girls In Rohini Delhi NCR
 
Roles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceRoles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in Pharmacovigilance
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptx
 
OS-operating systems- ch04 (Threads) ...
OS-operating systems- ch04 (Threads) ...OS-operating systems- ch04 (Threads) ...
OS-operating systems- ch04 (Threads) ...
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptx
 
Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
 
CELL CYCLE Division Science 8 quarter IV.pptx
CELL CYCLE Division Science 8 quarter IV.pptxCELL CYCLE Division Science 8 quarter IV.pptx
CELL CYCLE Division Science 8 quarter IV.pptx
 

Tower of Hanoi Programming Solution

  • 1. Prepaid by: Hina S. Murdhani Enr: 150030107062 Div : bx3
  • 2. Tower of Hanoi (TOH) • In tower of Hanoi we have N disks in tower A. • We are suppose to go all N disks from tower A to C in a such way that smaller disk always appears on larger disk. • To solve this problem we have give the extra tower B.
  • 3. Rules for Tower of Hanoi • Only one disk can be moved at a time. • No larger disk can appears on a smaller disk. • Only one top most disk can be moved from one tower and it can appear only at the top most position of another tower. • You are to transfer all disk to the right hand tower in the fewest possible.
  • 4. Algorithm for TOH • // N : no of disks • // A,B,C : towers if (n>=1) { TOH (n-1,A,C,B) print (A->C) TOH (n-1,B,A,C) }
  • 5.
  • 6.
  • 7.
  • 8.
  • 9. No of movements in TOH No of disks movement 1 1 2 3 3 7 4 15 5 31 Due to this we can say that no of movement=2^n -1
  • 10. #include<stdio.h> #include<conio.h> void TOH(int,char,char,char); int count=0; void main() { int n; clrscr(); printf("enter no of disks"); scanf("%d",&n); TOH(n,'A','B','C'); printf("total no of movement=%d",count); getch(); } void TOH(int n,char a, char b,char c) { if(n>=1) { TOH(n-1,a,c,b); printf("n move %c -> %c",a,c); Programming for TOH