Write a Menu Driven Program for Towers of Hanoi and Binary Search in C

By | December 3, 2016

Menu Driven Program for Towers of Hanoi and Binary Search in C


Write a Menu Driven Program for Towers of Hanoi and Binary Search in C. Here’s a Simple Program for Towers of Hanoi and Binary Search in C using switch cases in C Programming Language.

  • In this program, first we create a menu to perform Tower of Hanoi and Binary Search in C.It involves Tower of Hanoi , Binary Search in C and Quit function to exit from the program.
  • We also create two functions : BS( ) for the binary search in C and tower( ) to perform Tower of Hanoi Algorithm.
  • Do while( ) Loop is used to continue iteration n times as long as the users wants to run the program. if the termination condition satisfies, it breaks out the loop and jump to the end of the main function.

Below is the source code for C Program for Towers of Hanoi and Binary Search in C which is successfully compiled and run on Windows System to produce desired output as shown below :


SOURCE CODE : :


#include<stdio.h>
#include<conio.h>

int BS(int [], int ,int ,int );
void tower(int , char , char , char );

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

        printf(" \n\n Press 1 -> Binary Search , 2-> Towers of Hanoi 3-> Quit\n");
        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 (Search) Element :: ");
            scanf("%d",&key);
            pos=BS(a,key,1,n);

            if(pos)
                printf(" Success: %d found at %d \n", key,pos);
            else
             printf(" Falure: %d Not found in the list ! \n",key);
            break;

        case 2:
            printf("\n\n How Many Disks ?");
            scanf("%d", &n);
            printf("\n\n Result of Towers of Hanoi for %d Disks \n",n);

            tower(n,'A','B','C');

            printf("\n\n Note: A-> Source, B-> Intermediate, C-> Destination\n");
            break;

        case 3:
            printf(" Terminating \n");
            break;
        default:
            printf(" Invalid Option !! Try Again !! \n");
        }

        printf(" Press a Key. . . ");
        getch();

    }while(opn != 3);

    return 0;
}
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));
    }
}
void 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);
    }
}

OUTPUT : :


Press 1 -> Binary Search , 2-> Towers of Hanoi 3-> Quit
1
 How Many Elements ? 6
 Read all the elements is ASC order ::
2
7
23
45
61
87
 Read the Key (Search) Element :: 50
 Falure: 50 Not found in the list !
 Press a Key. . .

 Press 1 -> Binary Search , 2-> Towers of Hanoi 3-> Quit
1
 How Many Elements ? 6
 Read all the elements is ASC order ::
2
5
7
9
34
56
 Read the Key (Search) Element :: 9
 Success: 9 found at 4
 Press a Key. . .

 Press 1 -> Binary Search , 2-> Towers of Hanoi 3-> Quit
2


 How Many Disks ?5


 Result of Towers of Hanoi for 5 Disks
Move disk 1 from A to C
Move disk 2 from A to B
Move disk 1 from C to B
Move disk 3 from A to C
Move disk 1 from B to A
Move disk 2 from B to C
Move disk 1 from A to C
Move disk 4 from A to B
Move disk 1 from C to B
Move disk 2 from C to A
Move disk 1 from B to A
Move disk 3 from C to B
Move disk 1 from A to C
Move disk 2 from A to B
Move disk 1 from C to B
Move disk 5 from A to C
Move disk 1 from B to A
Move disk 2 from B to C
Move disk 1 from A to C
Move disk 3 from B to A
Move disk 1 from C to B
Move disk 2 from C to A
Move disk 1 from B to A
Move disk 4 from B to C
Move disk 1 from A to C
Move disk 2 from A to B
Move disk 1 from C to B
Move disk 3 from A to C
Move disk 1 from B to A
Move disk 2 from B to C
Move disk 1 from A to C


 Note: A-> Source, B-> Intermediate, C-> Destination
 Press a Key. . .

 Press 1 -> Binary Search , 2-> Towers of Hanoi 3-> Quit
3
 Terminating
 Press a Key. . .
Process returned 0

If you found any error or any queries related to the above program or any questions or reviews , you wanna to ask from us ,you may Contact Us through our contact Page or you can also comment below in the comment section.We will try our best to reach upto you in the short interval.


Thanks for reading the post.

Leave a Reply