Showing posts with label Data Structure Using c Programs. Show all posts
Showing posts with label Data Structure Using c Programs. Show all posts

Write a C program to create file for N students, it should contain Roll-No, Name and Marks in two subjects. Using the above created file, create an out put file which contains Roll-No, Name, Marks in subjects, Total and Average.

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
void main()
{
 FILE *fp,*fout;
 int rollno,n,sub1,sub2,total,i;
 char name[20];
 float avg;
 clrscr();
 fp=fopen("student.dad","w");
 if(fp==NULL)
 {
 puts("\n file cannot be opened");
 getch();
 exit(0);
 }
 printf("\n enter the number of students:");
 scanf("%d",&n);
 for(i=0;i<n;i++)
 {
  printf("student %d",i+1);
  printf("enter the rollno,name,marks in sub1,marks in sub2\n");
  scanf("%d%s%d%d",&rollno,name,&sub1,&sub2);
  fprintf(fp,"%d%s%d%d",rollno,name,sub1,sub2);
  }
  fclose(fp);
  printf("\n student file created successfully ");
  fp=fopen("student.dat","r");
  if(fp==NULL)
  {
   puts("\n file cannot be opened");
   getch();
   exit(0);
  }
  fout=fopen("output.dat","w");
  if(fout==NULL)
  {
  puts("\n file cannot be opened ");
  getch();
  exit(0);
  }
 fprintf(fout,"\n rollno name sub1 sub 2 total average \n\n");
 while(fscanf(fp,"%d%s%d%d",&rollno,name,&sub1,&sub2)!=EOF)
 {
  total=sub1+sub2;
  avg=total/2.0;
  fprintf(fout,"%d\t%s\t%d\t%d\n",rollno,name,sub1,sub2,total,avg);
  }
  fclose(fout);
  }

Write a C program to search for an element using sequential search

#include<stdio.h>
#include<conio.h>
int search(int *,int,int);
void main()
{
 int num[100],loc=-1,i,n,ele;
 clrscr();
 printf("enter the number of elements to be inserted:");
 scanf("%d",&n);
 printf("\nenter the elements :");
 for(i=0;i<n;i++)
 scanf("%d",&num[i]);
 printf("\n enter the element to be searched:");
 scanf("%d",&ele);
 loc=search(num,ele,n);
 if(loc<0)
 printf("\n elements not found ");
 else
 printf("elements found at %d position",loc+1);
 getch();
 }
 int search(int num[],int item,int n)
 {
  int i;
  for(i=0;i<n;i++)
  {
   if(num[i]==item)
   return i;
   }
  return -1;
  }

Write a C program to convert and print a given valid fully parenthesized in fix arithmetic expression to post fix expression, the expression consists of single character (letter or digit) as operands and +, -, * , / as operators, assume that only binary operators are allowed in the expression

 #include<stdio.h>
 #include<conio.h>
 #include<string.h>
 main()
 {
  char ch,stack[50],infix[50];
  char ans='y';
  int i,j,top=0;
  clrscr();
  while((ans=='y')||(ans=='Y'))
  {
   stack[0]='(';
   printf("\n infix to postfix expression");
   printf("\n enter the infix expression");
   gets(infix);
   fflush(stdin);
   j=strlen(infix);
   printf("\nthe postfix expression is :");
   for(i=0;i<j;i++)
   {
    ch=infix[i];
    if(((ch>='0')&&(ch<='9'))||((ch>='a')&&(ch<='z'))||((ch>='A')&&(ch<='Z')))
    printf("%c",ch);
    if(ch=='(')
    stack[++top]=ch;
    if((ch=='*')||(ch=='/'))
    {
     while((stack[top]=='*')||(stack[top]=='/'))
     printf("%c",stack[top--]);
     stack[++top]=ch;
     }
     if((ch=='+')||(ch=='-'))
     {
      while((stack[top]=='*')||(stack[top]=='/')||(stack[top]=='+')||(stack[top]=='-'))
      {
       printf("%c",stack[top]);
       top--;
       }
       stack[++top]=ch;
       }
       if(ch==')')
       {
    while(stack[top]!='(')
    printf("%c",stack[top--]);
    top--;
       }
      }
      while(stack[top]!='(')
      printf("%c",stack[top--]);
      printf("\ndo you want to continue(y/n)");
      scanf("%c",&ans);
      fflush(stdin);
      }
      return 0;
      }

Write a C program to find ncr using recursion

#include<stdio.h>
#include<conio.h>
int fact(int);
void main()
{
 int n,r,ncr;
 clrscr();
 printf("\n enter n and r:");
 scanf("%d%d",&n,&r);
 ncr=fact(n)/(fact(n-r)*fact(r));
 printf("\n Binomial co-efficient : %d",ncr);
 getch();
 }
  int fact(int x)
  {
   if(x==0)
   return 1;
   else
   return(x*fact(x-1));
  }

Write a C program to sort a list of N elements of integer type using quick sort Algorithm

#include<stdio.h>
#include<conio.h>
void quicksort(int a[],int lb,int ub);
void main()
{
 int i,n,a[20];
 clrscr();
 printf("\n * * * * *  * * * * *");
 printf("\n      INPUT       \n");
 printf("enter the size of the array:");
 scanf("%d",&n);
 printf("\n enter the elements of the array :");
 for(i=0;i<n;i++)
 scanf("%d",&a[i]);
 printf("\n * * * * * * * * * * *");
 printf("\n        OUTPUT ");
 printf("\n orginal list \n");
 for(i=0;i<n;i++)
 printf("%d\t",a[i]);
 quicksort(a,0,n-1);
 printf("\n * * * * * * * * * * \n");
 printf("\n sorted list\n ");
 for(i=0;i<n;i++)
 printf("%d\t",a[i]);
 getch();
 }
 void quicksort(int a[],int lb,int ub)
 {
  int up,down,temp,key;
  int flag=1 ;
  if(lb<ub)
  {
   up=lb;
   down=ub;
   key=a[lb];
   while(flag)
   {
    up++;
    while(a[up]<key)
    up++;
    while(a[down]>key)
    down--;
    if(up<down)
    {
     temp=a[up];
     a[up]=a[down];
     a[down]=temp;
    }
    else
    flag=0;
    }
    temp=a[lb];
    a[lb]=a[down];
    a[down]=temp;
    quicksort(a,lb,down-1);
    quicksort(a,down+1,ub);
    }
   }

Write a C program to sort a list of N elements of integer type using heap sort Algorithm

#include<stdio.h>
#include<conio.h>
void heapsort(int [],int);
void createheap();
void main()
{
int k[10],i,n;
clrscr();
printf("\n ! ! ! ! ! ! ! ! ! ");
printf("\n      INPUT      ");
printf("\n enter how many elements ");
scanf("%d",&n);
printf("\n enter array elements");
for(i=0;i<n;i++)
scanf("%d",&k[i]);
printf("\n ! ! ! ! ! ! ! ! \n");
printf("\n * * * * * * * * *\n");
printf("\n        output     \n ");
printf("\n orginal list  \n");
for(i=1;i<=n;i++)
printf("%4d",k[i]);
heapsort(k,n);
printf("\n* * * * * * * * * * *");
printf("\n sorted list \n");
for(i=1;i<=n;i++)
printf("%4d",k[i]);
printf("\n * * * * * * *  * *\n");
getch();
return;
}
void createheap(int k[],int n)
{
 int temp,key,i,q,j;
 for(q=2;q<=n;q++)
 {
  i=q;
  key=k[q];
  j=i/2;
  while((i>1)&&(key>k[j]))
  {
   temp=k[j];
   k[j]=k[i];
   k[i]=temp;
   i=j;
   j=i/2;
   if(j<1)
   j=1;
   }
   k[i]=key;
   }
   return;
   }
   void heapsort(int k[],int n)
   {
    int temp,q,i,j,key;
    createheap(k,n);
    for(q=n;q>=2;q--)
    {
     temp=k[q];
     k[q]=k[i];
     k[i]=temp;
     i=1;
     j=2;
     key=k[i];
     if((j+1)<q)
     {
      if(k[j+1]>k[j])
      j++;
      }
  while((j<=(q-1))&&(k[j]>key))
  {
   temp=k[j];
   k[j]=k[i];
   k[i]=temp;
   i=j;j=2*i;
   if(j+1<q)
   {
    if(k[j+1]>k[j])
    j++;
    else
    if(j>n)
    j=n;
    }
    k[i]=key;
   }
  }
  return;
  }

Using Dynamic variables and pointers construct Binary search tree of integers, Write C functions to do the following:

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#define NULL 0
struct node
{
 struct node *left;
 int info;
 struct node *right;
};
void insert(struct node **,int);
void inorder(struct node *);
void postorder(struct node *);
void preorder(struct node *);
void main()
{
 struct node *ptr;
 int n,i,item,ch;
 ptr=NULL;
 clrscr();
 while(1)
 {
  printf("\n BINARY search tree");
  printf("\n 1.creation of bst");
  printf("\n 2.preorder traversal");
  printf("\n 3.inorder traversal");
  printf("\n 4.postorder traversal");
  printf("\n 5.exit");
  printf("\n enter your choice");
  scanf("%d",&ch);
  switch(ch)
  {
   case 1:printf("enter number of terms to add to tree");
      scanf("%d",&n);
      for(i=0;i<n;i++)
      {
       printf("\nenter the element");
       scanf("%d",&item);
       insert(&ptr,item);
       }
       break;
  case 2:printf("preorder traversal");
         preorder(ptr);
         break;
      case 3:printf("inorder traversal");
         inorder(ptr);
         break;
      case 4:printf("postorder traversal");
         postorder(ptr);
         break;
      case 5:exit(0);
      }
   }
   }
   void insert(struct node **p,int item)
   {
    if((*p)==NULL)
    {
     printf("\n node created");
     (*p)=malloc(sizeof(struct node));
     (*p)->left=NULL;
     (*p)->right=NULL;
     (*p)->info=item;
     return;
     }
    else
    if (item<(*p)->info)
    {
     printf("\n directed to left link");
     insert(&((*p)->left),item);
     }
    else
    {
    printf("directed to right link");
    insert(&((*p)->right),item);
    }
    return;
    }
    void inorder(struct node *p)
    {
     if(p!=NULL)
     {
      inorder(p->left);
      printf("\n%d",p->info);
      inorder(p->right);
      }
      else
      return;
      }
      void preorder(struct node *p)
      {
      if(p!=NULL)
      {
      printf("\n%d",p->info);
      preorder(p->left);
      preorder(p->right);
      }
     else
     return;
     }
     void postorder(struct node *p)
     {
      if(p!=NULL)
      {
       postorder(p->left);
       postorder(p->right);
       printf("\n %d",p->info);
       }
       else
       return;
      }

Write a C program to sort a list of N elements using Merge sort Algorithm

#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<stdlib.h>
#define NULL 0
create();
display();
del_item(int);
ins_beg();
search(int);
struct node
{
 int rollno;
 char name[20];
 struct node *link;
 } *start=NULL,*p,*q,*prev;
 int regno,rno;
 char str[20];
 void main()
 {
  int ch,i;
  clrscr();
  while(1)
  {
   printf("\n 1.create a linked list ");
   printf("\n 2.insertion of node in front of the list ");
   printf("\n 3.deletion of node based on rollno");
   printf("\n 4.searching a node based on rollno");
   printf("\n 5.display linked list ");
   printf("\n 6.exit");
   printf("\n enter your choice:");
   scanf("%d",&ch);
   switch(ch)
   {
    case 1:create();
    break;
    case 2:printf("\n linked list before insertion\n");
    display();
    ins_beg();
    printf("\n linked list after insertion\n");
    display();
    break;
    case 3:printf("\n enter the item to be deleted:");
    scanf("%d",&regno);
    del_item(regno);
    printf("\n linked list after deletion");
    display();
    break;
    case 4:printf("\n searching for an node based on rollno");
       scanf("%d",&rno);
       search(rno);
       break;
    case 5:display();
        break;
    case 6:exit(0);
    default:printf("\n error in choice");
    }
    }
    }
    create()
    {
     char choice='y';
     clrscr();
     start=NULL;
     q=start;
     while(choice=='y')
     {
      p=malloc(sizeof(struct node));
      printf("\n enter the register number :");
      scanf("%d",&regno);
      printf("\n enter the student name:");
      scanf("%s",str);
      p->rollno=regno;
      strcpy(p->name,str);
      p->link=NULL;
      if(start==NULL)
      {
       start=p;
       q=p;
       }
       else
       {
    q->link=p;
    q=p;
    }
       printf("\n do you want to create another node (y/n");
       choice=getch();
       }
       return 0;
       }
       display()
       {
       printf("\n the elements are");
       p=start;
       while(p!=NULL)
       {
    printf("\n register number :%d",p->rollno);
    printf("\n NAME :%s",p->name);
    p=p->link;
    }
    getch();
       return 0;
       }
       ins_beg()
       {
    printf("\n enter register number:");
    scanf("%d",&regno);
    printf("\n enter name of the student:");
    scanf("%s",str);
    p=malloc(sizeof(struct node));
    p->rollno=regno;
    strcpy(p->name,str);
    p->link=start;
    start=p;
    return 0;
       }
       del_item(int regno)
       {
    p=start;
       prev=NULL;
       if(start==NULL)
       {
    printf("\n linked list is empty");
    return 0;
    }
    if(start->rollno==regno)
    {
    start=start->link;
    free(p);
    return 0;
    }
       while((p->rollno!=regno)&&(p!=NULL))
       {
    prev=p;
    p=p->link;
       }
       if(p==NULL)
       printf("\n register number %d is not found in the linked list \n",regno);
       else
       prev->link=p->link;
       return 0;
       }
       search(int rno)
       {
    int i=0;
    p=start;
    while(p!=NULL)
    {
     if(rno==p->rollno)
     {
          i++;
          printf("\n node found at position %d",i);
          printf("register number:%d",p->rollno);
          printf("\n name:%s",p->name);
          }
          else
          {
          p=p->link;
          i++;
          }
     }
     printf("\n node with register number %d does not exist",regno);
     return 0;
    }

Using dynamic variables and pointers Write a C program to construct a singly linked list consisting of the following information in each node; Roll – No (Integer), Name (Character string)

#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<stdlib.h>
#define NULL 0
create();
display();
del_item(int);
ins_beg();
search(int);
struct node
{
 int rollno;
 char name[20];
 struct node *link;
 } *start=NULL,*p,*q,*prev;
 int regno,rno;
 char str[20];
 void main()
 {
  int ch,i;
  clrscr();
  while(1)
  {
   printf("\n 1.create a linked list ");
   printf("\n 2.insertion of node in front of the list ");
   printf("\n 3.deletion of node based on rollno");
   printf("\n 4.searching a node based on rollno");
   printf("\n 5.display linked list ");
   printf("\n 6.exit");
   printf("\n enter your choice:");
   scanf("%d",&ch);
   switch(ch)
   {
    case 1:create();
    break;
    case 2:printf("\n linked list before insertion\n");
    display();
    ins_beg();
    printf("\n linked list after insertion\n");
    display();
    break;
    case 3:printf("\n enter the item to be deleted:");
    scanf("%d",&regno);
    del_item(regno);
    printf("\n linked list after deletion");
    display();
    break;
    case 4:printf("\n searching for an node based on rollno");
       scanf("%d",&rno);
       search(rno);
       break;
    case 5:display();
        break;
    case 6:exit(0);
    default:printf("\n error in choice");
    }
    }
    }
    create()
    {
     char choice='y';
     clrscr();
     start=NULL;
     q=start;
     while(choice=='y')
     {
      p=malloc(sizeof(struct node));
      printf("\n enter the register number :");
      scanf("%d",&regno);
      printf("\n enter the student name:");
      scanf("%s",str);
      p->rollno=regno;
      strcpy(p->name,str);
      p->link=NULL;
      if(start==NULL)
      {
       start=p;
       q=p;
       }
       else
       {
    q->link=p;
    q=p;
    }
       printf("\n do you want to create another node (y/n");
       choice=getch();
       }
       return 0;
       }
       display()
       {
       printf("\n the elements are");
       p=start;
       while(p!=NULL)
       {
    printf("\n register number :%d",p->rollno);
    printf("\n NAME :%s",p->name);
    p=p->link;
    }
    getch();
       return 0;
       }
       ins_beg()
       {
    printf("\n enter register number:");
    scanf("%d",&regno);
    printf("\n enter name of the student:");
    scanf("%s",str);
    p=malloc(sizeof(struct node));
    p->rollno=regno;
    strcpy(p->name,str);
    p->link=start;
    start=p;
    return 0;
       }
       del_item(int regno)
       {
    p=start;
       prev=NULL;
       if(start==NULL)
       {
    printf("\n linked list is empty");
    return 0;
    }
    if(start->rollno==regno)
    {
    start=start->link;
    free(p);
    return 0;
    }
       while((p->rollno!=regno)&&(p!=NULL))
       {
    prev=p;
    p=p->link;
       }
       if(p==NULL)
       printf("\n register number %d is not found in the linked list \n",regno);
       else
       prev->link=p->link;
       return 0;
       }
       search(int rno)
       {
    int i=0;
    p=start;
    while(p!=NULL)
    {
     if(rno==p->rollno)
     {
          i++;
          printf("\n node found at position %d",i);
          printf("register number:%d",p->rollno);
          printf("\n name:%s",p->name);
          }
          else
          {
          p=p->link;
          i++;
          }
     }
     printf("\n node with register number %d does not exist",regno);
     return 0;
    }

Write a C program to simulate the working of an Circular Queue using an array. Provide the operations CQINSERT, CQDELETE and CQDISPLAY. Check the Circular Queue status for empty and full.

#include<stdio.h>
#include<conio.h>
#define n 3
int front=-1,rear=-1;
int queue[n],item;
int isempty();
main()
{
 void cqinsert();
 void cqdelete();
 void cqdisplay();

 int choice;
 clrscr();
 while(1)
 {
  printf("\n circular queue simulator");
  printf("\n 1.insert");
  printf("\n 2.display");
  printf("\n 3.delete");
  printf("\n 4.exit ");
  printf("\n enter your choice");
  scanf("%d",&choice);
  switch(choice)
  {
   case 1:cqinsert();
   break;
   case 2:cqdisplay();
   break;
   case 3:cqdelete();
   break;
   case 4:exit();
    break;
    default:printf("\n invalid entry ");
    }
    }
    }
   void cqinsert()
   {
    if(cqfull())
    {
     printf("\n queue is full - overflow");
     return;
    }
   else
   {
    printf("\n enter the element to be added:");
    scanf("%d",&item);
    if(front==-1)
    front=rear=0;
    else
    rear=(rear+1)%n;
    queue[rear]=item;
    }
    return;
    }
    void cqdisplay()
    {
    int i;
    if(cqempty())
    {
    printf ("queue is empty");
    return;
    }
    printf("\n queue elements\n");
    if(front<=rear)
    {
    for(i=front;i<=n-1;i++)
    printf("\n%d",queue[i]);
    }
    if(front>rear)
    {
    for(i=front;i<=n-1;i++)
    printf("\n%d",queue[i]);
    for(i=0;i<=rear;i++)
    printf("%n%d",queue[i]);
    }
    }
    void cqdelete()
    {
    int item;
    if(cqempty())
    {
    printf("\n underflow");
    return;
    }
    else
    {
    item=queue[front];
    printf("\n deleted elements is%d",item);
    queue[front]=0;
    if(front==rear)
    {
    printf("\n queue has a single element");
    front=rear=-1;
    }
    else
    front=(front+1)%n;
    }
    return;
    }
    int cqempty()
    {
    if((front==-1)&&(rear==-1))
    return 1;
    else
    return 0;
    }
    int cqfull()
    {
    if((front==1)*(rear+1)%n)
    return 1;
    else
    return 0;
    }

Write a C program to simulate the working of an ordinary Queue using an array. Provide the operations QINSERT, QDELETE and QDISPLAY. Check the Queue status for empty and full.

#include<stdio.h>
#include<conio.h>
#define n 3
int front=-1,rear=-1;
int queue[n];
main()
{
 void qinsert(int);
 void qdelete();
 void qdisplay();
 int isfull();
 int isempty();
 int choice,item;
 clrscr();
 while(1)
 {
  printf("\n QUEUE PROGRAM \n");
  printf("\n 1.insert");
  printf("\n 2.delete");
  printf("\n 3.display");
  printf("\n 4.exit");
  printf("\n enter your choice :");
  scanf("%d",&choice);
  switch(choice)
  {
   case 1:printf("enter the number to be inserted :");
      scanf("%d",&item);
      qinsert(item);
      break;
   case 2:qdelete();
      break;
   case 3:qdisplay();
      break;
   case 4:exit();
   default:printf("\n invalid choice !!!");
    }
   }
 }
 void qinsert(int item)
 {
  if(isfull())
  {
   printf("\n overflow");
   return;
  }
  if(front && rear==-1)
   front=rear=0;
   else
   rear=rear+1;
   queue[rear]=item;
   if(rear==n-1)
   printf("\n queue is full");
   return;
  }
  void qdisplay()
  {
   int i;
   if(isempty())
   {
    printf("\n no elements is the queue ");
    return;
   }
   else
   {
    for(i=front;i<=rear;i++)
    printf("\n%d",queue[i]);
   }
   return;
  }
  void     qdelete()
  {
   int item;
   if(isempty())
   {
    printf("\n underflow");
    return;
    }
    else
    item=queue[front];
    printf("the elements deleted is %d ",item);
    if(front==rear)
    {
     front=-1;
     rear=-1;
     }
     else
     front++;
     return;
    }
    int isfull()
    {
     if(rear==n-1)
     return 1;
     else
     return 0;
    }
    int isempty()
    {
     if(front==-1)
     return 1;
     else
     return 0;
     }

Write a C program to demonstrate the working of stack of size N using an array. The elements of the stack may assume to be of type integer or real, the operations to be supported are 1. PUSH 2. POP 3. DISPLAY. The program should print appropriate messages for STACK overflow, Under flow and empty, use separate

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#define maxstk 3
int top=-1,stack[maxstk];
int isempty();
int isfull();
main()
{
void push(int);
void display();
void pop();

int choice,item;
clrscr();
while(1)
{
 printf("\n stack simulator ");
 printf("\n ---------------- ");
 printf("\n 1.push");
 printf("\n 2.display");
 printf("\n 3.pop");
 printf("\n 4.exit");
 printf("\n\n enter your choice :");
 scanf("%d",&choice);
 switch(choice)
 {
  case 1:printf("\n enter the elements to be added :");
     scanf("%d",&item);
     push(item);
     break;
  case 2:printf("enter the element to the stack :");
     display();
     break;
  case 3:pop();
     break;
  case 4:exit(1);
  default:printf("error in choice!!!");
    }
    }
  }

  void push(int item)
  {
    if(isfull())
     {
       printf("\n stack overflow ");
       return;
     }
    else
      top++;
      stack[top]=item;
   if(top==maxstk-1)
    printf("\n stack is full \n");
  }



  void display()
  {
   int i;
    if(isempty())
     {
       printf("stack is empty\n");
       return;
     }
    else
     {
       printf("\n stack elements \n");
       for(i=top;i>=0;i--)
       printf("%d\t",stack[i]);
     }
  }

  void pop()
  {
    int item;
    if(isempty())
     {
      printf("\n stack underflow");
      return;
     }
    else
     {
      item=stack[top];
      top=top-1;
      printf("\n the poped element is %d \n",item);
      if(top==-1)
      printf("\n stack is empty \n");
     }
  }
  int isempty()
  {
   if(top==-1)
   return 1;
   else
   return 0;
  }
  int isfull()
  {
   if(top==maxstk-1)
   return 1;
   else
   return 0;
  }

Write a C program to sort a list of N elements using Bubble sort Technique

#include<stdio.h>
#include<conio.h>
void main()
{
 int i,n,j,a[30],temp;
 clrscr();
 printf("enter the number of elements :");
 scanf("%d",&n);
 printf("enter the elements of the list:\n");
 for(i=0;i<n;i++)
 {
  scanf("%d",&a[i]);
 }
 printf("\n\n BUBBLE SORT \n\n");
 printf("\n orginal order \n ");
  printf(" * * * * * * * * * \n");
 for(i=0;i<n;i++)
  printf("%d\t",a[i]);

 for(i=0;i<n;i++)
 {
   for(j=0;j<n;j++)
    if(a[j]>a[j+1])
     {
       temp=a[j];
       a[j]=a[j+1];
       a[j+1]=temp;
     }
 }

 printf("\n\n SORTED ORDER  \n");
 printf("* * * * * * * * * * \n");
 for(i=0;i<n;i++)
 printf("%d\t",a[i]);
 printf("\n * * * * * * * * * * * \n");
 getch();
 }

Write a C program to search for an element in an array using Binary search

#include<stdio.h>
#include<conio.h>
int binarysearch(int,int[],int);
 void main()
 {
   int a[10],i,item,n;
   clrscr();
   printf("enter the size of the array:");
   scanf("%d",&n);
   for(i=1;i<=n;i++)
   {
     printf("Element[%d]:",i);
     scanf("%d",&a[i]);
   }
     printf("\n enter ther item to be searched:");
     scanf("%d",&item);
     binarysearch(n,a,item);
     getch();
   }

 int binarysearch(int n,int a[],int item)
  {
    int beg=1,end=n,mid;
    while(beg<=end)
  {
    mid=(beg+end)/2;
     if(item==a[mid])
      {
    printf("item %d is found at location %d",item,mid);
    return;
      }
     if(item > a[mid])
      {
    beg=mid+1;
      }
       else
    end=mid-1;
   }
    printf("\n item %d is not found",item);
    return;
  }