C Program for Tower of Hanoi Problem Using Recursion

Here I shared C program for tower of hanoi problem using recursion.

The Tower of Hanoi is a mathematical game or puzzle. It consists of a number of disks of different sizes which can slide on any of three rods(pegs). The game starts with the disks arranged in ascending order in stack on one rod but makes sure the smallest at the top all-time and thus making a conical shape as shown in below image.

C program for Tower of Hanoi


Aim of the game or puzzle is to move the whole entire stack to another rod, Rules to move all disks over to 3 towers but you can not place a larger disk on a smaller disk but few rules to be followed are −
  1. Only one disk move at a time.
  2. Only "top" disk can be removed.
  3. In each movement taking the top disk from one tower to top of another tower but you can not place larger on a smaller disk.
With three disks, the game or puzzle can be solved in seven moves. The minimum number of moves required to solve a Tower of Hanoi puzzle is 2n-1, where n is the number of disks.

Animated solution of the Tower of Hanoi Puzzle


C program for Tower of Hanoi


C program using recursion is given below which finds solution for Tower of Hanoi Problem.

C Program for Tower of Hanoi Problem Using Recursion

#include


void hanoi(int n, char source_rod, char dest_rod, char aux_rod);

void main()

{

	int n;

	printf("Enter Number of Disks:\t");

	scanf("\n%d",&n);

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

	return;

}

void hanoi(int n, char from_rod, char to_rod, char aux_rod) 

{ 

    if(n<=0)

    {

 	    printf("Wrong Number of disk\n");

    }

    else if (n == 1) 

    { 

        printf("\n Move disk 1 from rod %c to rod %c", from_rod, to_rod); 

        return; 

    } 

    else

    {

    hanoi(n-1, from_rod, aux_rod, to_rod); 

    printf("\n Move disk %d from rod %c to rod %c", n, from_rod, to_rod); 

    hanoi(n-1, aux_rod, to_rod, from_rod);

    }

}

(Avoid line 58)

OUTPUT


Enter Number of Disks:  3

 Move disk 1 from rod A to rod C
 Move disk 2 from rod A to rod B
 Move disk 1 from rod C to rod B
 Move disk 3 from rod A to rod C
 Move disk 1 from rod B to rod A
 Move disk 2 from rod B to rod C
 Move disk 1 from rod A to rod C
--------------------------------

NOTE: Comment below if you have doubts or found anything incorrect related to an above program for tower of hanoi in C.

Tower of Hanoi

Tower of Hanoi is a mathematical game or puzzle was invented by the French mathematician Edouard Lucas in 1883. It consists of 3 towers and n numbers of different sizes disks which can easily move on any rod.

Aim or objective is to move all disks over to 3 towers but you can not place a larger disk on a smaller disk but few rules to be followed are −
  1. Only one disk move at a time.
  2. Only "top" disk can be removed.
  3. In each movement taking the top disk from one tower to top of another tower but you can not place larger on a smaller disk.

Tower of Hanoi

Formula

For finding the number of moves for n disks is 2n-1 is moves required.
For example, for 3 disks 23-1=7 moves required.

Algorithm

Here I shared an algorithm for Tower of Hanoi, We solve an algorithm for three towers with name, A for sourceB fo destinationC (for help in moving the disks) and N is number of disks.
An Algorithm as follows:

START
  Hanoi(A, B, C, N) 
 IF N<=0 
  Wrong No. of Disk 
 ELSE IF N == 1, THEN
  move N from A to B // move disk from source to destination
 ELSE Hanoi (A, B, C, N-1) // Step 1 
  move N from A to B // move disk from source to destination 
 Hanoi (C, B, A, N-1) // Step 3
 END IF END
Exit

HTML Introduction

HTML is a short form of Hypertext Markup Language. It is developed by Tim Berners-Lee in 1900, Tim Berners-Lee invented WWW(World Wide Web). He also created the first Website.

It is a text-based approach to describing the HTML file structured and uses a browser to interpret how to display the text, images and other forms of multimedia(audio and video, etc.) on WWW. Everything that you see on the web is documents written in a special language called one version of HTML or another and interpret by web browsers like Netscape or Internet Explorer or Google Chrome etc. Web browser knows how to display text as images, links and other supported media on the screen. 

It provides a basic structure of the web pages and for changes its appearance we use Cascading Style Sheets or CSS. CSS is like a skin of structure which provides user-friendly and attractive to a user. 


Different Versions:

In the early days of the world wide web, marking up text-based documents using HTML. So, there have been many versions:



different versions of html

HTML Tags

It provides many layout commands, called 'tags' that control the presentation of information on the pages. For example, there are various types of tags for "image", "heading", "lines", "paragraph", "table", and so on.The main structure of page makes use of <head>, <title> and <body> tags.

Example to Explain Elements:

An element is an individual component of the webpage.

  1. <html> element define whole document and all other elements are contain in this.
  2. <head> element contains processing information and meta information about the document.
  3. <title> element doesnot contain other element only text and specifies a title for the document.
  4. <body> element defines display document.
  5. <h1> element defines Heading.
  6. <p> the element defines Paragraph.
and so on.

Tags look like? 

HTML Tags
HTML Tags

In the above tag example, Elements are written with start tag or with attributes and closed with forward slash end tag, content written in between. Each tag contained within less than and greater than brackets. The Opening tags can contain Attribute name-Attribute value pairs known as attributes.

Page Structure

Here I shared a basic webpage layout written in HTML and description of each section.
Below box contains a key section to the basic web page.
The first line contains the opening tag lets the browser know that it is reading code form here. The head tag contains all meta-information about the page such as title, location of CSS. Heading tag contains heading and paragraph tag contains a paragraph of your heading.

Basic HTML Structure
HTML Basic Structure

How to Create, view and file extension used with HTML?

As we know it is markup language, it can be created in the text editor like Notepad, Notepad++, etc. and It used .htm or .html file extension to save this file. 
Once the file is created and save it can be viewed locally on desktop using browser or upload to a webserver to be viewed by a browser.

Web Browser

Browsers like Google Chrome, Internet Explorer, Firefox, Edge, and Safari is used to display our written documents:

my first page open in web browser
My First Page


Circular Queue Implementation in C

Circular Queue is a linear data structure which is implemented in circular form rather than a straight array and avoids the wastage of space in Queue using arrays. If we joint both ends of the linear queue then it overcomes the problem of unutilized space in linear queue implemented as an array. 

circular queue in data structure
Circular Queue in Data Structure



Here I shared C Program for Insertion and Deletion in Circular Queue as an Array. Circular Queue stored in the memory as array in c programming. 

In case of Insertion of elements in Circular Queue, the first element insertion takes place in the circular queue then you have to increase your front and rear from "front=-1" and "rear=-1" then you have to shift both with an increment of +1 and your front and rear at "front=0" and "rear=0" then you insert your first element otherwise your "front=0" is fixed and increment your "rear=rear+1" to insert more. 

In case of Deletion of elements in Circular Queue, if "front==-1" than deletion is not possible then check front and rear both are at the same place then a front and rear both shifted to "front=-1" and "rear=-1" otherwise "front==size-1"(where size is the size of queue whose both ends are joint) than shift "front=0" or at last you increase your front with "front=front+1" to delete. 

Implementation of Circular Queue
 Implementation of Circular Queue

Algorithm: Insertion in Circular Queue

  1. Start
  2. If ((rear = = size-1 && front==0) || (rear = = front - 1))
  3.        printf("Circular Queue is Overflow");
  4. Else If (rear = = size -1 && front ! = 0) 
  5.            rear=0;
  6.            CQ[rear]=item;
  7.        Else If (front = = -1)
  8.            front=0; 
  9.            rear=0;
  10.            Q[rear]=item;
  11. End   

Algorithm: Deletion in Circular Queue

  1. Start
  2. If (front = = -1)
  3.        printf("Circular Queue is Underflow");
  4. Else
  5.       item=CQ[front];
  6.        If (front = = rear)
  7.            front = -1;
  8.            rear = -1;
  9.        Else If (front = = size - 1)
  10.            front=0;
  11.        Else
  12.            front = front +1;
  13.     printf("%d is delete");
  14. End


Following C Program for Circular Queue implementation in C then display the result on the screen:

#include
 
#define size 5
 
void cinsert();
void cdelete();
void cdisplay();
int cqueue[size];
int rear = - 1;
int front = - 1;

main()
{
    int choice;
    while (1)
    {
        printf("\n 1.Insert element to CIRCULAR QUEUE \n");
        printf("2.Delete element from CIRCULAR QUEUE \n");
        printf("3.Display all elements of CIRCULAR QUEUE \n");
        printf("4.Quit \n");
        printf("Enter your choice : ");
        scanf("%d", &choice);
        switch (choice)
        {
            case 1:cinsert();
                   break;
            case 2:cdelete();
                   break;
            case 3:cdisplay();
                   break;
            case 4://exit(0);
            default:printf("Wrong choice \n");
        } 
    } 
} 
 
void cinsert()
{
   int item;
   printf("Insert the element in queue : ");
                   scanf("%d", &item);
    if ((rear == size - 1 && front == 0) ||(rear == front-1))
    {
    printf("Circular Queue Overflow \n");
    }
 else 
 {
   if (rear== size -1 && front != 0)
             {
       rear = 0;
                cqueue[rear]=item;
             }
             else
    {
    if(front==-1)
    {
     front=0;
    rear = 0;
        cqueue[rear] = item;
    }
  }
 }
} 
 
void cdelete()
{
 int item;
    if (front == - 1)
    {
        printf("Circular Queue Underflow \n");
    }
    else
    { 
       item = cqueue[front];
        if(front==rear)
        { 
          front=-1;
          rear=-1;
  }
  else if(front==size-1)
  {
   front=0;
  }
  else 
  {
   front=front+1;
  }
  printf("Element deleted from queue is : %d\n", cqueue[front+1]);
     }
} 
 
void cdisplay()
{
 int i;        
   if(front == -1)  
      printf("\nCircular Queue is Empty!!!\n");  
   else  
   {  
      i = front;  
      printf("\nCircular Queue Elements are : \n");  
      if(front <= rear)
   {  
     while(i <= rear)  
        printf("%d \n",cqueue[i++]);  
      }  
      else
   {  
     while(i <= size - 1)  
        printf("%d \n", cqueue[i++]);  
     i = 0;  
     while(i <= rear)  
        printf("%d\n",cqueue[i++]);  
      }  
   }     
} 
avoid 116 line



When the above c program is compiled and executed, it will produce the following result:


circular queue

C Program for Insertion and Deletion in Queue using Array

Here I shared C Program for Insertion and Deletion in Queue using Array to
the implementation of Queue using array. Queue stored in the memory using an array with N elements entered by the user in c programming. 

In case of insertion of elements, the first element in the queue takes place in the queue then your front and rear at "front=-1" and "rear=-1" then you have to shift both with an increment of +1 and your front and rear at "front=0" and "rear=0" then you insert your first element otherwise your "front=0" is fixed and increment your "rear=rear+1" to insert more. 

In case of deletion of elements, take place with the front you only increase your front with "front=front+1" but if front and rear both are at the same place then a front and rear both shifted to "front=-1" and "rear=-1".


Algorithm: Insertion in Queue-Array

  1. Start
  2. If (rear = = size-1)
  3.        printf("Queue is Overflow");
  4. Else
  5.        If (front == -1)
  6.            front=0;
  7.            rear=rear+1;
  8.            Q[rear]=item;
  9.        Else
  10.            rear=rear+1;
  11.            Q[rear]=item;
  12. End   

Algorithm: Deletion in Queue-Array

  1. Start
  2. If (front == -1 && rear == -1)
  3.        printf("Queue is Underflow");
  4. Else
  5.        If (front > rear)
  6.            front = -1;
  7.            rear = -1;
  8.        Else
  9.            item=Q[rear];
  10.            printf("%d",item);
  11.            front=front+1;
  12. End

Following C Program for Insertion and Deletion in Queue using Array then display the result on the screen:


#include 
#define size 50
 
void insert();
void delete();
void display();
int queue[size];
int rear = - 1;
int front = - 1;
main()
{
    int choice;
    while (1)
    {
        printf("1.Insert element to QUEUE \n");
        printf("2.Delete element from QUEUE \n");
        printf("3.Display all elements of QUEUE \n");
        printf("4.Quit \n");
        printf("Enter your choice : ");
        scanf("%d", &choice);
        switch (choice)
        {
            case 1:insert();
                   break;
            case 2:delete();
                   break;
            case 3:display();
                   break;
            case 4:exit(1);
            default:printf("Wrong choice \n");
        } 
    } 
} 
 
void insert()
{
    int item;
    if (rear == size - 1)
    printf("Queue Overflow \n");
    else
    {
        if (front == - 1)
                front = 0;
        printf("Inset the element in queue : ");
        scanf("%d", &item);
        rear = rear + 1;
        queue[rear] = item;
    }
} 
 
void delete()
{
    if (front == - 1 || front > rear)
    {
        printf("Queue Underflow \n");
    }
    else
    {
        printf("Element deleted from queue is : %d\n", queue[front]);
        front = front + 1;
    }
} 
 
void display()
{
    int i;
    if (front == - 1)
        printf("Queue is empty \n");
    else
    {
        printf("Queue is : \n");
        for (i = front; i <= rear; i++)
            printf("%d ", queue[i]);
        printf("\n");
    }
} 
(Avoid 77 line)

When the above c program is compiled and executed, it will produce the following result:


C Program for Insertion and Deletion in Queue using Array

Queue Data Structure

What is a Queue Data Structure?

Queue is also a linear data structure which can be implemented either as an array or a linked list. Queue is open at both ends. Queue insertions take place at 'rear' end and deletions take place at 'front' end. Logically queue works on the concept of FIFO(First IN First OUT) data structure which means that element inserted first and deleted first.

In real life how to queue system works. If you go to deposit fees and you are first in a queue, then you will be the first one to deposit fees. Next, If you drive a vehicle on the one-way road where first enter vehicle exit first. 

Basic Queue Operations:

  1. The process of add item in a queue is called Enqueue.
  2. The process of removing an item from the queue is called Dequeue.
Queue in Data Structure
QUEUE 
In Queue 'front' stores the index of the first element in the queue and 'rear' stores the index of the last element in the queue, when a queue is an array. 

Applications of Queue:

  1. Queue is used in CPU task Scheduling when multiple processes require CPU at the same time.
  2. Queue also used in call center system to hold the call in order until the representative free.
  3. When data is transfer asynchronously between two processes like IO Buffers.
  4. Queue is used in Round Robin scheduling.
  5. Operating System uses a queue to maintain processes of ready to executed or wait for a particular event to occur.

C Program to Find Maximum and Minimum of N Numbers with Array

To find maximum and Minimum numbers from N numbers of an Array entered by the user in c programming, just user entered numbers into an array and display N numbers of an array. Assume the first element as Minimum i.e. min = arr[0] and Maximum i.e.max = arr[0] and Check all entered numbers using for loop that current element is greater than max or current element is less than min and display Maximum and Minimum at the end of for loop.  

C Program to Find Maximum and Minimum of N Numbers with Array


Following C Program to Find Maximum and Minimum of N Numbers with Array ask the user to enter 5 numbers using for loop to check Maximum and Minimum, then display the result on the screen:

/*
 * C program to find maximum and minimum of N numbers with array
 */
#include<stdio.h>
#define size 5 // Maximum array size

int main()
{
int i,max,min,a[size];
printf("Enter elements in array");
for(i=0;i<=4;i++)
{
printf("\nEnter element number \t",i+1);
scanf("%d",&a[i]);
}
printf("\nEntered 10 arrray elements are:-\t");
for(i=0;i<=4;i++)
{
printf("\n\ta[%d]=%d\n",i,a[i]);
}
/* Assume first element as maximum and minimum */
max=a[0];
min=a[0];
/*
     * Find Maximum and Minimum in All Array Elements.
     */
for(i=0;i<=4;i++)
{
if(a[i]>max) // If current element is greater than max
{
    max=a[i];
        }
       else
{
if(a[i]<min) // If current element is smaller than min
{
  min=a[i];
}
}
}
printf("\nMaximum = %d",max); /* Print maximum number */
printf("\nMinimum = %d",min); /* Print minimum number */
return 0;
}

When the above c program is compiled and executed, it will produce the following result:


C Program to Find Maximum and Minimum of N Numbers with Array