Monday, 4 August 2014

D.F.S Programs


                                          PROGRAMMES





  • FIBONANNIC SERIES


#include<stdio.h>
#include<conio.h>
void fibo(int i, int j, int t)
{
if(i==0)
printf(" %d ",i);
if(t<=1)
return;
printf(" %d ",j);
fibo(j, i+j, --t);
}

void main()
{
int t;
clrscr();
printf("\nEnter the no. of terms :- ");
scanf("%d",&t);
fibo(0,1,t);
getch();
}




  • BINARY SEARCH


#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
void main()
{
int a[100],i,n,loc,beg,end,mid,flag=0,item;
clrscr();
printf("How many elements ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
printf("Enter element to search ");
scanf("%d",&item);
loc=0;
beg=0;
end=n-1;
while((beg<=end)&&(item!=a[mid]))
{
mid=((beg + end)/2);
if (item == a[mid])
{
printf("item found");
loc=mid;
printf("position of element %d",loc+1);
flag=flag+1;

}
else if(item < a[mid])
end=mid-1;
else
beg=mid+1;
}
if(flag==0)
printf("The search unsucessful");

getch();
}

  • LINEAR SEARCH


#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
void main()
{
int a[100],n,i,item,loc=0;
clrscr();
printf("\nEnter no. of elements :- ");
scanf("%d",&n);
printf("\nEnter the numbers");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}

printf("\nEnter the number to search: - ");
scanf("%d",&item);
for(i=0;i<n;i++)
{
if(item == a[i])
{
loc=i;
break;
}
}
if (loc>=0)
 printf("\nThe location of the number is  %d",loc+1);
else
 printf("\nThe number does not exist");

 getch();
}



QUEUE PROGRAM



#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
# define n 10

void insert();
int del();
int queue[n];
int front=-1,rear=-1;
int main()
{
int choice;
char ch;
system("cls");
printf("\n 1.INSERT");
printf("\n 2.DELETE");
printf("\n\n Enter your choice ");
scanf("%d",&choice);

switch(choice)
{
case 1 :insert();
break;

case 2 :printf("\n The deleted item is %d",del());
break;

}
return 0;
}

void insert()
{
int item;
if(rear>= n)
{
printf("The queue is full");
exit(0);
}
else
{
printf("Enter Element");
scanf("%d",&item);
rear = rear + 1;
queue[rear]=item;
printf("The item is %d ",item);
}
}


int del()
{
int item;
if(front<0)
{
printf("The queue is empty");
exit(0);
}
else
{
item = queue[front];
front=front + 1;
}
return(item);
}

  • AVERAGE NUMBER



#include<stdio.h>
#include<conio.h>
void main()
{
int number[20];
int i;
int sum=0;
float avg;
clrscr();
for(i=0;i<=19;i++)
{
printf("\nenter the numbers:");

scanf("%d",&number[i]);
}
for(i=0;i<=19;i++)
{sum=sum+number[i];
avg=sum/20;
}
printf(" the avg:%f",avg);

}