Program to generate a hash table.

#include <iostream>
using namespace std;

struct node {
int key;
node* next;
node() {
next=NULL;
}

};

typedef node* NODE;

int main() {
NODE hashtable[10];
//hashtable with 10 buckets
for(int i=0;i<10;i++) {
//initialize buckets to NULL
hashtable[i]=NULL;
}
int key;
while(1) {
cout<<"Enter key/bucket(only positive) (-1 to stop):";
cin>>key;

if(key==-1)
break;
NODE temp=new node;
temp->key=key;
//hashfunction:using the remainder hash function to find bucket
int bucket=key%10;
NODE head=hashtable[bucket];

temp->next=head;
hashtable[bucket]=temp;
}
while(1) {
cout<<"Enter key to search(-1 to stop):";
cin>>key;
if(key==-1)
break;
int bucket=key%10;
NODE head=hashtable[bucket];
bool found=false;
while(head!=NULL) {
if(head->key==key) {
found=true;
break;
}
head=head->next;
}
if(found)
cout<<"Key found, Bucket:"<<bucket<<endl;
else
cout<<"Not found"<<endl;
}
return 0;
}

Program for insertion and deletion in heap.

# include <stdio.h>
int arr[100],n;
main()
{
int choice,num;
n=0;/*Represents number of nodes in the heap*/
while(1)
{
printf("1.Insert\n");
printf("2.Delete\n");
printf("3.Display\n");
printf("4.Quit\n");
printf("Enter your choice : ");
scanf("%d",&choice);
switch(choice)
{
case 1:
printf("Enter the number to be inserted : ");
scanf("%d",&num);
insert(num,n);
n=n+1;
break;
case 2:
printf("Enter the number to be deleted : ");
scanf("%d",&num);
del(num);
break;
case 3:
display();
break;
case 4:
exit();
default:
printf("Wrong choice\n");
}/*End of switch */
}/*End of while */
}/*End of main()*/

display()
{       int i;
if(n==0)
{
printf("Heap is empty\n");
return;
}
for(i=0;i<n;i++)
printf("%d ",arr[i]);
printf("\n");
}/*End of display()*/

insert(int num,int loc)
{
int par;
while(loc>0)
{
par=(loc-1)/2;
if(num<=arr[par])
{
arr[loc]=num;
return;
}
arr[loc]=arr[par];
loc=par;
}/*End of while*/
arr[0]=num; /*assign num to the root node */
}/*End of insert()*/

del(int num)
{
int left,right,i,temp,par;

for(i=0;i<n;i++)
{
if(num==arr[i])
break;
}
if( num!=arr[i] )
{
printf("%d not found in heap\n",num);
return;
}
arr[i]=arr[n-1];
n=n-1;
par=(i-1)/2;   /*find parent of node i */
if(arr[i] > arr[par])
{
insert( arr[i],i);
return;
}
left=2*i+1;  /*left child of i*/
right=2*i+2; /* right child of i*/
while(right < n)
{
if( arr[i]>=arr[left] && arr[i]>=arr[right] )
return;
if( arr[right]<=arr[left] )
{
temp=arr[i];
arr[i]=arr[left];
arr[left]=temp;
i=left;
}
else
{
temp=arr[i];
arr[i]=arr[right];
arr[right]=temp;
i=right;
}
left=2*i+1;
right=2*i+2;
}/*End of while*/
if( left==n-1 && arr[i]<arr[left] ) /* right==n */
{    temp=arr[i];
arr[i]=arr[left];
arr[left]=temp;
}
}/*End of del()*/

Insertion ,Deletion and Traversal in fully in-threaded Binary Search Tree

# include <stdio.h>
# include <malloc.h>
#define infinity 9999
typedef enum { thread,link} boolean;
struct node *in_succ(struct node *p);
struct node *in_pred(struct node *p);

struct node
{
struct node *left_ptr;
boolean left;
int info;
boolean right;
struct node *right_ptr;
}*head=NULL;

main()
{
int choice,num;
insert_head();
while(1)
{
printf("\n");
printf("1.Insert\n");
printf("2.Delete\n");
printf("3.Inorder Traversal\n");
printf("4.Preorder Traversal\n");
printf("5.Quit\n");
printf("Enter your choice : ");
scanf("%d",&choice);

switch(choice)
{
case 1:
printf("Enter the number to be inserted : ");
scanf("%d",&num);
insert(num);
break;
case 2:
printf("Enter the number to be deleted : ");
scanf("%d",&num);
del(num);
break;
case 3:
inorder();
break;
case 4:
preorder();
break;
case 5:
exit();
default:
printf("Wrong choice\n");
}/*End of switch */
}/*End of while */
}/*End of main()*/

insert_head()
{
struct node *tmp;
head=(struct node *)malloc(sizeof(struct node));
head->info= infinity;
head->left=thread;
head->left_ptr=head;
head->right=link;
head->right_ptr=head;
}/*End of insert_head()*/

find(int item,struct node **par,struct node **loc)
{
struct node *ptr,*ptrsave;
if(head->left_ptr==head)  /* If tree is empty*/
{
*loc=NULL;
*par=head;
return;
}
if(item==head->left_ptr->info) /* item is at head->left_ptr */
{
*loc=head->left_ptr;
*par=head;
return;
}
ptr=head->left_ptr;
while(ptr!=head)
{
ptrsave=ptr;
if( item < ptr->info )
{
if(ptr->left==link)
ptr=ptr->left_ptr;
else
break;
}
else
if(item > ptr->info )
{
if(ptr->right==link)
ptr=ptr->right_ptr;
else
break;
}
if(item==ptr->info)
{
*loc=ptr;
*par=ptrsave;
return;
}
}/*End of while*/
*loc=NULL;   /*item not found*/
*par=ptrsave;
}/*End of find()*/

insert(int item)
{
struct node *tmp,*parent,*location;
find(item,&parent,&location);

if(location!=NULL)
{
printf("Item already present");
return;
}

tmp=(struct node *)malloc(sizeof(struct node));
tmp->info=item;
tmp->left=thread;
tmp->right=thread;

if(parent==head) /*tree is empty*/
{
head->left=link;
head->left_ptr=tmp;
tmp->left_ptr=head;
tmp->right_ptr=head;
}
else
if( item < parent->info )
{
tmp->left_ptr=parent->left_ptr;
tmp->right_ptr=parent;
parent->left=link;
parent->left_ptr=tmp;
}
else
{
tmp->left_ptr=parent;
tmp->right_ptr=parent->right_ptr;
parent->right=link;
parent->right_ptr=tmp;
}
}/*End of insert()*/

del(int item)
{
struct node *parent,*location;
if(head==NULL)
{
printf("Tree empty");
return;
}

find(item,&parent,&location);
if(location==NULL)
{
printf("Item not present in tree");
return;
}

if(location->left==thread && location->right==thread)
case_a(parent,location);
if(location->left==link && location->right==thread)
case_b(parent,location);
if(location->left==thread && location->right==link)
case_b(parent,location);
if(location->left==link && location->right==link )
case_c(parent,location);
}/*End of del()*/

case_a(struct node *par,struct node *loc )
{
if(par==head) /*item to be deleted is first node*/
{
head->left=thread;
head->left_ptr=head;
}
else
if(loc==par->left_ptr)
{
par->left=thread;
par->left_ptr=loc->left_ptr;
}

else
{
par->right=thread;
par->right_ptr=loc->right_ptr;
}
free(loc);
}/*End of case_a()*/

case_b(struct node *par,struct node *loc)
{
struct node *child,*s,*p;

/*Initialize child*/
if(loc->left==link) /*item to be deleted has left_ptr */
child=loc->left_ptr;
else                /*item to be deleted has right_ptr */
child=loc->right_ptr;

if(par==head )   /*Item to be deleted is first node*/
head->left_ptr=child;    //see this one
else
if( loc==par->left_ptr)   /*item is left_ptr of its parent*/
par->left_ptr=child;
else                 /*item is right_ptr of its parent*/
par->right_ptr=child;

s=in_succ(loc);
p=in_pred(loc);

if(loc->right==link)         /*if loc has right subtree*/
s->left_ptr=p;
else                           /*if loc has left subtree */
{
if( loc->left==link)
p->right_ptr=s;
}
free(loc);
}/*End of case_b()*/

case_c(struct node *par,struct node *loc)
{
struct node *ptr,*ptrsave,*suc,*parsuc,*s,*p;

/*Find inorder successor and its parent*/
ptrsave=loc;
ptr=loc->right_ptr;
while(ptr->left==link)
{
ptrsave=ptr;
ptr=ptr->left_ptr;
}

suc=ptr;
parsuc=ptrsave;

loc->info=suc->info;

if(suc->left==thread && suc->right==thread)
case_a(parsuc,suc);
else
case_b(parsuc,suc);
}/*End of case_c()*/

struct node *in_succ(struct node *ptr)
{
struct node *succ;
if(ptr->right==thread)
succ=ptr->right_ptr;
else
{
ptr=ptr->right_ptr;
while(ptr->left==link)
ptr=ptr->left_ptr;
succ=ptr;
}
return succ;
}/*End of in_succ()*/

struct node *in_pred(struct node *ptr)
{
struct node *pred;
if(ptr->left==thread)
pred=ptr->left_ptr;
else
{
ptr=ptr->left_ptr;
while(ptr->right==link)
ptr=ptr->right_ptr;
pred=ptr;
}
return pred;
}/*End of in_pred()*/

inorder()
{
struct node *ptr;
if(head->left_ptr==head)
{
printf("Tree is empty");
return;
}

ptr=head->left_ptr;
/*Find the leftmost node and traverse it */
while(ptr->left==link)
ptr=ptr->left_ptr;
printf("%d ",ptr->info);

while( 1 )
{
ptr=in_succ(ptr);
if(ptr==head)     /*If last node reached */
break;
printf("%d  ",ptr->info);
} /*End of while*/
}/*End of inorder()*/
preorder()
{
struct node *ptr;
if(head->left_ptr==head)
{
printf("Tree is empty");
return;
}

ptr=head->left_ptr;

while( ptr != head )
{
printf("%d ",ptr->info);
if( ptr->left==link )
ptr=ptr->left_ptr;
else
if(ptr->right_ptr==link)
ptr=ptr->right_ptr;
else
{
while( ptr!=head && ptr->right==thread )
ptr=ptr->right_ptr;
if(ptr!=head )
ptr=ptr->right_ptr;
}
}/*End of while*/
}/*End of preorder()*/

Program to implement Trapezoidal method.

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

char postfix[80];
float stack[80];
char stack1[80];
int top=-1,top1=-1;

float eval(char postfix[], float x1);
void infix_postfix(char infix[]);

main()
{
float x0, xn, h, s,e1,e2;
char exp[80], arr[80];
int i,n,l=0;
clrscr();
printf("\nEnter an expression: ");
gets(exp);
puts("Enter x0, xn and number of subintervals");
scanf("%f%f%d", &x0, &xn, &n);
h=(xn-x0)/n;
if(exp[0]=='l'&& exp[1]=='o'&& exp[2]=='g')
{
l=strlen(exp);
for(i=0;i<l-3; i++)
arr[0]=exp[i+3];
arr[i]='';
infix_postfix(arr);
e1=eval(postfix,x0);
e2=eval(postfix,xn);
s=log(e1)+log(e2);
for (i=1;i<=n-1;i++)
s+=2*log(eval(postfix,x0+i*h));
}
else
{
infix_postfix(exp);
s=eval(postfix,x0)+eval(postfix,xn);
for (i=1;i<=n-1;i++)
s+=2*eval(postfix,x0+i*h);
}
printf("Value of the integral is %6.3f\n",(h/2)*s);
return(0);
}
/*Inserting the operands in a stack. */
void push(float item)
{
if(top==99)
{
printf("\n\tThe stack is full");
getch();
exit(0);
}
else
{
top++;
stack[top]=item;
}
return;
}
/*Removing the operands from a stack. */
float pop()
{
float item;
if(top==-1)
{
printf("\n\tThe stack is empty\n\t");
getch();
}
item=stack[top];
top--;
return (item);
}
void push1(char item)
{
if(top1==79)
{
printf("\n\tThe stack is full");
getch();
exit(0);
}
else
{
top1++;
stack1[top1]=item;
}
return;
}
/*Removing the operands from a stack. */
char pop1()
{
char item;
if(top1==-1)
{
printf("\n\tThe stack1 is empty\n\t");
getch();
}
item=stack1[top1];
top1--;
return (item);
}

/*Converting an infix expression to a postfix expression. */
void infix_postfix(char infix[])
{
int i=0,j=0,k;
char ch;
char token;
for(i=0;i<79;i++)
postfix[i]=' ';
push1('?');
i=0;
token=infix[i];
while(token!='')
{
if(isalnum(token))
{
postfix[j]=token;
j++;
}
else if(token=='(')
{
push1('(');
}
else if(token==')')
{
while(stack1[top1]!='(')
{
ch=pop1();
postfix[j]=ch;
j++;
}
ch=pop1();
}
else
{

while(ISPriority(stack1[top1])>=ICP(token))
{
ch=pop1();
/*Assigning the popped element into the postfix array. */
postfix[j]=ch;
j++;
}
push1(token);
}
i++;
token=infix[i];
}
while(top1!=0)
{
ch=pop1();
postfix[j]=ch;
j++;
}
postfix[j]='';
}

int ISPriority(char token)
{
switch(token)
{
case '(':return (0);
case ')':return (9);
case '+':return (7);
case '-':return (7);
case '*':return (8);
case '/':return (8);
case '?':return (0);
default: printf("Invalid expression");
break;
}
return 0;
}
/*Determining the priority of elements that are approaching towards the stack. */
int ICP(char token)
{
switch(token)
{
case '(':return (10);
case ')':return (9);
case '+':return (7);
case '-':return (7);
case '*':return (8);
case '/':return (8);
case '':return (0);
default: printf("Invalid expression");
break;
}
return 0;
}
/*Calculating the result of expression, which is converted in postfix notation. */
float eval(char p[], float x1)
{
float t1,t2,k,r;
int i=0,l;
l=strlen(p);
while(i<l)
{
if(p[i]=='x')
push(x1);
else
if(isdigit(p[i]))
{
k=p[i]-'0';
push(k);
}
else
{
t1=pop();
t2=pop();
switch(p[i])
{
case '+':k=t2+t1;
break;
case '-':k=t2-t1;
break;
case '*':k=t2*t1;
break;
case '/':k=t2/t1;
break;
default: printf("\n\tInvalid expression");
break;
}
push(k);
}
i++;
}
if(top>0)
{
printf("You have entered the operands more than the operators");
exit(0);
}
else
{
r=pop();
return (r);
}
return 0;
}

Program to implement Simpson method in C.

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

char postfix[80];
float stack[80];
char stack1[80];
int top=-1,top1=-1;
float eval(char postfix[], float x1);
void infix_postfix(char infix[]);

main()
{
float x0, xn, h, s,e1,e2, e3;
char exp[80], arr[80];
int i,n,l=0;
clrscr();
printf("\nEnter an expression: ");
gets(exp);
puts("Enter x0, xn and number of sub-intervals: ");
scanf("%f%f%d", &x0, &xn, &n);
h=(xn-x0)/n;
if(exp[0]=='l'&& exp[1]=='o'&& exp[2]=='g')
{
l=strlen(exp);
for(i=0;i<l-3; i++)
arr[0]=exp[i+3];
arr[i]='';
infix_postfix(arr);
e1=eval(postfix,x0);
e2=eval(postfix,xn);
e3=4*eval(postfix, x0+h);
s=log(e1)+log(e2)+log(e3);
for (i=3;i<=n-1;i+=2)
s+=4*eval(postfix,x0+i*h)+2*eval(postfix, x0+(i-1)*h);
}
else
{
infix_postfix(exp);
s=eval(postfix,x0)+eval(postfix,xn)+4*eval(postfix, x0+h);
for (i=3;i<=n-1;i+=2)
s+=4*eval(postfix,x0+i*h)+2*eval(postfix, x0+(i-1)*h);
}
printf("The value of integral is %6.3f\n",(h/3)*s);
return(0);
}
/*Inserting the operands in a stack. */
void push(float item)
{
if(top==99)
{
printf("\n\tThe stack is full");
getch();
exit(0);
}
else
{
top++;
stack[top]=item;
}
return;
}
/*Removing the operands from a stack. */
float pop()
{
float item;
if(top==-1)
{
printf("\n\tThe stack is empty\n\t");
getch();
}
item=stack[top];
top--;
return (item);
}
void push1(char item)
{
if(top1==79)
{
printf("\n\tThe stack is full");
getch();
exit(0);
}
else
{
top1++;
stack1[top1]=item;
}
return;
}
/*Removing the operands from a stack. */
char pop1()
{
char item;
if(top1==-1)
{
printf("\n\tThe stack1 is empty\n\t");
getch();
}
item=stack1[top1];
top1--;
return (item);
}
/*Converting an infix expression to a postfix expression. */
void infix_postfix(char infix[])
{
int i=0,j=0,k;
char ch;
char token;
for(i=0;i<79;i++)
postfix[i]=' ';
push1('?');
i=0;
token=infix[i];
while(token!='')
{
if(isalnum(token))
{
postfix[j]=token;
j++;
}
else if(token=='(')
{
push1('(');
}
else if(token==')')
{
while(stack1[top1]!='(')
{
ch=pop1();
postfix[j]=ch;
j++;
}
ch=pop1();
}
else
{
while(ISPriority(stack1[top1])>=ICP(token))
{
ch=pop1();
postfix[j]=ch;
j++;
}
push1(token);
}
i++;
token=infix[i];
}
while(top1!=0)
{
ch=pop1();
postfix[j]=ch;
j++;
}
postfix[j]='';
}

/*Determining the priority of elements that are placed inside the stack. */
int ISPriority(char token)
{
switch(token)
{
case '(':return (0);
case ')':return (9);
case '+':return (7);
case '-':return (7);
case '*':return (8);
case '/':return (8);
case '?':return (0);
default: printf("Invalid expression");
}
return 0;
}

/*Determining the priority of elements that are approaching towards the stack. */
int ICP(char token)
{
switch(token)
{
case '(':return (10);
case ')':return (9);
case '+':return (7);
case '-':return (7);
case '*':return (8);
case '/':return (8);
case '':return (0);
default: printf("Invalid expression");
}
return 0;
}
/*Calculating the result of expression, which is converted in postfix notation. */
float eval(char p[], float x1)
{
float t1,t2,k,r;
int i=0,l;
l=strlen(p);
while(i<l)
{
if(p[i]=='x')
push(x1);
else
if(isdigit(p[i]))
{
k=p[i]-'0';
push(k);
}
else
{
t1=pop();
t2=pop();
switch(p[i])
{
case '+':k=t2+t1;
break;
case '-':k=t2-t1;
break;
case '*':k=t2*t1;
break;
case '/':k=t2/t1;
break;
default: printf("\n\tInvalid expression");
}
push(k);
}
i++;
}
if(top>0)
{
printf("You have entered the operands more than the operators");
exit(0);
}
else
{
r=pop();
return (r);
}
return 0;
}

Program to implement the linear regression algorithm in C.

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

float mean(float *a, int n);
void deviation(float *a, float mean, int n, float *d, float *S);

void main()
{
float a[20],b[20],dx[20],dy[20];
float sy=0,sx=0,mean_x=0,mean_y=0,sum_xy=0;
float corr_coff=0,reg_coff_xy=0, reg_coff_yx=0;
char type_coff[7];
int n=0,i=0;

clrscr();

printf("Enter the value of n: ");
scanf("%d",&n);
printf("Enter the values of x and y:\n");
for(i=0;i<n;i++)
scanf("%f%f",&a[i],&b[i]);
mean_x=mean(a,n);
mean_y=mean(b,n);
deviation(a,mean_x,n,dx,&sx);
deviation(b,mean_y,n,dy,&sy);

for(i=0;i<n;i++)
sum_xy=sum_xy+dx[i]*dy[i];
corr_coff=sum_xy/(n*sx*sy);
printf("Enter the type of regression coefficient as 'x on y' or 'y on x': ");
fflush(stdin);
gets(type_coff);

if(strcmp(type_coff,"x on y")==1)
{
reg_coff_xy=corr_coff*(sx/sy);
printf("\nThe value of linear regression coefficient is %f",reg_coff_xy);
}
else if(strcmp(type_coff,"y on x")==1)
{
reg_coff_yx=corr_coff*(sy/sx);
printf("\nThe value of linear regression coefficient is %f",reg_coff_yx);
}
else
printf("\nEnter the correct type of regression coefficient.");
getch();
}


float mean(float *a, int n)
{
float sum=0, i=0;
for(i=0;i<n;i++)
sum=sum+a[i];
sum=sum/n;
return (sum);
}

void deviation(float *a, float mean, int n, float *d, float *s)
{
float sum=0,t=0;
int i=0;
for(i=0;i<n;i++)
{
d[i]=a[i]-mean;
t=d[i]*d[i];
sum=sum+t;
}
sum=sum/n;
*s=sqrt(sum);
}

Program to implement the Newton- Gregory forward interpolation.

#include<stdio.h>
#include<conio.h>
#define MaxN 100
#define Order_of_diff 4

void main ()
{
float arr_x[MaxN+1], arr_y[MaxN+1], numerator=1.0, denominator=1.0, x, y, p, h, diff_table[MaxN+1][Order_of_diff+1];
int i,j,n,k;
clrscr();

printf("Enter the value of n \n");
scanf("%d",&n);
printf("Enter the values of x and y");

for(i=0; i<=n; i++)
scanf("%f%f", &arr_x[i], &arr_y[i]);
printf("Enter the value of x at which value of y is to be calculated");
scanf("%f", &x);
h=arr_x[1]-arr_x[0];

for(i=0; i<=n-1; i++)
diff_table[i][1]=arr_y[i+1]-arr_y[i];/*Creating the difference table and calculating first order differences*/
for(j=2; j<=Order_of_diff; j++)/*Calculating higher order differences*/
for(i=0; i<=n-j; i++)
diff_table[i][j]=diff_table[i+1][j-1] - diff_table[i][j-1];
i=0;

while(!(arr_x[i]>x)) /* Finding x0 */
i++;
i--;
p=(x-arr_x[i])/h;
y=arr_y[i];

for (k=1; k<=Order_of_diff; k++)
{
numerator *=p-k+1;
denominator *=k;
y +=(numerator/denominator)*diff_table[i][k];
}
printf("When x=%6.1f, y=%6.2f\n",x, y);
getch();
}

program to implement the Lagrange interpolation.

#include<stdio.h>
#include<conio.h>
#define MaxN 90

void main()
{
float arr_x[MaxN+1], arr_y[MaxN+1], numerator, denominator, x, y=0;
int i, j, n;
clrscr();
printf("Enter the value of n: \n");
scanf("%d", &n);
printf("Enter the values of x and y: \n");
for(i=0; i<=n; i++)
scanf("%f%f", &arr_x[i], &arr_y[i]);
printf("Enter the value of x at which value of y is to be calculated: ");
scanf("%f", &x);
for (i=0; i<=n; i++)
{
numerator=1;
denominator=1;
for (j=0; j<=n; j++)
if(j!=i)
{
numerator *= x-arr_x[j];
denominator *= arr_x[i]-arr_x[j];
}
y+=(numerator/denominator)*arr_y[i];
}
printf("When x=%4.1f y=%7.1f\n",x,y);
getch();
}

Program to perform Linear Search using recursive and non recursive function in C.

#include <stdio.h>
#define MAX_LEN 10

void l_search_recursive(int l[],int num,int ele);
void l_search(int l[],int num,int ele);
void read_list(int l[],int num);
void print_list(int l[],int num);

void main()
{
int l[MAX_LEN], num, ele;
int ch;

clrscr();

printf("======================================================");
printf("\n\t\t\tMENU");
printf("\n=====================================================");
printf("\n[1] Linary Search using Recursion method");
printf("\n[2] Linary Search using Non-Recursion method");
printf("\n\nEnter your Choice:");
scanf("%d",&ch);

if(ch<=2 & ch>0)
{
printf("Enter the number of elements :");
scanf("%d",&num);
read_list(l,num);
printf("\nElements present in the list are:\n\n");
print_list(l,num);
printf("\n\nElement you want to search:\n\n");
scanf("%d",&ele);

switch(ch)
{
case 1:printf("\n**Recursion method**\n");
l_search_recursive(l,num,ele);
getch();
break;

case 2:printf("\n**Non-Recursion method**\n");
l_search_nonrecursive(l,num,ele);
getch();
break;
}
}
getch();
}
/*end main*/

/* Non-Recursive method*/
void l_search_nonrecursive(int l[],int num,int ele)
{
int j, f=0;
for(j=0;j<num;j++)
if( l[j] == ele)
{
printf("\nThe element %d is present at position %d in list\n",ele,j);
f=1;
break;
}
if(f==0)
printf("\nThe element is %d is not present in the list\n",ele);
}

/* Recursive method*/
void l_search_recursive(int l[],int num,int ele)
{
int f = 0;

if( l[num] == ele)
{
printf("\nThe element %d is present at position %d in list\n",ele,num);
f=1;
}
else
{
if((num==0) && (f==0))
{
printf("The element %d is not found.",ele);
}
else
{
l_search(l,num-1,ele);
}
}
getch();
}

void read_list(int l[],int num)
{
int j;
printf("\nEnter the elements:\n");
for(j=0;j<num;j++)
scanf("%d",&l[j]);
}

void print_list(int l[],int num)
{
int j;
for(j=0;j<num;j++)
printf("%d\t",l[j]);
}

Program of Binary Search in C, using Recursive and Non Recursive functions.

#include <stdio.h>
#define MAX_LEN 10

/* Non-Recursive function*/
void b_search_nonrecursive(int l[],int num,int ele)
{
int l1,i,j, flag = 0;
l1 = 0;
i = num-1;
while(l1 <= i)
{
j = (l1+i)/2;
if( l[j] == ele)
{
printf("\nThe element %d is present at position %d in list\n",ele,j);
flag =1;
break;
}
else
if(l[j] < ele)
l1 = j+1;
else
i = j-1;
}
if( flag == 0)
printf("\nThe element %d is not present in the list\n",ele);
}

/* Recursive function*/
int b_search_recursive(int l[],int arrayStart,int arrayEnd,int a)
{
int m,pos;
if (arrayStart<=arrayEnd)
{
m=(arrayStart+arrayEnd)/2;
if (l[m]==a)
return m;
else if (a<l[m])
return b_search_recursive(l,arrayStart,m-1,a);
else
return b_search_recursive(l,m+1,arrayEnd,a);
}
return -1;
}

void read_list(int l[],int n)
{
int i;
printf("\nEnter the elements:\n");
for(i=0;i<n;i++)
scanf("%d",&l[i]);
}

void print_list(int l[],int n)
{
int i;
for(i=0;i<n;i++)
printf("%d\t",l[i]);
}

/*main function*/
void main()
{
int l[MAX_LEN], num, ele,f,l1,a;
int ch,pos;

clrscr();

printf("======================================================");
printf("\n\t\t\tMENU");
printf("\n=====================================================");
printf("\n[1] Binary Search using Recursion method");
printf("\n[2] Binary Search using Non-Recursion method");
printf("\n\nEnter your Choice:");
scanf("%d",&ch);

if(ch<=2 & ch>0)
{
printf("\nEnter the number of elements : ");
scanf("%d",&num);
read_list(l,num);
printf("\nElements present in the list are:\n\n");
print_list(l,num);
printf("\n\nEnter the element you want to search:\n\n");
scanf("%d",&ele);


switch(ch)
{
case 1:printf("\nRecursive method:\n");
pos=b_search_recursive(l,0,num,ele);
if(pos==-1)
{
printf("Element is not found");
}
else
{
printf("Element is found at %d position",pos);
}
getch();
break;

case 2:printf("\nNon-Recursive method:\n");
b_search_nonrecursive(l,num,ele);
getch();
break;
}
}
getch();
}