Professional Documents
Culture Documents
1. Odd or Even
#include<stdio.h>
#include<conio.h>
void main()
{
int num;
clrscr();
printf(“\n Enter the value of num”);
scanf(“%d”,&num);
if(num%2==0)
printf("%d is Even number");
else
printf("%d is Odd number");
getch();
}
1
Appendix E – Lab Programs
2
Appendix E – Lab Programs
5. File Handling
#include <stdio.h>
void main()
{
FILE *fopen(), *fp;
int c ;
fp = fopen( “prog.c”, “r” );
c = getc( fp ) ;
while ( c != EOF )
{
putchar( c );
c = getc ( fp );
}
fclose( fp );
}
3
Appendix E – Lab Programs
4
Appendix E – Lab Programs
scanf("%d",&temp->no);
printf("\n Enter the position:");
scanf("%d",&pos);
if(pos==1)
{
temp->next=head->next;
head->next=temp;
}
else
{
q=head->next;
for(i=1;i<pos-1;i++)
{
q=q->next;
}
temp->next=q->next;
q->next=temp;
}
}
void delete()
{
if(head->next==NULL)
printf("\nlist is empty");
else
{
printf("\n Enter the position u want to delete:");
scanf("%d",&pos1);
q=head->next;
for(i=1;i<pos1-1;i++)
q=q->next;
q->next=q->next->next;
}
}
void count()
{
if(head->next==NULL)
printf("\nlist is empty");
else
{
5
Appendix E – Lab Programs
q=head->next;
while(q!=NULL)
{
q=q->next;
c++;
}
printf("\n The no.of elements are: %d",c);
getch();
}
}
void display()
{
if(head->next==NULL)
printf("\nlist is empty");
else
{
q=head->next;
printf("\n The elements are:\n\t");
while(q!=NULL)
{
printf("%d->",q->no);
q=q->next;
}
getch();
}
}
# include<stdio.h>
# include<conio.h>
# include<stdlib.h>
int pos,pos1,i;
struct list
{
int no;
struct list *prev,*next;
}
6
Appendix E – Lab Programs
*p,*q,*temp,*head;
void insert();
void delete();
void display();
void exit();
void main()
{
int choice;
clrscr();
head=(struct list*)malloc(sizeof(struct list));
head->next=NULL;
while(1)
{
printf("\n 1.insert\n2.delete\n3.list all\n 4.exit");
printf("\nEnter the choice:");
scanf("%d",&choice);
switch(choice)
{
case 1:
insert();
break;
case 2:
delete();
break;
case 3:
display();
break;
default:
exit(0);
break;
}
}
}
void insert()
{
int ch;
temp = (struct list*) malloc(sizeof(struct list));
printf("\n enter the element:");
7
Appendix E – Lab Programs
scanf("%d",&temp->no);
if(head->next==NULL)
{
head->next=temp;
temp->prev=head;
temp->next=NULL;
}
else
{
printf("\n Enter the position u want to insert:");
printf("\n1.First\n2.Middle\n3.End\n--->");
scanf("%d",&ch);
switch(ch)
{
case 1:
q=head->next;
temp->next=q;
q->prev=temp;
head->next=temp;
temp->prev=head;
break;
case 2:
printf("\n Enter the position:");
scanf("%d",&pos);
q=head->next;
for(i=1;i<pos-1;i++)
{
q=q->next;
}
p=q->next;
temp->next=p;
p->prev=temp;
q->next=temp;
temp->prev=q;
break;
case 3:
q=head->next;
while(q->next!=NULL)
q=q->next;
8
Appendix E – Lab Programs
q->next=temp;
temp->prev=q;
temp->next=NULL;
break;
}
}
}
void delete()
{
if(head->next==NULL)
printf("\nlist is empty");
else
{
printf("\n Enter the position u want to delete:");
scanf("%d",&pos1);
q=head->next;
if(pos1==1)
{
head->next=q->next;
q->prev=head;
}
else
{
for(i=1;i<pos1-1;i++)
q=q->next;
p=q->next;
q->next=p->next;
p->prev=q;
}
}
}
void display()
{
if(head->next==NULL)
printf("\nlist is empty");
else
{
q=head->next;
printf("\n the elements present in the list are:\n");
9
Appendix E – Lab Programs
while(q!=NULL)
{
printf("%d->",q->no);
q=q->next;
}
getch();
}
}
10
Appendix E – Lab Programs
case 2:
pop();
break;
case 3:
display();
break;
default:
exit(0);
break;
}
}
}
void push()
{
int ch;
temp = (struct stack*) malloc(sizeof(struct stack));
printf("\n enter the element:");
scanf("%d",&temp->no);
if(head->next==NULL)
{
head->next=temp;
temp->next=NULL;
}
else
{
temp->next=head->next;
head->next=temp;
}
}
void pop()
{
if(head->next==NULL)
printf("\nstack is empty");
else
{
q=head->next;
head->next=q->next;
}
}
11
Appendix E – Lab Programs
void display()
{
if(head->next==NULL)
printf("\nstack is empty");
else
{
q=head->next;
printf("\n the elements present in the stack are:\n");
while(q!=NULL)
{
printf("%d->",q->no);
q=q->next;
}
getch();
}
}
12
Appendix E – Lab Programs
{
printf("\n 1.Enqueue\n2.Dequeue\n3.list all\n 4.exit");
printf("\nEnter the choice:");
scanf("%d",&choice);
switch(choice)
{
case 1:
enqueue();
break;
case 2:
dequeue();
break;
case 3:
display();
break;
default:
exit(0);
break;
}
}
}
void enqueue()
{
int ch;
temp = (struct queue*) malloc(sizeof(struct queue));
printf("\n enter the element:");
scanf("%d",&temp->no);
if(head->next==NULL)
{
head->next=temp;
temp->next=NULL;
}
else
{
q=head->next;
while(q->next!=NULL)
q=q->next;
q->next=temp;
temp->next=NULL;
13
Appendix E – Lab Programs
}
}
void dequeue()
{
if(head->next==NULL)
printf("\nqueue is empty");
else
{
q=head->next;
head->next=q->next;
}
}
void display()
{
if(head->next==NULL)
printf("\nqueue is empty");
else
{
q=head->next;
printf("\n the elements present in the queue are:\n");
while(q!=NULL)
{
printf("%d->",q->no);
q=q->next;
}
getch();
}
}
14
Appendix E – Lab Programs
char topelement();
void push(char ch);
void main()
{
char ele,elem,st[2];
int i,prep,pre,popped,j=0;
clrscr();
strcpy(postfix," ");
printf("\n\n Enter the infix\n");
gets(infix);
for(i=0;infix[i]!='\0';i++)
{
if(infix[i]!='('&&infix[i]!=')'&&infix[i]!='^'&&infix[i]!='*'&&infix[i]!='/'&&infix[i]!='+'&&infi
x[i]!='-')
{
postfix[j++]=infix[i];
}
else if(infix[i]=='(')
{
elem=infix[i];
push(elem);
}
else if(infix[i]==')')
{
while((popped=pop())!='(')
{
postfix[j++]=popped;
}
}
else
{
elem=infix[i];
pre=precedence(elem);
ele=topelement();
prep=precedence(ele);
if(pre > prep)
push(elem);
else
15
Appendix E – Lab Programs
{
while(prep >= pre)
{
if(ele=='#')
break;
popped=pop();
ele=topelement();
postfix[j++]=popped;
prep=precedence(ele);
}
push(elem);
}
}
}
while((popped=pop())!='#')
{
postfix[j++]=popped;
}
postfix[j]='\0';
printf("\n post fix :%s",postfix);
getch();
}
int precedence(char ch)
{
switch(ch)
{
case '^' : return 5;
case '/' : return 4;
case '*' : return 4;
case '+' : return 3;
case '-' : return 3;
default : return 0;
}
}
char pop()
{
char ret;
if(top!=-1)
{
16
Appendix E – Lab Programs
ret =stack[top];
top--;
return ret;
}
else
return '#';
}
char topelement()
{
char ch;
if(top!=-1)
ch=stack[top];
else
ch='#';
return ch;
}
void push(char ch)
{
if(top!=size-1)
{
top++;
stack[top]= ch;
}
}
17
Appendix E – Lab Programs
enum Type{operand,oprtor};
void push(double val)
{
if(st.top+1>=size)
printf("Error:Stack is full\n");
st.top++;
st.s[st.top]=val;
}
double pop()
{
double val;
if(st.top==-1)
printf("Error :stack is empty\n");
val=st.s[st.top];
st.top--;
return(val);
}
char gettokn(char exp[])
{
static int i=0;
char ch;
ch=exp[i];
i++;
return ch;
}
enum Type Gettype(char ch)
{
ch=toupper (ch);
if(ch>='0'&&ch<='9')
return operand;
if(ch=='+'||ch=='-'||ch=='*'||ch=='/'||ch=='^')
return oprtor;
printf("Invalid operator\n");
}
double post(char exp[])
{
char ch,type;
double result,val,op1,op2;
st.top=0;
18
Appendix E – Lab Programs
ch=gettokn(exp);
while(ch!='$')
{
type=Gettype(ch);
if(type==operand)
{
val=ch-48;
push(val);
}
else
if(type==oprtor)
{
op2=pop();
op1=pop();
switch(ch)
{
case '+': result=op1+op2;
break;
case'-': result =op1-op2;
break;
case'*': result =op1*op2;
break;
case'/': result =op1/op2;
break;
case'^': result =pow (op1,op2);
break;
}
push(result);
}
ch=gettokn(exp);
}
result=pop();
return(result);
}
void main()
{
char exp[size];
int len;
double result;
19
Appendix E – Lab Programs
clrscr();
printf("enter the postfix expression and put $ at end\n");
scanf("%s",exp);
exp[len]=' ';
result=post(exp);
printf("the value of the expression is %f\n",result);
getch();
exit(0);
}
20
Appendix E – Lab Programs
j=last;
while(i<j)
{
while(x[i]<=pivot && i<last)
i++;
while(x[j]>=pivot && j>first)
j--;
if(i<j)
{
temp=x[i];
x[i]=x[j];
x[j]=temp;
}
}
temp=x[first];
x[first]=x[j];
x[j]=temp;
sort(first,j-1);
sort(j+1,last);
getch();
}
}
21
Appendix E – Lab Programs
22
Appendix E – Lab Programs
{
x[i]=x[j];
i=j;
j=2*i;
if((j+1)<k)
if(x[j+1]>x[j])
j++;
else
if(j>size)
j=size;
x[i]=value;
}
}
}
23
Appendix E – Lab Programs
{
temp[k] = a[i];
i++;
}
else if (a[i] > a[j])
{
temp[k] = a[j];
j++;
}
else
{
temp[k++] = a[i];
temp[k] = a[j];
i++;
j++;
}
k++;
}
while (i <= mid)
{
temp[k] = a[i];
i++;
k++;
}
while (j <= last)
{
temp[k] = a[j];
j++;
k++;
}
for (k = first;k <= last;k++)
a[k] = temp[k];
}
void main()
{
int a[50],i,j,n;
printf("\nEnter number of elements: ");
scanf("%d",&n);
24
Appendix E – Lab Programs
25
Appendix E – Lab Programs
26
Appendix E – Lab Programs
{
m=(l+h)/2;
if(s<a[m])
h=m-1;
else if(s>a[m])
l=m+1;
else if(s==a[m])
{
printf("SEARCH SUCCESSFUL");
printf("location of %d is %d",s,m+1);
}
printf("\n SEARCH UNSUCCESSFUL");
}
getch();
}
27