C program to implement Towers of Hanoi and Binary Search

C Recursion:
C Program to implement Towers of Hanoi and Binary Search using the Recursion method. Recursive functions solves the complexity of the problem by calling the function again and again itself. Using recursive methods we can save execution time and memory. In this program we have two recursive functions for Binary search and the Tower of Hanoi problem. Read more about C Programming Language .

* You can use all the programs on www.c-program-example.com
* for personal and learning purposes. For permissions to use the
* programs for commercial purposes,
* contact [email protected]
* To find more C programs, do visit www.c-program-example.com
* and browse!
* Happy Coding

main() {
int n, a[50], key, opn, i, pos;

do {
" nn Press 1 -> Binary Search , 2-> Towers of Hanoi 3-> Quitn");
scanf("%d", &opn);
switch (opn) {
case 1:
printf(" How Many Elements?");
scanf("%d", &n);
printf(" Read all the elements is ASC order n");
for (i = 1; i <= n; i++)
scanf("%d", &a[i]);
printf(" Read the Key Elementn");
scanf("%d", &key);
pos = BS(a, key, 1, n);
if (pos)
printf(" Success: %d found at %d n", key, pos);
printf(" Falure: %d Not found in the list ! n", key);
case 2:
printf("nn How Many Disks ?");
scanf("%d", &n);
printf("nn Result of Towers of Hanoi for %d Disks n", n);
tower(n, 'A', 'B', 'C');
printf("nn Note: A-> Source, B-> Intermediate, C-> Destinationn");
case 3:
printf(" Terminating n");
printf(" Invalid Option !! Try Again !! n");
printf(" Press a Key. . . ");
} while (opn != 3);

int BS(int a[], int key, int low, int high) {
int mid;

if (low > high)
return 0; /* failure */
else {
mid = (low + high) / 2;
if (a[mid] == key)
return mid; /* Success */
if (key < a[mid])
return (BS(a, key, low, mid - 1));
return (BS(a, key, mid + 1, high));

tower(int n, char src, char intr, char dst) {
if (n > 0) {
tower(n - 1, src, dst, intr);
printf("Move disk %d from %c to %c n", n, src, dst);
tower(n - 1, intr, src, dst);

Read more Similar C Programs

Data Structures

C Recursion

You can easily select the code by double clicking on the code area above.

To get regular updates on new C programs, you can Follow @c_program

You can discuss these programs on our Facebook Page. Start a discussion right now,

our page!

Share this program with your Facebook friends now! by liking it

(you can send this program to your friend using this button)

Like to get updates right inside your feed reader? Grab our feed!

(c) www.c-program-example.com

Leave a Reply