Documente Academic
Documente Profesional
Documente Cultură
# include<stdio.h>
# include<conio.h>
void main()
{
int a[5], i, j, temp;
clrscr();
for(i=0;i<5;i++)
{
for(j=i;j<4;j++)
{
if(a[i]>a[j+1])
{
temp=a[i];
a[i]=a[j+1];
a[j+1]=temp;
}
}
}
for(i=0;i<5;i++)
{
printf("\n%d",a[i]);
}
getch();
}
Output
# include<stdio.h>
# include<conio.h>
void main()
{
int a[5],sno,i,f=0;
clrscr();
for(i=0;i<5;i++)
{
if(sno==a[i])
{
printf("Number is found at %d position",i);
f=1;
}
}
if(f==0)
{
printf("Number is not found");
}
getch();
}
Output
# include<stdio.h>
# include<conio.h>
void main()
{
int a[5]={1,4,6,8,9};
int item, loc, beg=1,end=5,mid;
mid=(beg+end)/2;
if(a[mid]==item)
{
loc=mid;
printf("%d is found at position %d",item,loc);
}
else
{
loc=NULL;
printf("Element is not found");
}
getch();
}
Output
# include<stdio.h>
# include<conio.h>
int i, len;
void main()
{
int a[100], pos;
void del(int a[], int, int);
clrscr();
getch();
}
for(j=pos;j<=len;j++)
{
a[j]=a[j+1];
}
len=len-1;
printf("%d is deleted\n",item);
# include<stdio.h>
# include<conio.h>
void main()
{
int a[5], i, j, temp;
clrscr();
for(i=0;i<5;i++)
{
for(j=0;j<4;j++)
{
if(a[j]>a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
getch();
}
Output
#include<stdio.h>
#include<conio.h>
for(i=1;i<n;i++)
{
temp=a[i];
j=i-1;
a[j+1]=temp;
}
}
void main()
{
int i,j,a[10],n;
clrscr();
insertionsort(a,n);
getch();
}
Output
# include<stdio.h>
# include<conio.h>
int fact(int n)
{
int x,y;
if(n==1)
return(n);
x=n-1;
y=fact(x);
return(n*y);
}
void main()
{
int n;
int z=fact(n);
printf("Factorial of a %d is %d",n,z);
getch();
}
Output
Enter a Number: 4
Factorial of a 4 is 24
Tower of Hanoi
# include<stdio.h>
# include<conio.h>
void main()
{
void toh(int,char,char,char);
int n;
toh(n,'A','B','C');
getch();
}
# include<stdio.h>
# include<conio.h>
# include<stdlib.h>
# define max 10
int que[max];
int front=0,rear=-1;
void insert()
{
int n;
if(rear>max-1)
{
printf("que is full");
}
else
{
printf("\nEnter number to be inserted ");
scanf("%d",&n);
rear++;
que[rear]=n;
}
}
void delet()
{
int temp;
if(front>rear)
{
printf("\nque is empty");
}
else
{
temp=que[front];
front++;
printf("\n%d is deleted",temp);
}
}
void display()
{
int i;
for(i=rear;i>=front;i--)
{
printf("\n%d",que[i]);
}
}
void main()
{
int ch;
while(1)
{
printf("\n\nEnter which operation do you want to perform \n1 To insert \n2
To delete \n3 To display \n4 To exit\n\n");
scanf("%d",&ch);
switch(ch)
{
case 1:
insert();
break;
case 2:
delet();
break;
case 3:
display();
break;
case 4:
exit(0);
default:
printf("\n\nInvalid choice");
break;
}
}
}
Output
5 is deleted
Invalid choice
# include<stdio.h>
# include<conio.h>
# include<stdlib.h>
# define max 10
int stack[max];
int top=-1;
void push()
{
int n;
if(top>max-1)
{
printf("\nstack overflow occurs");
}
else
{
printf("\nEnter number to be inserted");
scanf("%d",&n);
top++;
stack[top]=n;
}
}
void pop()
{
int temp;
if(top==-1)
{
printf("\nstack is empty");
}
else
{
temp=stack[top];
printf("\n%d is deleted",temp);
top--;
}
}
void display()
{
int i;
for(i=top;i>=0;i--)
{
printf("\n\n%d",stack[i]);
}
}
void main()
{
int stack[max],top=-1,ch;
while(1)
{
printf("\n\nEnter which operation do you want to perform\n1 To push\n2 To
pop\n3 To display\n4 To exit\n\n");
scanf("%d",&ch);
switch(ch)
{
case 1:
push();
break;
case 2:
pop();
break;
case 3:
display();
break;
case 4:
exit(0);
default:
printf("\n\nInvalid choice");
break;
}
}
}
Output
5 is deleted
# include<stdio.h>
# include<conio.h>
# include<stdlib.h>
struct clist
{
int data;
void insertatbeg()
{
struct clist *p;
int num;
p->data=num;
p->next=end->next;
end->next=p;
}
void insertatend()
{
struct clist *p;
int num;
p->data=num;
p->next=end->next;
end->next=p;
end=p;
}
void insertatpos()
{
int num,i=1,pos;
struct clist *p,*temp;
temp=end->next;
while(i<pos-1)
{
temp=temp->next;
i++;
}
p->data=num;
p->next=temp->next;
temp->next=p;
}
void deletatbeg()
{
struct clist *temp,*prv;
temp=end->next;
prv=temp->next;
end->next=prv;
temp->next=NULL;
printf("\n\n%d is deleted",temp->data);
free(temp);
}
void deletatend()
{
struct clist *temp;
temp=end->next;
while(temp->next!=end)
{
temp=temp->next;
}
temp->next=end->next;
printf("\n\n%d is deleted",end->data);
end->next=NULL;
free(end);
end=temp;
}
void deletatpos()
{
struct clist *temp,*prv;
int pos,i=1,num;
printf("\n\nEnter position");
scanf("%d",&pos);
temp=end->next;
while(i<pos-1)
{
temp=temp->next;
i++;
}
prv=temp->next;
temp->next=prv->next;
printf("\n\n%d is deleted",prv->data);
prv->next=NULL;
free(prv);
}
void display()
{
struct clist *temp;
temp=end->next;
if(end!=NULL)
{
while(temp->next!=end)
{
printf("\n\n%d",temp->data);
temp=temp->next;
}
printf("\n%d",temp->data);
}
else
{
printf("\n\nList is empty");
}
}
void main()
{
int ch;
struct clist *p;
int num;
printf("\n\nEnter number");
scanf("%d",&num);
p->data=num;
p->next=p;
end=p;
while(1)
{
printf("\n\nEnter which operation do you want to perform\n1 To insert at
begining\n2 To insert at end\n3 To insert at given position\n4 To delete at
begining\n5 To delete at end\n6 To delete at given position\n7 To
display\n8 To exit\n\n");
scanf("%d",&ch);
switch(ch)
{
case 1:
insertatbeg();
break;
case 2:
insertatend();
break;
case 3:
insertatpos();
break;
case 4:
deletatbeg();
break;
case 5:
deletatend();
break;
case 6:
deletatpos();
break;
case 7:
display();
break;
case 8:
exit(0);
default:
printf("\n\nInvalid choice");
break;
}
}
}
Output
Enter Number: 2
Enter Number:
struct list
{
int data;
struct list *next;
};
void insertatbeg()
{
struct list *p;
int num;
printf("enter number");
scanf("%d",&num);
p->data=num;
p->next=start;
start=p;
}
void insertatend()
{
struct list *p,*temp;
int num;
printf("enter number");
scanf("%d",&num);
temp=start;
while(temp->next!=NULL)
{
temp=temp->next;
}
p->data=num;
temp->next=p;
p->next=NULL;
}
void insertatpos()
{
int num,i=1,pos;
struct list *p,*temp;
printf("Enter number");
scanf("%d",&num);
temp=start;
while(i<pos-1)
{
temp=temp->next;
i++;
}
p->data=num;
p->next=temp->next;
temp->next=p;
}
void deletatbeg()
{
struct list *temp;
temp=start;
start=start->next;
temp->next=NULL;
printf("%d is deleted",temp->data);
free(temp);
}
void deletatend()
{
struct list *temp,*prv;
temp=start;
while(temp->next!=NULL)
{
prv=temp;
temp=temp->next;
}
prv->next=NULL;
printf("%d is deleted",temp->data);
free(temp);
}
void deletatpos()
{
struct list *temp,*prv;
int pos,i=1;
printf("Enter position");
scanf("%d",&pos);
temp=start;
while(i<pos-1)
{
temp=temp->next;
i++;
}
prv=temp->next;
temp->next=prv->next;
printf("%d is deleted",prv->data);
free(prv);
}
void display()
{
struct list *temp;
temp=start;
if(start!=NULL)
{
while(temp->next!=NULL)
{
printf("\n%d",temp->data);
temp=temp->next;
}
printf("\n%d",temp->data);
}
else
{
printf("List is empty");
}
}
void main()
{
int ch;
struct list *p;
int num;
p=(struct list *)malloc(sizeof(struct list));
printf("Enter number");
scanf("%d",&num);
p->data=num;
p->next=NULL;
start=p;
while(1)
{
printf("\nEnter which operation do you want to perform\n1 To insert at
begining\n2 To insert at end\n3 To insert at given position\n4 To delete at
begining\n5 To delete at end\n6 To delete at given position\n7 To
display\n8 To exit");
scanf("%d",&ch);
switch(ch)
{
case 1:
insertatbeg();
break;
case 2:
insertatend();
break;
case 3:
insertatpos();
break;
case 4:
deletatbeg();
break;
case 5:
deletatend();
break;
case 6:
deletatpos();
break;
case 7:
display();
break;
case 8:
exit(0);
default:
printf("\nInvalid choice");
break;
}
}
}
Output
Enter Number: 2
Enter Number:
Insertion in array
# include<stdio.h>
# include<conio.h>
void main()
{
int a[100]: void insert(int a[],int,int,int);
clrscr();
--pos;
insert(a,len,pos,num);
}
a[pos]=num;
len++;
Output
New array:
2
4
5
7
9
11
12
Quick Sort
#include<stdio.h>
#include<conio.h>
void quicksort(int a[],int l,int h)
{
int temp,low,high,key;
low=l;
high=h;
key=a[(low+high)/2];
while(low<=high)
{
while(a[low]<key)
low++;
while(a[high]>key)
high--;
if(low<=high)
{
temp=a[low];
a[low]=a[high];
a[high]=temp;
low++;
high--;
}
}
if(l<high)
quicksort(a,l,high);
if(low<h)
quicksort(a,low,h);
}
void main()
{
int i,j,a[10],n,l,h;
clrscr();
quicksort(a,l,h);
getch();
}
Output