pg1:-
#include<stdio.h>
#include<conio.h>
int ncr(int n,int r);
void main()
{
int n,r,y;
clrscr();
printf("Enter the value of n and r\n");
scanf("%d%d",&n,&r);
y=ncr(n,r);
printf("the value of ncr is %d\n",y);
getch();
}
int ncr(int n,int r)
{
int y,res;
if(r==0)
{
y=1; return y;
}
else if(n==r)
{
y=1; return y;
}
else
{
res=ncr(n-1,r-1)+ncr(n-1,r);
}
return res;
}
pg2:-
7#include<stdio.h>
#include<conio.h>
void towers(int,char,char,char);
void main()
{
int num;
clrscr();
printf("enter the number of disks:");
scanf("%d",&num);
printf("the sequence involved are:\n");
towers(num,'A','C','B');
getch();
}
void towers(int n,char fp,char tp,char ap)
{
if(n==1)
{
printf("\n move disk 1 from peg%c to peg%c",fp,tp);
return;
}
towers(n-1,fp,ap,tp);
printf("\n move disk %d from peg%c to peg%c",n,fp,tp);
towers(n-1,ap,tp,fp);
}
pg3:-
#include<stdio.h>
#include<conio.h>
int fibonacci(int);
void main()
{
int n,i=0,c;
printf("enter the fibonacci number");
scanf("%d",&n);
printf("fibonacci series\n");
for(c=1;c<=n;c++)
{
printf("%d\n",fibonacci(i));
i++;
}
getch();
}
int fibonacci(int n)
{
if(n==0)
return 0;
else if(n==1)
return 1;
else
return(fibonacci(n-1)+fibonacci(n-2));
}
pg4:-
#include<stdio.h>
void main()
{
int a[10],i,n,item,res=-1;
clrscr();
printf("enter array size\n");
scanf("%d",&n);
printf("\nenter array elements:\n");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("\nenter an item to search:");
scanf("%d",&item);
for(i=0;i<n;i++)
if(item==a[i])
{
printf("\nitem found at location %d",i+1);
res=1;
break;
}
if(res==-1)
printf("\nitem does not exist");
getch();
}
pg5:-
#include <stdio.h>
void main()
{
int c, first, last, middle, n, search, array[100];
printf("Enter number of elements\n");
scanf("%d",&n);
printf("Enter %d integers\n", n);
for (c = 0; c < n; c++)
scanf("%d",&array[c]);
printf("Enter value to find\n");
scanf("%d", &search);
first = 0;
last = n - 1;
middle = (first+last)/2;
while (first <= last) {
if (array[middle] < search)
first = middle + 1;
else if (array[middle] == search) {
printf("%d found at location %d.\n", search, middle+1);
break;
}
else
last = middle - 1;
middle = (first + last)/2;
}
if (first > last)
printf("Not found! %d isn't present in the list.\n", search);
getch();
}
pg6:-
#include<stdio.h>
#include<conio.h>
int item,a[100];
void heapsort(int[],int);
void heapify(int[],int);
void adjust(int[],int,int);
void main()
{
int temp,i,k,n,j;
clrscr();
printf("enter the size of an array\n");
scanf("%d",&n);
printf("enter the array element\n");
for(i=0;i<=n;i++)
scanf("%d",&a[i]);
heapsort(a,n);
printf("after sorting by max heap sort technique\n");
for(i=1;i<=n;i++)
printf("%d\n",a[i]);
getch();
}
void heapsort(int a[],int n)
{
int t,i;
heapify(a,n);
for(i=n;i>=2;i--)
{
t=a[i];
a[i]=a[1];
a[1]=t;
adjust(a,1,i-1);
}
}
void heapify(int a[],int n)
{
int i;
for(i=n/2;i>=1;i--)
adjust(a,i,n);
}
void adjust(int a[],int i,int n)
{
int j;
j=2*i;
item=a[i];
while(j<=n)
{
if((j<n)&&(a[j]<a[j+1]))
j=j+1;
if(item>=a[j])
break;
a[j/2]=a[j];
j=2*j;
}
a[j/2]=item;
}
pg9;-stacks using arrays
#include<stdio.h>
#include<conio.h>
#define n 5
int top,i,c,p;
int s[10];
int item;
int push()
{
if(top==n-1)
{
printf("\n stack overflow");
return;
}
top=top+1;
s[top]=item;
return;
}
void pop()
{
int x;
if(top==-1)
{
printf("\n stack underflow");
getch();
}
else
{
x=s[top];
printf("\ndeleted element is %d",x);
top=top-1;
}
}
void display()
{
int i;
if(top==-1)
{
printf("\nstack underflow");
getch();
}
else
{
printf("\n stack elements are\n");
for(i=top;i!=-1;i--)
{
printf("\n%d",s[i]);
}
}
}
void main()
{
int x,ch;
top=-1;
clrscr();
for(;;)
{
printf("\n\n1.push\n2.pop\n3.display\n4.exit\n");
printf("\nenter your choice \t");
scanf("%d",&ch);
switch(ch)
{
case 1:printf("\nenter the element to be inserted\t");
scanf("%d",&item);
push();
break;
case 2:pop();
break;
case 3:display();
break;
case 4:printf("\n thank you");
getch();
exit(0);
}
}
}
pg10:-stacks using linked list
#include <stdio.h>
#include <stdlib.h>
struct node
{
int info;
struct node *ptr;
}*top,*top1,*temp;
int topelement();
void push(int data);
void pop();
void display();
int count = 0;
void main()
{
int no, ch, e;
printf("\n 1 - Push");
printf("\n 2 - Pop");
printf("\n 3 - Dipslay");
printf("\n 4 - Exit");
while (1)
{
printf("\n Enter choice : ");
scanf("%d", &ch);
switch (ch)
{
case 1:
printf("Enter data : ");
scanf("%d", &no);
push(no);
break;
case 2:
pop();
break;
case 3:
display();
break;
case 4:
exit(0);
default :
printf(" Wrong choice, Please enter correct choice ");
break;
}
}
}
/* Push data into stack */
void push(int data)
{
if (top == NULL)
{
top =(struct node *)malloc(1*sizeof(struct node));
top->ptr = NULL;
top->info = data;
}
else
{
temp =(struct node *)malloc(1*sizeof(struct node));
temp->ptr = top;
temp->info = data;
top = temp;
}
count++;
}
/* Display stack elements */
void display()
{
top1 = top;
if (top1 == NULL)
{
printf("Stack is empty");
return;
}
while (top1 != NULL)
{
printf("%d ", top1->info);
top1 = top1->ptr;
}
}
/* Pop Operation on stack */
void pop()
{
top1 = top;
if (top1 == NULL)
{
printf("\n Error : Trying to pop from empty stack");
return;
}
else
top1 = top1->ptr;
printf("\n Popped value : %d", top->info);
free(top);
top = top1;
count--;
}
pg12:-queues using arrays
#include <stdio.h>
#define MAX 50
void insert();
void delete();
void display();
int queue_array[MAX];
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");
} /* End of switch */
} /* End of while */
} /* End of main() */
void insert()
{
int add_item;
if (rear == MAX - 1)
printf("Queue Overflow \n");
else
{
if (front == - 1)
/*If queue is initially empty */
front = 0;
printf("Inset the element in queue : ");
scanf("%d", &add_item);
rear = rear + 1;
queue_array[rear] = add_item;
}
} /* End of insert() */
void delete()
{
if (front == - 1 || front > rear)
{
printf("Queue Underflow \n");
return ;
}
else
{
printf("Element deleted from queue is : %d\n", queue_array[front]);
front = front + 1;
}
} /* End of delete() */
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_array[i]);
printf("\n");
}
} /* End of display() */
pg13:-queues using linked list
#include <stdio.h>
#include <stdlib.h>
struct node
{
int info;
struct node *ptr;
}*front,*rear,*temp,*front1;
void insertion(int data);
void Deletion();
void display();
int count = 0;
void main()
{
int no, ch, e;
printf("\n 1 - insertion");
printf("\n 2 - Deletion");
printf("\n 3 - Display");
printf("\n 4 - Exit");
while (1)
{
printf("\n Enter choice : ");
scanf("%d", &ch);
switch (ch)
{
case 1:
printf("Enter data : ");
scanf("%d", &no);
insertion(no);
break;
case 2:
Deletion();
break;
case 3:
display();
break;
case 4:
exit(0);
}
}
}
/* Enqueing the queue */
void insertion(int data)
{
if (rear == NULL)
{
rear = (struct node *)malloc(1*sizeof(struct node));
rear->ptr = NULL;
rear->info = data;
front = rear;
}
else
{
temp=(struct node *)malloc(1*sizeof(struct node));
rear->ptr = temp;
temp->info = data;
temp->ptr = NULL;
rear = temp;
}
count++;
}
/* Dequeing the queue */
void Deletion()
{
front1 = front;
if (front1 == NULL)
{
printf("\n Error: Trying to display elements from empty queue");
return;
}
else
if (front1->ptr != NULL)
{
front1 = front1->ptr;
printf("\n Dequed value : %d", front->info);
free(front);
front = front1;
}
else
{
printf("\n Dequed value : %d", front->info);
free(front);
front = NULL;
rear = NULL;
}
count--;
}
/* Displaying the queue elements */
void display()
{
front1 = front;
if ((front1 == NULL) && (rear == NULL))
{
printf("Queue is empty");
return;
}
while (front1 != rear)
{
printf("%d ", front1->info);
front1 = front1->ptr;
}
if (front1 == rear)
printf("%d", front1->info);
}
pg14:-circular queue using array
#include <stdio.h>
#define size 5
void insertion(int[], int);
void Deletion(int[]);
void display(int[]);
int front = - 1;
int rear = - 1;
int main()
{
int n, ch;
int queue[size];
do
{
printf("\n\n Circular Queue:\n1. Insert \n2. Delete\n3. Display\n4. Exit");
printf("\nEnter Choice 0-4? : ");
scanf("%d", &ch);
switch (ch)
{
case 1:
printf("\nEnter number: ");
scanf("%d", &n);
insertion(queue, n);
break;
case 2:
Deletion(queue);
break;
case 3:
display(queue);
break;
case 4:
exit(0);
}
}while (ch != 0);
}
void insertion(int queue[], int item)
{
if ((front == 0 && rear == size - 1) || (front == rear + 1))
{
printf("queue is full");
return;
}
else if (rear == - 1)
{
rear++;
front++;
}
else if (rear == size - 1 && front > 0)
{
rear = 0;
}
else
{
rear++;
}
queue[rear] = item;
}
void display(int queue[])
{
int i;
printf("\n");
if (front > rear)
{
for (i = front; i < size; i++)
{
printf("%d ", queue[i]);
}
for (i = 0; i <= rear; i++)
printf("%d ", queue[i]);
}
else
{
for (i = front; i <= rear; i++)
printf("%d ", queue[i]);
}
}
void Deletion(int queue[])
{
if (front == - 1)
{
printf("Queue is empty ");
}
else if (front == rear)
{
printf("\n %d deleted", queue[front]);
front = - 1;
rear = - 1;
}
else
{
printf("\n %d deleted", queue[front]);
front++;
}
}
pg15:-binary search tree and traversal
#include <stdio.h>
#include <stdlib.h>
struct btnode
{
int value;
struct btnode *l;
struct btnode *r;
}*root = NULL, *temp = NULL, *t2, *t1;
void insert();
void inorder(struct btnode *t);
void create();
void search(struct btnode *t);
void preorder(struct btnode *t);
void postorder(struct btnode *t);
int smallest(struct btnode *t);
int largest(struct btnode *t);
int flag = 1;
void main()
{
int ch;
printf("\nOPERATIONS ---");
printf("\n1 - Insert an element into tree\n");
printf("2 - Inorder Traversal\n");
printf("3 - Preorder Traversal\n");
printf("4 - Postorder Traversal\n");
printf("5 - Exit\n");
while(1)
{
printf("\nEnter your choice : ");
scanf("%d", &ch);
switch (ch)
{
case 1:
insert();
break;
case 2:
inorder(root);
break;
case 3:
preorder(root);
break;
case 4:
postorder(root);
break;
case 5:
exit(0);
default :
printf("Wrong choice, Please enter correct choice ");
break;
}
}
}
/* To insert a node in the tree */
void insert()
{
create();
if (root == NULL)
root = temp;
else
search(root);
}
/* To create a node */
void create()
{
int data;
printf("Enter data of node to be inserted : ");
scanf("%d", &data);
temp = (struct btnode *)malloc(1*sizeof(struct btnode));
temp->value = data;
temp->l = temp->r = NULL;
}
/* Function to search the appropriate position to insert the new node */
void search(struct btnode *t)
{
if ((temp->value > t->value) && (t->r != NULL)) /* value more than root node value insert at right */
search(t->r);
else if ((temp->value > t->value) && (t->r == NULL))
t->r = temp;
else if ((temp->value < t->value) && (t->l != NULL)) /* value less than root node value insert at left */
search(t->l);
else if ((temp->value < t->value) && (t->l == NULL))
t->l = temp;
}
/* recursive function to perform inorder traversal of tree */
void inorder(struct btnode *t)
{
if (root == NULL)
{
printf("No elements in a tree to display");
return;
}
if (t->l != NULL)
inorder(t->l);
printf("%d -> ", t->value);
if (t->r != NULL)
inorder(t->r);
}
/* To find the preorder traversal */
void preorder(struct btnode *t)
{
if (root == NULL)
{
printf("No elements in a tree to display");
return;
}
printf("%d -> ", t->value);
if (t->l != NULL)
preorder(t->l);
if (t->r != NULL)
preorder(t->r);
}
/* To find the postorder traversal */
void postorder(struct btnode *t)
{
if (root == NULL)
{
printf("No elements in a tree to display ");
return;
}
if (t->l != NULL)
postorder(t->l);
if (t->r != NULL)
postorder(t->r);
printf("%d -> ", t->value);
}