Sub: JNTU Hyderabad – GHMC Elections - Postponement of B.Tech & B.Pharmacy University examinations and Mid Examinations-Reg.
*****
In view of GHMC Elections scheduled on 23rd November 2009, B.Tech and B.Pharmacy II and IV year I-Semester University Examinations scheduled On 23rd November 2009 are Postponed to 2nd December-2009, as per the same time schedule,III and I year B.Tech I semester University exams scheduled on 2nd December are postponed to 19th December 2009 as per the same time schedule.
Further, I year B.Tech and B.pharmacy I mid Examinations scheduled on 23rd November are postponed to 27th November as per the same time schedule.
Saturday, November 7, 2009
Thursday, May 7, 2009
ITWS RECORD
USERNAME :ece.b08@gmail.com
PASSWORD:electron
u can downlaod itws record files 4rm dis id...........
PASSWORD:electron
u can downlaod itws record files 4rm dis id...........
Saturday, April 25, 2009
WRITE.....WrTiTe....wRiTE.......
Wednesday, April 22, 2009
SOMETHING ELSE
HEY EVERY ONE THAT BELONG TO ECE B,
LET US HAVE AN INTRESTING DISCUSSION ON THE TOPIC BELOW !
DID YOU EXPECT THE SAME ,WHAT YOU ARE DOING NOW AT ENGINEERING LEVEL?
IF NOT, WHAT IS THAT YOU EXPECTED .........
LET US HAVE AN INTRESTING DISCUSSION ON THE TOPIC BELOW !
DID YOU EXPECT THE SAME ,WHAT YOU ARE DOING NOW AT ENGINEERING LEVEL?
IF NOT, WHAT IS THAT YOU EXPECTED .........
Tuesday, April 21, 2009
mana bathuku...
thuu endii ra ee bathuku ila rasukunta kurchovala ika...... asalu endo engineerng vachinapati nunchi inter schoolng kantey ikkade ekuva rayalsi vastundii n malli thitlu okati.... endo emo.....
Monday, April 20, 2009
C RECORD
CQUEUE
#include
#include
#define max 5
int front=-1,rear=-1;
int cq[max];
void main()
{
int rpt=1;
int ch;
clrscr();
while(rpt)
{
printf("1.cqinsert\n");
printf("2.cqdelete\n");
printf("3.cqdisplay\n");
printf("4.exit\n");
printf("enter your choice\n");
scanf("%d",&ch);
switch(ch)
{
case 1:cqinsert();
break;
case 2:cqdelete();
break;
case 3:cqdisplay();
break;
case 4:return;
}
printf("Do you want to continue press 1 for yes and 0 for no\n");
scanf("%d",&rpt);
}
}
cqinsert()
{
int num;
if(front==(rear+1)%max)
{
printf("queue is full\n");
return;
}
else
{
printf("enter the element\n");
scanf("%d",&num);
rear=rear+1%max;
cq[rear]=num;
if(front==-1)
front=0;
}
}
cqdelete()
{
int num;
if(front==-1)
{
printf("queue is empty\n");
return;
}
else
{
num=cq[front];
printf("Deleted number is %d",num);
if(front==rear)
front=rear=-1;
else
front=(front+1)%max;
}
}
cqdisplay()
{
int i;
if(front==-1)
{
printf("queue is empty\n");
return;
}
else if(front {
for(i=front;i<=rear;i++)
printf("%d\n",cq[i]);
}
else
{
for(i=front;i printf("%d\n",cq[i]);
for(i=0;i<=rear;i++)
printf("%d\n",cq[i]);
}
}
INFTPOST
/*Infix To Postfix*/
#include
#include
struct stack
{
int top;
char item[30];
};
void push(struct stack *ps,char n)
{
ps->top++;
ps->item[ps->top]=n;
}
void pop(struct stack *ps)
{
ps->top--;
}
int isempty(struct stack *ps)
{
if(ps->top==-1)
{
return(1);
}
else
{
return(0);
}
}
int prcd(char stktop,char op)
{
if(((stktop=='+')||(stktop=='-'))&&((op=='*')||(op=='/')))
{
return(0);
}
if(stktop=='(')
{
return(0);
}
if((stktop!=')')&&(op=='('))
{
return(0);
}
if((stktop!='(')&&(op==')'))
{
return(1);
}
}
void main()
{
struct stack s;
char input[30],output[30];
char optop,ans;
int i=0,j=0,k=0;
s.top=-1;
clrscr();
printf("enter an expression in infix form:\n");
scanf("%s",input);
while(input[i]!='\0')
{
if(isalpha(input[i]))
{
output[j]=input[i];
j++;
}
else
{
while(!isempty(&s)&&(prcd(s.item[s.top],input[i])))
{
optop=s.item[s.top];
pop(&s);
output[j]=optop;
j++;
}
if(isempty(&s)||input[i]!=')')
{
push(&s,input[i]);
}
else
{
pop(&s);
}
}
i++;
}
while(!isempty(&s))
{
optop=s.item[s.top];
pop(&s);
output[j]=optop;
j++;
}
printf("\npostfix form:\t");
output[j]='\0';
while(output[k]!='\0')
{
printf("%c",output[k]);
k++;
}
scanf("%c",&ans);
getch();
}
MERGSOR
#include
#include
void main()
{
int m,n,p,sum=0;
int a[15],b[15],c[45]={0 };
//c[15]={0};
clrscr();
//printf("enter x,y values");
//scanf("%d",&x,&y);
printf("\n enter elements in first list\n");
for(m=0;m<5;m++)
{
scanf("%d",&a[m]);
if(a[m]==0)
m--;
sum=sum+abs(a[m]);
}
printf("\n enter elements in first list\n");
for(n=0;n<5;n++)
{
scanf("%d",&b[n]);
if(a[n]==0)
m--;
sum=sum+abs(b[n]);
}
p=n=m=0;
m=m-sum;
while(m {
for(n=0;n<5;n++)
{
if(m==a[n] || m==b[n])
c[p++]=m;
if(m==a[n] && m==b[n])
c[p++]=m;
}
m++;
}
puts("MERGED SORTED LIST :");
for(n=0;n<10;++n)
printf("\t%d ",c[n]);
}
QLINK
/* Write C programs that implement Queue (its operations) using ii) Pointers */
#define true 1
#define false 0
#include
#include
#include
struct q_point
{
int ele;
struct q_point* n;
};
struct q_point *f_ptr = NULL;
int e_que(void);
void add_ele(int);
int rem_ele(void);
void show_ele();
/*main function*/
void main()
{
int ele,choice,j;
while(1)
{
clrscr();
printf("\n\n****IMPLEMENTATION OF QUEUE USING POINTERS****\n");
printf("==============================================");
printf("\n\t\t MENU\n");
printf("==============================================");
printf("\n\t[1] To insert an element");
printf("\n\t[2] To remove an element");
printf("\n\t[3] To display all the elements");
printf("\n\t[4] Exit");
printf("\n\n\tEnter your choice:");
scanf("%d", &choice);
switch(choice)
{
case 1:
{
printf("\n\tElement to be inserted:");
scanf("%d",&ele);
add_ele(ele);
getch();
break;
}
case 2:
{
if(!e_que())
{
j=rem_ele();
printf("\n\t%d is removed from the queue",j);
getch();
}
else
{
printf("\n\tQueue is Empty.");
getch();
}
break;
}
case 3:
show_ele();
getch();
break;
case 4:
exit(1);
break;
default:
printf("\n\tInvalid choice.");
getch();
break;
}
}
}
/* Function to check if the queue is empty*/
int e_que(void)
{
if(f_ptr==NULL)
return true;
return false;
}
/* Function to add an element to the queue*/
void add_ele(int ele)
{
struct q_point *queue = (struct q_point*)malloc(sizeof(struct q_point));
queue->ele = ele;
queue->n = NULL;
if(f_ptr==NULL)
f_ptr = queue;
else
{
struct q_point* ptr;
ptr = f_ptr;
for(ptr=f_ptr ;ptr->n!=NULL; ptr=ptr->n);
ptr->n = queue;
}
}
/* Function to remove an element from the queue*/
int rem_ele()
{
struct q_point* queue=NULL;
if(e_que()==false)
{
int j = f_ptr->ele;
queue=f_ptr;
f_ptr = f_ptr->n;
free (queue);
return j;
}
else
{
printf("\n\tQueue is empty.");
return -9999;
}
}
/* Function to display the queue*/
void show_ele()
{
struct q_point *ptr=NULL;
ptr=f_ptr;
if(e_que())
{
printf("\n\tQUEUE is Empty.");
return;
}
else
{
printf("\n\tElements present in Queue are:\n\t");
while(ptr!=NULL)
{
printf("%d\t",ptr->ele);
ptr=ptr->n;
}
}
}
SL
#include
#include
#include
#include
struct node
{
int data;
struct node *ptr;
};
typedef struct node NODE;
NODE *head=NULL;
NODE *nn,*temp,*temp1,*temp2;
main()
{
int ch,r=1;
clrscr();
while(r)
{
printf("enter the choice\n");
printf("1.entering from the front\n");
printf("2.entering from the end\n");
printf("3.entering from the middle\n");
printf("4.deleting from the front\n");
printf("5.deleting from the end\n");
printf("6.display\n");
printf("7.deleting from the middle\n");
printf("8.exit\n");
scanf("%d",&ch);
switch(ch)
{
case 1:enterf();
break;
case 2: entere();
break;
case 3:enterm();
break;
case 4:delf();
break;
case 5:dele();
break;
case 6:display();
break;
case 7:delm();
break;
case 8:return;
}
printf("do you want to continue press 1 for yes and 0 for no\n");
scanf("%d",&r);
}
}
enterf()
{
int item;
nn=(NODE*)malloc(sizeof(NODE));
printf("enter the item to be inserted\n");
scanf("%d",&item);
if(head==NULL)
{
nn->data=item;
nn->ptr=NULL;
head=nn;
}
else
{
nn->data=item;
nn->ptr=head;
head=nn;
}
}
display()
{
if(head==NULL)
printf("empty list\n");
else
{
printf("elements in the list are\n");
temp=head;
while(temp!=NULL)
{
printf("%d->",temp->data);
temp=temp->ptr;
}
}
}
entere()
{
int item;
nn=(NODE*)malloc(sizeof(NODE));
printf("enter the item to be inserted\n");
scanf("%d",&item);
if(head==NULL)
{
nn->data=item;
nn->ptr=NULL;
head=nn;
}
else
{
temp=head;
while(temp->ptr!=NULL)
{
temp=temp->ptr;
}
nn->data=item;
temp->ptr=nn;
nn->ptr=NULL;
}
}
delf()
{
if(head==NULL)
printf("empty list\n");
else if(head->ptr==NULL)
{
printf("deleted element =%d",head->data);
free(head);
head=NULL;
}
else
{
temp=head;
head=head->ptr;
printf("deleted element=%d",temp->data);
free(temp);
}
}
dele()
{
if(head==NULL)
printf("empty list\n");
else if(head->ptr==NULL)
{
printf("deleted element=%d",head->data);
free(head);
head=NULL;
}
else
{
temp1=head;
temp2=NULL;
while(temp1->ptr!=NULL)
{
temp2=temp1;
temp1=temp1->ptr;
}
printf("deleted element=%d",temp1->data);
free(temp1);
temp2->ptr=NULL;
}
}
enterm()
{
int pos,item,i;
if(head==NULL)
{
nn=(NODE*)malloc(sizeof(NODE));
printf("enter the item to be inserted\n");
scanf("%d",&item);
nn->data=item;
nn->ptr=NULL;
head=nn;
}
else
{
printf("enter the position where you want to insert the element\n");
scanf("%d",&pos);
temp1=head;
temp2=NULL;
for(i=1;i {
temp2=temp1;
temp1=temp1->ptr;
}
printf("enter the item to be inserted\n");
scanf("%d",&item);
nn=(NODE*)malloc(sizeof(NODE));
nn->data=item;
temp2->ptr=nn;
nn->ptr=temp1;
}
}
delm()
{
int pos,i;
if(head==NULL)
{
printf("empty list\n");
}
else if(head->ptr==NULL)
{
printf("deleted element=%d",head->data);
free(head);
head=NULL;
}
else
{
printf("enter the position of the element to be deleted\n");
scanf("%d",&pos);
temp1=NULL;
temp2=head;
for(i=1;i {
temp1=temp2;
temp2=temp2->ptr;
}
printf("deleted element=%d\n",temp2->data);
temp1->ptr=temp2->ptr;
}
}
STACKL
/*stack using linked list*/
#include
#include
#include
struct node
{
int data;
struct node *ptr;
};
typedef struct node NODE;
NODE *top=NULL,*nn,*temp;
main()
{
int ch,rpt=1;
clrscr();
while(rpt)
{
printf("1.push\n");
printf("2.pop\n");
printf("3.display\n");
printf("4.exit\n");
printf("enter your choice\n");
scanf("%d",&ch);
switch(ch)
{
case 1:push();
break;
case 2:pop();
break;
case 3:display();
break;
case 4:return;
}
printf("\nDo you want to continue press 1 for yes and 0 for no\n");
scanf("%d",&rpt);
}
}
push()
{
int num;
printf("enter the element to be inserted\n");
scanf("%d",&num);
nn=(NODE*)malloc(sizeof(NODE));
nn->data=num;
if(top==NULL)
{
nn->ptr=NULL;
top=nn;
}
else
{
nn->ptr=top;
top=nn;
}
}
pop()
{
if(top==NULL)
printf("stack is empty\n");
else
{
temp=top;
printf("Deleted element=%d",temp->data);
top=top->ptr;
free(temp);
}
}
display()
{
if(top==NULL)
printf("empty list\n");
else
{
printf("status of the stack is\n");
temp=top;
while(temp!=NULL)
{
printf("%d\n",temp->data);
temp=temp->ptr;
}
}
}
TRTV
#include
#include
#include
#define newnode (tree_type *)malloc(sizeof(tree_type))
typedef struct tree
{
int info;
struct tree *left,*right;
}tree_type;
tree_type *insert(tree_type *t,int val)
{
if(t==NULL)
{
t=newnode;
t->info=val;
t->left=t->right=NULL;
}
else if(valinfo)
t->left=insert(t->left,val);
else if(val>t->info)
t->right=insert(t->right,val);
else
printf("\n Duplicate number");
return(t);
}
void preorder(tree_type *t)
{
if(t)
{
printf("%4d",t->info);
preorder(t->left);
preorder(t->right);
}
}
void inorder(tree_type *t)
{
if(t)
{
inorder(t->left);
printf("%4d",t->info);
inorder(t->right);
}
}
void postorder(tree_type *t)
{
if(t)
{
postorder(t->left);
postorder(t->right);
printf("%4d",t->info);
}
}
int menu()
{
int opt;
printf("\n 1.Create Tree\n 2. Insert Node\n 3. Delete Node\n");
printf("4.Search Item\n 5. Exit \nEnter Your Option");
scanf("%d",&opt);
return(opt);
}
int search(tree_type *t,int key)
{
if(t==NULL)
return NULL;
else if(keyinfo)
return(search(t->left,key));
else if(key>t->info)
return(search(t->right,key));
else
return(1);
}
tree_type *find(tree_type *t)
{
while(t->right!=NULL)
t=t->right;
return(t);
}
tree_type *Delete(tree_type *t,int val)
{
tree_type *temp,*child;
if(t==NULL)
printf("ELEMENT NOT FOUND");
else if(valinfo)
t->left=Delete(t->left,val);
else if(val > t->info)
t->right=Delete(t->right,val);
else if(t->left!=NULL && t->right!=NULL)
{
temp=find(t->left);
t->info=temp->info;
t->left=Delete(t->left,t->info);
}
else
{
temp=t;
if(t->left==NULL)
child=t->right;
if(t->right==NULL)
child=t->left;
free(temp);
return(child);
}
return(t);
}
void main()
{
tree_type *root=NULL ,*val;
int opt,num;
while(1)
{
clrscr();
printf("\n\n PREORDER :");
preorder(root);
printf("\n\n INORDER :");
inorder(root);
printf("\n\n POST ORDER :");
postorder(root);
opt=menu();
switch(opt)
{
case 1:printf("Enter value or 0 to end\n");
do
{
scanf("%d",&num);
if(num!=0)
root=insert(root,num);
}while(num!=0);
break;
case 2:printf("Enter the number :");
scanf("%d",&num);
root=insert(root,num);
break;
case 3:printf("Enter the number to br deleted:");
scanf("%d",&num);
root=Delete(root,num);
break;
case 4:printf("\n Enter Item to search:");
scanf("%d",&num);
if(search(root,num))
printf("Item %d Found\n",num);
else
printf("NOT FOUND");
getch();
break;
case 5:return;
}
}
}
CIRL
#include
#include
#include
#include
struct node
{
int data;
struct node *ptr;
};
typedef struct node NODE;
NODE *first=NULL,*last=NULL;
NODE *nn,*temp,*temp1,*temp2;
main()
{
int ch,r=1;
clrscr();
while(r)
{
printf("enter the choice\n");
printf("1.entering from the front\n");
printf("2.entering from the end\n");
printf("3.deleting from the front\n");
printf("4.deleting from the end\n");
printf("5.display\n");
printf("6.exit\n");
scanf("%d",&ch);
switch(ch)
{
case 1:enterf();
break;
case 2: entere();
break;
case 3:delf();
break;
case 4:dele();
break;
case 5:display();
break;
case 6:return;
}
printf("do you want to continue press 1 for yes and 0 for no\n");
scanf("%d",&r);
}
}
enterf()
{
int item;
nn=(NODE*)malloc(sizeof(NODE));
printf("enter the item to be inserted\n");
scanf("%d",&item);
nn->data=item;
nn->ptr=nn;
if(first==NULL)
{
first=last=nn;
}
else
{
last->ptr=nn;
nn->ptr=first;
first=nn;
}
}
display()
{
if(first==NULL)
printf("empty list\n");
else if(first->ptr==first)
printf("%d->",first->data);
else
{
temp=first;
while(temp!=last)
{
printf("%d->",temp->data);
temp=temp->ptr;
}
printf("%d->",last->data);
}
}
entere()
{
int item;
nn=(NODE*)malloc(sizeof(NODE));
printf("enter the item to be inserted\n");
scanf("%d",&item);
nn->data=item;
nn->ptr=nn;
if(first ==NULL)
{
first=last=nn;
}
else
{
last->ptr=nn;
nn->ptr=first;
last=nn;
}
}
delf()
{
if(first==NULL)
printf("empty list\n");
else if(first->ptr==first)
{
printf("deleted element =%d",first->data);
free(first);
first=last=NULL;
}
else
{
temp=first;
first=first->ptr;
printf("deleted element=%d",temp->data);
free(temp);
last->ptr=first;
}
}
dele()
{
if(first==NULL)
printf("empty list\n");
else if(last->ptr==last)
{
printf("deleted element=%d",first->data);
free(first);
first=last=NULL;
}
else
{
temp1=first;
while(temp1->ptr!=last)
{
temp1=temp1->ptr;
}
printf("deleted element=%d",last->data);
free(last);
last=temp1;
last->ptr=first;
}
}
DOUBLE
#include
#include
#include
struct node
{
int data;
struct node *lptr;
struct node *rptr;
};
typedef struct node NODE;
NODE *head=NULL,*nn,*temp,*temp1,*temp2;
main()
{
int rpt=1,ch;
clrscr();
while(rpt)
{
printf("1.entering the element from the front\n");
printf("2.entering the element from the middle\n");
printf("3.entering the element from the end\n");
printf("4.deleting the element from the front\n");
printf("5.deleting the element from the end\n");
printf("6.deleting the element from the middle\n");
printf("7.display\n");
printf("8.exit\n");
printf("\n\nenter the choice\n");
scanf("%d",&ch);
switch(ch)
{
case 1:denterf();
break;
case 2:denterm();
break;
case 3:dentere();
break;
case 4:ddeletef();
break;
case 5:ddeletee();
break;
case 6:ddeletem();
break;
case 7:display();
break;
case 8:return;
}
printf("Do you want to continue press 1 for yes and 0 for no\n");
scanf("%d",&rpt);
}
}
denterf()
{
int item;
nn=(NODE*)malloc(sizeof(NODE));
printf("enter the item to be inserted\n");
scanf("%d",&item);
nn->data=item;
if(head==NULL)
{
nn->lptr=nn->rptr=NULL;
head=nn;
}
else
{
nn->rptr=head;
head->lptr=nn;
nn->lptr=NULL;
head=nn;
}
}
dentere()
{
int item;
nn=(NODE*)malloc(sizeof(NODE));
printf("enter the element to be inserted\n");
scanf("%d",&item);
nn->data=item;
if(head==NULL)
{
nn->lptr=nn->rptr=NULL;
head=nn;
}
else
{
temp=head;
while(temp->rptr!=NULL)
{
temp=temp->rptr;
}
temp->rptr=nn;
nn->lptr=temp;
nn->rptr=NULL;
}
}
denterm()
{
int item,pos,i;
nn=(NODE*)malloc(sizeof(NODE));
printf("enter the element to be inserted\n");
scanf("%d",&item);
nn->data=item;
nn->lptr=nn->rptr=NULL;
if(head==NULL)
head=nn;
else
{
temp1=head;
temp2=NULL;
printf("enter the position where to insert the element\n");
scanf("%d",&pos);
for(i=1;i {
temp2=temp1;
temp1=temp1->rptr;
}
temp2->rptr=nn;
nn->lptr=temp2;
nn->rptr=temp1;
temp1->lptr=nn;
}
}
ddeletef()
{
if(head==NULL)
printf("empty list\n");
else if(head->rptr==NULL)
{
printf("deleted element=%d",head->data);
free(head);
head=NULL;
}
else
{
temp=head;
head=head->rptr;
printf("Deleted element=%d",temp->data);
head->lptr=NULL;
free(temp);
}
}
ddeletee()
{
if(head==NULL)
printf("empty list\n");
else if(head->rptr==NULL)
{
printf("deleted element=%d",head->data);
free(head);
head=NULL;
}
else
{
temp1=NULL;
temp2=head;
while(temp2->rptr!=NULL)
{
temp1=temp2;
temp2=temp2->rptr;
}
printf("deleted element=%d",temp2->data);
temp1->rptr=NULL;
free(temp2);
}
}
ddeletem()
{
int pos,i;
if(head==NULL)
{
printf("empty list\n");
}
else if(head->rptr==NULL)
{
printf("Deleted element=%d",head->data);
free(head);
head=NULL;
}
else
{
printf("enter the position of the element to be inserted\n");
scanf("%d",&pos);
temp1=NULL;
temp2=head;
for(i=1;i {
temp1=temp2;
temp2=temp2->rptr;
}
printf("deleted element=%d",temp2->data);
temp1->rptr=temp2->lptr;
temp2->lptr=temp->rptr;
free(temp2);
}
}
display()
{
if(head==NULL)
printf("empty list\n");
else if(temp->rptr==NULL)
{
temp=head;
printf("%d->\n",temp->data);
}
else
{
temp=head;
while(temp!=NULL)
{
printf("%d->\n",temp->data);
temp=temp->rptr;
}
}
}
LAG
//LAGRANGE INTERPOLATION
#include
#include
#define max 90
void main()
{
float a[max+1],b[max+1],num,den,x,y=0;
int i,j,n;
clrscr();
printf("enter n value:");
scanf("%d",&n);
printf("Enter values of x,y");
for(i=0;i<=n;i++)
scanf("%f %f",&a[i],&b[i]);
printf("Enter value of x at which value of y is to be calculated");
scanf("%f",&x);
for(i=0;i<=n;i++)
{
num=1;
den=1;
for(j=0;j if(j!=i)
{
num*=x-a[j];
den*=a[i]-a[j];
}
y+=(num/den)*b[i];
}
printf("when x=4.1f | y=7.1f\n", x,y);
getch();
}
POSTE
#include
#include
#include
#include
#define MAX 50
char stack[MAX] ;
char infix[MAX] ;
char postfix[MAX] ;
char eval[MAX] ;
char *s, *t ; /*pointers to input and output strings*/
int top; /*Stack top*/
/*Function Prototypes*/
void Initialize (void);
void SetExpression (char *);
char Pop (void );
void Push (char);
int priority (char);
void Convert (void);
int Evaluate(void);
void main( )
{
int m;
clrscr( ) ;
Initialize ( ) ;
printf ( "\nEnter an infix expression: " ) ;
gets ( infix ) ;
SetExpression (infix) ;
Convert( ) ;
printf ( "\nThe Postfix expression is: " ) ;
puts(postfix);
strcpy(eval,postfix);
m=Evaluate( );
printf("answer: %d", m );
getch( ) ;
}
void Initialize (void)
{
top = -1 ;/*Make stack empty*/
}
void SetExpression ( char *str )
{
s = str ;
t = postfix;
}
/* adds operator to the stack */
void Push ( char c )
{
if ( top == MAX - 1 )
printf ( "\nStack is full.\n" ) ;
else
{
top++ ;
stack[top] = c ;
}
}
/* pops an operator from the stack */
char Pop ( void )
{
if ( top == -1 ) /* Stack is empty*/
return -1 ;
else
{
char item = stack[top] ;
top-- ;
return item ;
}
}
int priority(char c)
{
if ( c == '*' || c == '/' || c == '%' )
return 2;
else if ( c == '+' || c == '-' )
return 1;
else
return 0;
}
/* converts the infix expr. to postfix form */
void Convert (void)
{
char x ;
while ( *( s ) )
{ /*Skip white spaces, if any*/
if ( *( s ) == ' ' || *( s ) == '\t' )
{
s++ ;
continue ;
}
if ( isdigit ( *( s ) ) )/*Operands*/
{
while ( isdigit ( *( s ) ) )
{
*( t ) = *( s ) ;
s++ ;
t++ ;
}
}
if ( *( s ) == '(' )/*Opening Parenthesis*/
{
Push ( *( s ) ) ;
s++ ;
}
if ( *( s ) == '*' || *( s ) == '+' || *( s ) == '/' || *( s ) == '%' || *( s )
== '-' ) /*operators*/
{
if ( top != -1 )
{
x = Pop ( ) ;
while ( priority ( x ) >= priority ( *( s ) ) )
{
*( t ) = x ;
t++ ;
x = Pop ( ) ;
}
Push( x ) ;
Push ( *( s ) ) ;
}
else Push( *( s ) ) ;
s++ ;
}
if ( *( s ) == ')' )/*Closing Parenthesis*/
{
x = Pop ( ) ;
while ( x != '(' )
{
*( t ) = x ;
t++ ;
x = Pop ( ) ;
}
s++ ;
}
}
while ( top != -1 )/*While stack is not empty*/
{
x = Pop ( ) ;
*( t ) = x ;
t++ ;
}
t++ ;
}
int Evaluate(void)
{
int i,l,a,b,q,z;
l=strlen(eval);
for(i=0;i {
if ( isdigit ( eval[i] ) )
{
Push(eval[i]);
}
else if ( eval[i] == '*' || eval[i] == '+' || eval[i] == '/' || eval[i] == '%'
|| eval[i] == '-' )
{
a = Pop ( );
b = Pop ( );
switch( eval[i] )
{
case '+' : q=b+a; break;
case '-' : q=b-a; break;
case '*' : q=b*a; break;
case '/' : q=b/a; break;
}
Push ( q );
}
}
z = Pop ( );
return z;
}
QUEUE
/* queue using arrays*/
#include
#include
#define max 5
int front=-1,rear=-1;
int q[max];
void main()
{
int rpt=1;
int ch;
clrscr();
while(rpt)
{
printf("1.qinsert\n");
printf("2.qdelete\n");
printf("3.display\n");
printf("4.exit\n");
printf("enter your choice\n");
scanf("%d",&ch);
switch(ch)
{
case 1:qinsert();
break;
case 2:qdelete();
break;
case 3:display();
break;
case 4:return;
}
printf("\nDo you want to continue press 1 for yes and 0 for no\n");
scanf("%d",&rpt);
}
}
qinsert()
{
int num;
if(rear==(max-1))
{
printf("queue is full\n");
return;
}
else
{
printf("enter the element\n");
scanf("%d",&num);
rear=rear+1;
q[rear]=num;
if(front==-1)
front=0;
}
}
qdelete()
{
int num;
if(front==-1)
{
printf("queue is empty\n");
return;
}
else
{
num=q[front];
printf("Deleted number is %d",num);
if(front==rear)
front=rear=-1;
else
front++;
}
}
display()
{
int i;
if(front==-1)
{
printf("queue is empty\n");
return;
}
else
{
for(i=front;i<=rear;i++)
{
printf("%d\n",q[i]);
}
}
}
STACK
#include
#include
/*stack using arrays*/
#define max 5
int top=-1;
int stack[max];
void main()
{
int rpt=1;
int ch;
clrscr();
while(rpt)
{
printf("1.push\n");
printf("2.pop\n");
printf("3.display\n");
printf("4.exit\n");
printf("enter your choice\n");
scanf("%d",&ch);
switch(ch)
{
case 1:push();
break;
case 2:pop();
break;
case 3:display();
break;
case 4:return;
}
printf("Do you want to continue press 1 for yes and 0 for no\n");
scanf("%d",&rpt);
}
}
push()
{
int item;
if(top==(max-1))
{
printf("stack is full\n");
return;
}
else
{
printf("enter the element to be inserted\n");
scanf("%d",&item);
top=top+1;
stack[top]=item;
}
}
pop()
{
if(top==-1)
{
printf("stack is empty\n");
return;
}
else
{
printf("Deleted element is %d",stack[top]);
top=top-1;
}
}
display()
{
int i;
if(top==-1)
{
printf("stack is empty\n");
return;
}
else
{
for(i=top;i>=0;i--)
printf("%d\n",stack[i]);
}
}
TREP
//trapezoidal method //
#include
#include
void main()
{
int a[40],i,j,k,n;
float h,x,b=0,a1,c1;
clrscr();
printf("Enter the no of terms\n enter integral values");
scanf("%d %f %f",&n,&a1,&c1);
h=(c1-a1)/n;
printf("ENTER %d VALUES",n+1);
for(i=0;i scanf("%d",&a[i]);
for(j=0;j b+=a[j];
x=(n/2)*(a[0]+a[n])+(2*b);
printf("%f",x);
getch();
}
CMX
/* Write a C program that uses functions to perform the following operations:
i) Reading a complex number
ii) Writing a complex number
iii) Addition of two complex numbers
iv) Multiplication of two complex numbers
(Note: represent complex number using a structure.) */
#include
#include
void arithmetic(int opern);
struct comp
{
double realpart;
double imgpart;
};
void main()
{
int opern;
clrscr();
printf("\n\n \t\t\t***** MAIN MENU *****");
printf("\n\n Select your option: \n 1 : ADD\n 2 : MULTIPLY\n 0 : EXIT \n\n\t\t Enter your Option [ ]\b\b");
scanf("%d",&opern);
switch(opern)
{
case 0:
exit(0);
case 1:
case 2:
arithmetic(opern);
default:
main();
}
}
void arithmetic(int opern)
{
struct comp w1, w2, w;
printf("\n Enter two Complex Numbers (x+iy):\n Real Part of First Number:");
scanf("%lf",&w1.realpart);
printf("\n Imaginary Part of First Number:");
scanf("%lf",&w1.imgpart);
printf("\n Real Part of Second Number:");
scanf("%lf",&w2.realpart);
printf("\n Imaginary Part of Second Number:");
scanf("%lf",&w2.imgpart);
switch(opern)
{
/*addition of complex number*/
case 1:
w.realpart = w1.realpart+w2.realpart;
w.imgpart = w1.imgpart+w2.imgpart;
break;
/*multiplication of complex number*/
case 2:
w.realpart=(w1.realpart*w2.realpart)-(w1.imgpart*w2.imgpart);
w.imgpart=(w1.realpart*w2.imgpart)+(w1.imgpart*w2.realpart);
break;
}
if (w.imgpart>0)
printf("\n Answer = %lf+%lfi",w.realpart,w.imgpart);
else
printf("\n Answer = %lf%lfi",w.realpart,w.imgpart);
getch();
main();
}
INSERT_SUBSTRINGS
/* Write a C program that uses functions to perform the following operations:
To insert a sub-string in to given main string from a given position.
*/
#include
#include
#include
void main()
{
char a[10];
char b[10];
char c[10];
int p=0,r=0,i=0;
int t=0;
int x,g,s,n,o;
clrscr();
puts("Enter First String:");
gets(a);
puts("Enter Second String:");
gets(b);
printf("Enter the position where the item has to be inserted: ");
scanf("%d",&p);
r = strlen(a);
n = strlen(b);
i=0;
// Copying the input string into another array
while(i <= r)
{
c[i]=a[i];
i++;
}
s = n+r;
o = p+n;
// Adding the sub-string
for(i=p;i{
x = c[i];
if(t {
a[i] = b[t];
t=t+1;
}
a[o]=x;
o=o+1;
}
printf("%s", a);
getch();
}
PYRAMID
/* Write a C program to construct a pyramid of numbers. */
#include
#include
void main()
{
int num,i,y,x=35;
clrscr();
printf("\nEnter the number to generate the pyramid:\n");
scanf("%d",&num);
for(y=0;y<=num;y++)
{
/*(x-coordinate,y-coordinate)*/
gotoxy(x,y+1);
/*for displaying digits towards the left and right of zero*/
for(i=0-y;i<=y;i++)
printf("%3d",abs(i));
x=x-3;
}
getch();
}
SEARCH STRING
/* Write a C program that displays the position or index in the string S
where the string T begins, or - 1 if S doesn't contain T.
*/
#include
#include
#include
void main()
{
char s[30], t[20];
char *found;
clrscr();
/* Entering the main string */
puts("Enter the first string: ");
gets(s);
/* Entering the string whose position or index to be displayed */
puts("Enter the string to be searched: ");
gets(t);
/*Searching string t in string s */
found=strstr(s,t);
if(found)
printf("Second String is found in the First String at %d position.\n",found-s);
else
printf("-1");
getch();
}
TOWER OF HANNOI
/* Write C programs that use both recursive and non-recursive functions
To solve Towers of Hanoi problem.*/
#include
#include
/* Non-Recursive Function*/
void hanoiNonRecursion(int num,char sndl,char indl,char dndl)
{
char stkn[100],stksndl[100],stkindl[100],stkdndl[100],stkadd[100],temp;
int top,add;
top=NULL;
one:
if(num==1)
{
printf("\nMove top disk from needle %c to needle %c ",sndl,dndl);
goto four;
}
two:
top=top+1;
stkn[top]=num;
stksndl[top]=sndl;
stkindl[top]=indl;
stkdndl[top]=dndl;
stkadd[top]=3;
num=num-1;
sndl=sndl;
temp=indl;
indl=dndl;
dndl=temp;
goto one;
three:
printf("\nMove top disk from needle %c to needle %c ",sndl,dndl);
top=top+1;
stkn[top]=num;
stksndl[top]=sndl;
stkindl[top]=indl;
stkdndl[top]=dndl;
stkadd[top]=5;
num=num-1;
temp=sndl;
sndl=indl;
indl=temp;
dndl=dndl;
goto one;
four:
if(top==NULL)
return;
num=stkn[top];
sndl=stksndl[top];
indl=stkindl[top];
dndl=stkdndl[top];
add=stkadd[top];
top=top-1;
if(add==3)
goto three;
else if(add==5)
goto four;
}
/* Recursive Function*/
void hanoiRecursion( int num,char ndl1, char ndl2, char ndl3)
{
if ( num == 1 ) {
printf( "Move top disk from needle %c to needle %c.", ndl1, ndl2 );
return;
}
hanoiRecursion( num - 1,ndl1, ndl3, ndl2 );
printf( "Move top disk from needle %c to needle %c.", ndl1, ndl2 );
hanoiRecursion( num - 1,ndl3, ndl2, ndl1 );
}
void main()
{
int no;
clrscr();
printf("Enter the no. of disks to be transferred: ");
scanf("%d",&no);
if(no<1)
printf("\nThere's nothing to move.");
else
printf("Non-Recursive");
hanoiNonRecursion(no,'A','B','C');
printf("\nRecursive");
hanoiRecursion(no,'A','B','C');
getch();
}
COMPLEMENT
/* 2’s complement of a number is obtained by scanning it from right to left and complementing all
the bits after the first appearance of a 1. Thus 2’s complement of 11100 is 00100.
Write a C program to find the 2’s complement of a binary number.*/
#include
#include
void complement (char *a);
void main()
{
char a[16];
int i;
clrscr();
printf("Enter the binary number");
gets(a);
for(i=0;a[i]!='\0';i++)
{
if(a[i]!='0' && a[i]!='1')
{
printf("NOT BINARY NUMBER");
exit(0);
}
}
complement(a);
getch();
}
void complement (char *a)
{
int l, i, c=0;
char b[16];
l=strlen(a);
for (i=l-1; i>=0; i--)
{
if (a[i]=='0')
b[i]='1';
else
b[i]='0';
}
for(i=l-1; i>=0; i--)
{
if(i==l-1)
{
if (b[i]=='0')
b[i]='1';
else
{
b[i]='0';
c=1;
}
}
else
{
if(c==1 && b[i]=='0')
{
b[i]='1';
c=0;
}
else if (c==1 && b[i]=='1')
{
b[i]='0';
c=1;
}
}
}
b[l]='\0';
printf("The 2's complement is %s", b);
}
PASCAL
/* Write a C program to generate Pascal's triangle. */
#include
#include
void main()
{
int bin,p,q,r,x;
clrscr();
bin=1;
q=0;
printf("Rows you want to input:");
scanf("%d",&r);
printf("\nPascal's Triangle:\n");
while(q {
for(p=40-3*q;p>0;--p)
printf(" ");
for(x=0;x<=q;++x)
{
if((x==0)||(q==0))
bin=1;
else
bin=(bin*(q-x+1))/x;
printf("%6d",bin);
}
printf("\n");
++q;
}
getch();
}
ROM_DEC
/* Write a C program to convert a Roman numeral to its decimal equivalent. */
#include
#include
#include
#include
void main()
{
int *a,len,i,j,k;
char *rom;
clrscr();
printf("Enter the Roman Numeral:");
scanf("%s",rom);
len=strlen(rom);
for(i=0;i {
if(rom[i]=='I')
a[i]=1;
else if(rom[i]=='V')
a[i]=5;
else if(rom[i]=='X')
a[i]=10;
else if(rom[i]=='L')
a[i]=50;
else if(rom[i]=='C')
a[i]=100;
else if(rom[i]=='D')
a[i]=500;
else if(rom[i]=='M')
a[i]=1000;
else
{
printf("\nInvalid Value");
getch();
exit(0);
}
}
k=a[len-1];
for(i=len-1;i>0;i--)
{
if(a[i]>a[i-1])
k=k-a[i-1];
else if(a[i]==a[i-1] || a[i] k=k+a[i-1];
}
printf("\nIts Decimal Equivalent is:");
printf("%d",k);
getch();
}
SERIES
//compute sum of geometric progression
// 1+x+x2+x3+....+xn
#include
#include
#include
void main()
{
int sum,i,x,n;
clrscr();
printf("enter values for x,n");
scanf("%d %d",&x,&n);
if(x<=0 ||n<=0)
{
printf("It is valid\n");
sum=1;
for(i=1;i<=n;i++)
{
sum=sum+pow(n,i);
}
printf("sum of series is %d\n",sum);
}
getch();
}
#include
#include
#define max 5
int front=-1,rear=-1;
int cq[max];
void main()
{
int rpt=1;
int ch;
clrscr();
while(rpt)
{
printf("1.cqinsert\n");
printf("2.cqdelete\n");
printf("3.cqdisplay\n");
printf("4.exit\n");
printf("enter your choice\n");
scanf("%d",&ch);
switch(ch)
{
case 1:cqinsert();
break;
case 2:cqdelete();
break;
case 3:cqdisplay();
break;
case 4:return;
}
printf("Do you want to continue press 1 for yes and 0 for no\n");
scanf("%d",&rpt);
}
}
cqinsert()
{
int num;
if(front==(rear+1)%max)
{
printf("queue is full\n");
return;
}
else
{
printf("enter the element\n");
scanf("%d",&num);
rear=rear+1%max;
cq[rear]=num;
if(front==-1)
front=0;
}
}
cqdelete()
{
int num;
if(front==-1)
{
printf("queue is empty\n");
return;
}
else
{
num=cq[front];
printf("Deleted number is %d",num);
if(front==rear)
front=rear=-1;
else
front=(front+1)%max;
}
}
cqdisplay()
{
int i;
if(front==-1)
{
printf("queue is empty\n");
return;
}
else if(front
for(i=front;i<=rear;i++)
printf("%d\n",cq[i]);
}
else
{
for(i=front;i
for(i=0;i<=rear;i++)
printf("%d\n",cq[i]);
}
}
INFTPOST
/*Infix To Postfix*/
#include
#include
struct stack
{
int top;
char item[30];
};
void push(struct stack *ps,char n)
{
ps->top++;
ps->item[ps->top]=n;
}
void pop(struct stack *ps)
{
ps->top--;
}
int isempty(struct stack *ps)
{
if(ps->top==-1)
{
return(1);
}
else
{
return(0);
}
}
int prcd(char stktop,char op)
{
if(((stktop=='+')||(stktop=='-'))&&((op=='*')||(op=='/')))
{
return(0);
}
if(stktop=='(')
{
return(0);
}
if((stktop!=')')&&(op=='('))
{
return(0);
}
if((stktop!='(')&&(op==')'))
{
return(1);
}
}
void main()
{
struct stack s;
char input[30],output[30];
char optop,ans;
int i=0,j=0,k=0;
s.top=-1;
clrscr();
printf("enter an expression in infix form:\n");
scanf("%s",input);
while(input[i]!='\0')
{
if(isalpha(input[i]))
{
output[j]=input[i];
j++;
}
else
{
while(!isempty(&s)&&(prcd(s.item[s.top],input[i])))
{
optop=s.item[s.top];
pop(&s);
output[j]=optop;
j++;
}
if(isempty(&s)||input[i]!=')')
{
push(&s,input[i]);
}
else
{
pop(&s);
}
}
i++;
}
while(!isempty(&s))
{
optop=s.item[s.top];
pop(&s);
output[j]=optop;
j++;
}
printf("\npostfix form:\t");
output[j]='\0';
while(output[k]!='\0')
{
printf("%c",output[k]);
k++;
}
scanf("%c",&ans);
getch();
}
MERGSOR
#include
#include
void main()
{
int m,n,p,sum=0;
int a[15],b[15],c[45]={0 };
//c[15]={0};
clrscr();
//printf("enter x,y values");
//scanf("%d",&x,&y);
printf("\n enter elements in first list\n");
for(m=0;m<5;m++)
{
scanf("%d",&a[m]);
if(a[m]==0)
m--;
sum=sum+abs(a[m]);
}
printf("\n enter elements in first list\n");
for(n=0;n<5;n++)
{
scanf("%d",&b[n]);
if(a[n]==0)
m--;
sum=sum+abs(b[n]);
}
p=n=m=0;
m=m-sum;
while(m
for(n=0;n<5;n++)
{
if(m==a[n] || m==b[n])
c[p++]=m;
if(m==a[n] && m==b[n])
c[p++]=m;
}
m++;
}
puts("MERGED SORTED LIST :");
for(n=0;n<10;++n)
printf("\t%d ",c[n]);
}
QLINK
/* Write C programs that implement Queue (its operations) using ii) Pointers */
#define true 1
#define false 0
#include
#include
#include
struct q_point
{
int ele;
struct q_point* n;
};
struct q_point *f_ptr = NULL;
int e_que(void);
void add_ele(int);
int rem_ele(void);
void show_ele();
/*main function*/
void main()
{
int ele,choice,j;
while(1)
{
clrscr();
printf("\n\n****IMPLEMENTATION OF QUEUE USING POINTERS****\n");
printf("==============================================");
printf("\n\t\t MENU\n");
printf("==============================================");
printf("\n\t[1] To insert an element");
printf("\n\t[2] To remove an element");
printf("\n\t[3] To display all the elements");
printf("\n\t[4] Exit");
printf("\n\n\tEnter your choice:");
scanf("%d", &choice);
switch(choice)
{
case 1:
{
printf("\n\tElement to be inserted:");
scanf("%d",&ele);
add_ele(ele);
getch();
break;
}
case 2:
{
if(!e_que())
{
j=rem_ele();
printf("\n\t%d is removed from the queue",j);
getch();
}
else
{
printf("\n\tQueue is Empty.");
getch();
}
break;
}
case 3:
show_ele();
getch();
break;
case 4:
exit(1);
break;
default:
printf("\n\tInvalid choice.");
getch();
break;
}
}
}
/* Function to check if the queue is empty*/
int e_que(void)
{
if(f_ptr==NULL)
return true;
return false;
}
/* Function to add an element to the queue*/
void add_ele(int ele)
{
struct q_point *queue = (struct q_point*)malloc(sizeof(struct q_point));
queue->ele = ele;
queue->n = NULL;
if(f_ptr==NULL)
f_ptr = queue;
else
{
struct q_point* ptr;
ptr = f_ptr;
for(ptr=f_ptr ;ptr->n!=NULL; ptr=ptr->n);
ptr->n = queue;
}
}
/* Function to remove an element from the queue*/
int rem_ele()
{
struct q_point* queue=NULL;
if(e_que()==false)
{
int j = f_ptr->ele;
queue=f_ptr;
f_ptr = f_ptr->n;
free (queue);
return j;
}
else
{
printf("\n\tQueue is empty.");
return -9999;
}
}
/* Function to display the queue*/
void show_ele()
{
struct q_point *ptr=NULL;
ptr=f_ptr;
if(e_que())
{
printf("\n\tQUEUE is Empty.");
return;
}
else
{
printf("\n\tElements present in Queue are:\n\t");
while(ptr!=NULL)
{
printf("%d\t",ptr->ele);
ptr=ptr->n;
}
}
}
SL
#include
#include
#include
#include
struct node
{
int data;
struct node *ptr;
};
typedef struct node NODE;
NODE *head=NULL;
NODE *nn,*temp,*temp1,*temp2;
main()
{
int ch,r=1;
clrscr();
while(r)
{
printf("enter the choice\n");
printf("1.entering from the front\n");
printf("2.entering from the end\n");
printf("3.entering from the middle\n");
printf("4.deleting from the front\n");
printf("5.deleting from the end\n");
printf("6.display\n");
printf("7.deleting from the middle\n");
printf("8.exit\n");
scanf("%d",&ch);
switch(ch)
{
case 1:enterf();
break;
case 2: entere();
break;
case 3:enterm();
break;
case 4:delf();
break;
case 5:dele();
break;
case 6:display();
break;
case 7:delm();
break;
case 8:return;
}
printf("do you want to continue press 1 for yes and 0 for no\n");
scanf("%d",&r);
}
}
enterf()
{
int item;
nn=(NODE*)malloc(sizeof(NODE));
printf("enter the item to be inserted\n");
scanf("%d",&item);
if(head==NULL)
{
nn->data=item;
nn->ptr=NULL;
head=nn;
}
else
{
nn->data=item;
nn->ptr=head;
head=nn;
}
}
display()
{
if(head==NULL)
printf("empty list\n");
else
{
printf("elements in the list are\n");
temp=head;
while(temp!=NULL)
{
printf("%d->",temp->data);
temp=temp->ptr;
}
}
}
entere()
{
int item;
nn=(NODE*)malloc(sizeof(NODE));
printf("enter the item to be inserted\n");
scanf("%d",&item);
if(head==NULL)
{
nn->data=item;
nn->ptr=NULL;
head=nn;
}
else
{
temp=head;
while(temp->ptr!=NULL)
{
temp=temp->ptr;
}
nn->data=item;
temp->ptr=nn;
nn->ptr=NULL;
}
}
delf()
{
if(head==NULL)
printf("empty list\n");
else if(head->ptr==NULL)
{
printf("deleted element =%d",head->data);
free(head);
head=NULL;
}
else
{
temp=head;
head=head->ptr;
printf("deleted element=%d",temp->data);
free(temp);
}
}
dele()
{
if(head==NULL)
printf("empty list\n");
else if(head->ptr==NULL)
{
printf("deleted element=%d",head->data);
free(head);
head=NULL;
}
else
{
temp1=head;
temp2=NULL;
while(temp1->ptr!=NULL)
{
temp2=temp1;
temp1=temp1->ptr;
}
printf("deleted element=%d",temp1->data);
free(temp1);
temp2->ptr=NULL;
}
}
enterm()
{
int pos,item,i;
if(head==NULL)
{
nn=(NODE*)malloc(sizeof(NODE));
printf("enter the item to be inserted\n");
scanf("%d",&item);
nn->data=item;
nn->ptr=NULL;
head=nn;
}
else
{
printf("enter the position where you want to insert the element\n");
scanf("%d",&pos);
temp1=head;
temp2=NULL;
for(i=1;i
temp2=temp1;
temp1=temp1->ptr;
}
printf("enter the item to be inserted\n");
scanf("%d",&item);
nn=(NODE*)malloc(sizeof(NODE));
nn->data=item;
temp2->ptr=nn;
nn->ptr=temp1;
}
}
delm()
{
int pos,i;
if(head==NULL)
{
printf("empty list\n");
}
else if(head->ptr==NULL)
{
printf("deleted element=%d",head->data);
free(head);
head=NULL;
}
else
{
printf("enter the position of the element to be deleted\n");
scanf("%d",&pos);
temp1=NULL;
temp2=head;
for(i=1;i
temp1=temp2;
temp2=temp2->ptr;
}
printf("deleted element=%d\n",temp2->data);
temp1->ptr=temp2->ptr;
}
}
STACKL
/*stack using linked list*/
#include
#include
#include
struct node
{
int data;
struct node *ptr;
};
typedef struct node NODE;
NODE *top=NULL,*nn,*temp;
main()
{
int ch,rpt=1;
clrscr();
while(rpt)
{
printf("1.push\n");
printf("2.pop\n");
printf("3.display\n");
printf("4.exit\n");
printf("enter your choice\n");
scanf("%d",&ch);
switch(ch)
{
case 1:push();
break;
case 2:pop();
break;
case 3:display();
break;
case 4:return;
}
printf("\nDo you want to continue press 1 for yes and 0 for no\n");
scanf("%d",&rpt);
}
}
push()
{
int num;
printf("enter the element to be inserted\n");
scanf("%d",&num);
nn=(NODE*)malloc(sizeof(NODE));
nn->data=num;
if(top==NULL)
{
nn->ptr=NULL;
top=nn;
}
else
{
nn->ptr=top;
top=nn;
}
}
pop()
{
if(top==NULL)
printf("stack is empty\n");
else
{
temp=top;
printf("Deleted element=%d",temp->data);
top=top->ptr;
free(temp);
}
}
display()
{
if(top==NULL)
printf("empty list\n");
else
{
printf("status of the stack is\n");
temp=top;
while(temp!=NULL)
{
printf("%d\n",temp->data);
temp=temp->ptr;
}
}
}
TRTV
#include
#include
#include
#define newnode (tree_type *)malloc(sizeof(tree_type))
typedef struct tree
{
int info;
struct tree *left,*right;
}tree_type;
tree_type *insert(tree_type *t,int val)
{
if(t==NULL)
{
t=newnode;
t->info=val;
t->left=t->right=NULL;
}
else if(val
t->left=insert(t->left,val);
else if(val>t->info)
t->right=insert(t->right,val);
else
printf("\n Duplicate number");
return(t);
}
void preorder(tree_type *t)
{
if(t)
{
printf("%4d",t->info);
preorder(t->left);
preorder(t->right);
}
}
void inorder(tree_type *t)
{
if(t)
{
inorder(t->left);
printf("%4d",t->info);
inorder(t->right);
}
}
void postorder(tree_type *t)
{
if(t)
{
postorder(t->left);
postorder(t->right);
printf("%4d",t->info);
}
}
int menu()
{
int opt;
printf("\n 1.Create Tree\n 2. Insert Node\n 3. Delete Node\n");
printf("4.Search Item\n 5. Exit \nEnter Your Option");
scanf("%d",&opt);
return(opt);
}
int search(tree_type *t,int key)
{
if(t==NULL)
return NULL;
else if(key
return(search(t->left,key));
else if(key>t->info)
return(search(t->right,key));
else
return(1);
}
tree_type *find(tree_type *t)
{
while(t->right!=NULL)
t=t->right;
return(t);
}
tree_type *Delete(tree_type *t,int val)
{
tree_type *temp,*child;
if(t==NULL)
printf("ELEMENT NOT FOUND");
else if(val
t->left=Delete(t->left,val);
else if(val > t->info)
t->right=Delete(t->right,val);
else if(t->left!=NULL && t->right!=NULL)
{
temp=find(t->left);
t->info=temp->info;
t->left=Delete(t->left,t->info);
}
else
{
temp=t;
if(t->left==NULL)
child=t->right;
if(t->right==NULL)
child=t->left;
free(temp);
return(child);
}
return(t);
}
void main()
{
tree_type *root=NULL ,*val;
int opt,num;
while(1)
{
clrscr();
printf("\n\n PREORDER :");
preorder(root);
printf("\n\n INORDER :");
inorder(root);
printf("\n\n POST ORDER :");
postorder(root);
opt=menu();
switch(opt)
{
case 1:printf("Enter value or 0 to end\n");
do
{
scanf("%d",&num);
if(num!=0)
root=insert(root,num);
}while(num!=0);
break;
case 2:printf("Enter the number :");
scanf("%d",&num);
root=insert(root,num);
break;
case 3:printf("Enter the number to br deleted:");
scanf("%d",&num);
root=Delete(root,num);
break;
case 4:printf("\n Enter Item to search:");
scanf("%d",&num);
if(search(root,num))
printf("Item %d Found\n",num);
else
printf("NOT FOUND");
getch();
break;
case 5:return;
}
}
}
CIRL
#include
#include
#include
#include
struct node
{
int data;
struct node *ptr;
};
typedef struct node NODE;
NODE *first=NULL,*last=NULL;
NODE *nn,*temp,*temp1,*temp2;
main()
{
int ch,r=1;
clrscr();
while(r)
{
printf("enter the choice\n");
printf("1.entering from the front\n");
printf("2.entering from the end\n");
printf("3.deleting from the front\n");
printf("4.deleting from the end\n");
printf("5.display\n");
printf("6.exit\n");
scanf("%d",&ch);
switch(ch)
{
case 1:enterf();
break;
case 2: entere();
break;
case 3:delf();
break;
case 4:dele();
break;
case 5:display();
break;
case 6:return;
}
printf("do you want to continue press 1 for yes and 0 for no\n");
scanf("%d",&r);
}
}
enterf()
{
int item;
nn=(NODE*)malloc(sizeof(NODE));
printf("enter the item to be inserted\n");
scanf("%d",&item);
nn->data=item;
nn->ptr=nn;
if(first==NULL)
{
first=last=nn;
}
else
{
last->ptr=nn;
nn->ptr=first;
first=nn;
}
}
display()
{
if(first==NULL)
printf("empty list\n");
else if(first->ptr==first)
printf("%d->",first->data);
else
{
temp=first;
while(temp!=last)
{
printf("%d->",temp->data);
temp=temp->ptr;
}
printf("%d->",last->data);
}
}
entere()
{
int item;
nn=(NODE*)malloc(sizeof(NODE));
printf("enter the item to be inserted\n");
scanf("%d",&item);
nn->data=item;
nn->ptr=nn;
if(first ==NULL)
{
first=last=nn;
}
else
{
last->ptr=nn;
nn->ptr=first;
last=nn;
}
}
delf()
{
if(first==NULL)
printf("empty list\n");
else if(first->ptr==first)
{
printf("deleted element =%d",first->data);
free(first);
first=last=NULL;
}
else
{
temp=first;
first=first->ptr;
printf("deleted element=%d",temp->data);
free(temp);
last->ptr=first;
}
}
dele()
{
if(first==NULL)
printf("empty list\n");
else if(last->ptr==last)
{
printf("deleted element=%d",first->data);
free(first);
first=last=NULL;
}
else
{
temp1=first;
while(temp1->ptr!=last)
{
temp1=temp1->ptr;
}
printf("deleted element=%d",last->data);
free(last);
last=temp1;
last->ptr=first;
}
}
DOUBLE
#include
#include
#include
struct node
{
int data;
struct node *lptr;
struct node *rptr;
};
typedef struct node NODE;
NODE *head=NULL,*nn,*temp,*temp1,*temp2;
main()
{
int rpt=1,ch;
clrscr();
while(rpt)
{
printf("1.entering the element from the front\n");
printf("2.entering the element from the middle\n");
printf("3.entering the element from the end\n");
printf("4.deleting the element from the front\n");
printf("5.deleting the element from the end\n");
printf("6.deleting the element from the middle\n");
printf("7.display\n");
printf("8.exit\n");
printf("\n\nenter the choice\n");
scanf("%d",&ch);
switch(ch)
{
case 1:denterf();
break;
case 2:denterm();
break;
case 3:dentere();
break;
case 4:ddeletef();
break;
case 5:ddeletee();
break;
case 6:ddeletem();
break;
case 7:display();
break;
case 8:return;
}
printf("Do you want to continue press 1 for yes and 0 for no\n");
scanf("%d",&rpt);
}
}
denterf()
{
int item;
nn=(NODE*)malloc(sizeof(NODE));
printf("enter the item to be inserted\n");
scanf("%d",&item);
nn->data=item;
if(head==NULL)
{
nn->lptr=nn->rptr=NULL;
head=nn;
}
else
{
nn->rptr=head;
head->lptr=nn;
nn->lptr=NULL;
head=nn;
}
}
dentere()
{
int item;
nn=(NODE*)malloc(sizeof(NODE));
printf("enter the element to be inserted\n");
scanf("%d",&item);
nn->data=item;
if(head==NULL)
{
nn->lptr=nn->rptr=NULL;
head=nn;
}
else
{
temp=head;
while(temp->rptr!=NULL)
{
temp=temp->rptr;
}
temp->rptr=nn;
nn->lptr=temp;
nn->rptr=NULL;
}
}
denterm()
{
int item,pos,i;
nn=(NODE*)malloc(sizeof(NODE));
printf("enter the element to be inserted\n");
scanf("%d",&item);
nn->data=item;
nn->lptr=nn->rptr=NULL;
if(head==NULL)
head=nn;
else
{
temp1=head;
temp2=NULL;
printf("enter the position where to insert the element\n");
scanf("%d",&pos);
for(i=1;i
temp2=temp1;
temp1=temp1->rptr;
}
temp2->rptr=nn;
nn->lptr=temp2;
nn->rptr=temp1;
temp1->lptr=nn;
}
}
ddeletef()
{
if(head==NULL)
printf("empty list\n");
else if(head->rptr==NULL)
{
printf("deleted element=%d",head->data);
free(head);
head=NULL;
}
else
{
temp=head;
head=head->rptr;
printf("Deleted element=%d",temp->data);
head->lptr=NULL;
free(temp);
}
}
ddeletee()
{
if(head==NULL)
printf("empty list\n");
else if(head->rptr==NULL)
{
printf("deleted element=%d",head->data);
free(head);
head=NULL;
}
else
{
temp1=NULL;
temp2=head;
while(temp2->rptr!=NULL)
{
temp1=temp2;
temp2=temp2->rptr;
}
printf("deleted element=%d",temp2->data);
temp1->rptr=NULL;
free(temp2);
}
}
ddeletem()
{
int pos,i;
if(head==NULL)
{
printf("empty list\n");
}
else if(head->rptr==NULL)
{
printf("Deleted element=%d",head->data);
free(head);
head=NULL;
}
else
{
printf("enter the position of the element to be inserted\n");
scanf("%d",&pos);
temp1=NULL;
temp2=head;
for(i=1;i
temp1=temp2;
temp2=temp2->rptr;
}
printf("deleted element=%d",temp2->data);
temp1->rptr=temp2->lptr;
temp2->lptr=temp->rptr;
free(temp2);
}
}
display()
{
if(head==NULL)
printf("empty list\n");
else if(temp->rptr==NULL)
{
temp=head;
printf("%d->\n",temp->data);
}
else
{
temp=head;
while(temp!=NULL)
{
printf("%d->\n",temp->data);
temp=temp->rptr;
}
}
}
LAG
//LAGRANGE INTERPOLATION
#include
#include
#define max 90
void main()
{
float a[max+1],b[max+1],num,den,x,y=0;
int i,j,n;
clrscr();
printf("enter n value:");
scanf("%d",&n);
printf("Enter values of x,y");
for(i=0;i<=n;i++)
scanf("%f %f",&a[i],&b[i]);
printf("Enter value of x at which value of y is to be calculated");
scanf("%f",&x);
for(i=0;i<=n;i++)
{
num=1;
den=1;
for(j=0;j
{
num*=x-a[j];
den*=a[i]-a[j];
}
y+=(num/den)*b[i];
}
printf("when x=4.1f | y=7.1f\n", x,y);
getch();
}
POSTE
#include
#include
#include
#include
#define MAX 50
char stack[MAX] ;
char infix[MAX] ;
char postfix[MAX] ;
char eval[MAX] ;
char *s, *t ; /*pointers to input and output strings*/
int top; /*Stack top*/
/*Function Prototypes*/
void Initialize (void);
void SetExpression (char *);
char Pop (void );
void Push (char);
int priority (char);
void Convert (void);
int Evaluate(void);
void main( )
{
int m;
clrscr( ) ;
Initialize ( ) ;
printf ( "\nEnter an infix expression: " ) ;
gets ( infix ) ;
SetExpression (infix) ;
Convert( ) ;
printf ( "\nThe Postfix expression is: " ) ;
puts(postfix);
strcpy(eval,postfix);
m=Evaluate( );
printf("answer: %d", m );
getch( ) ;
}
void Initialize (void)
{
top = -1 ;/*Make stack empty*/
}
void SetExpression ( char *str )
{
s = str ;
t = postfix;
}
/* adds operator to the stack */
void Push ( char c )
{
if ( top == MAX - 1 )
printf ( "\nStack is full.\n" ) ;
else
{
top++ ;
stack[top] = c ;
}
}
/* pops an operator from the stack */
char Pop ( void )
{
if ( top == -1 ) /* Stack is empty*/
return -1 ;
else
{
char item = stack[top] ;
top-- ;
return item ;
}
}
int priority(char c)
{
if ( c == '*' || c == '/' || c == '%' )
return 2;
else if ( c == '+' || c == '-' )
return 1;
else
return 0;
}
/* converts the infix expr. to postfix form */
void Convert (void)
{
char x ;
while ( *( s ) )
{ /*Skip white spaces, if any*/
if ( *( s ) == ' ' || *( s ) == '\t' )
{
s++ ;
continue ;
}
if ( isdigit ( *( s ) ) )/*Operands*/
{
while ( isdigit ( *( s ) ) )
{
*( t ) = *( s ) ;
s++ ;
t++ ;
}
}
if ( *( s ) == '(' )/*Opening Parenthesis*/
{
Push ( *( s ) ) ;
s++ ;
}
if ( *( s ) == '*' || *( s ) == '+' || *( s ) == '/' || *( s ) == '%' || *( s )
== '-' ) /*operators*/
{
if ( top != -1 )
{
x = Pop ( ) ;
while ( priority ( x ) >= priority ( *( s ) ) )
{
*( t ) = x ;
t++ ;
x = Pop ( ) ;
}
Push( x ) ;
Push ( *( s ) ) ;
}
else Push( *( s ) ) ;
s++ ;
}
if ( *( s ) == ')' )/*Closing Parenthesis*/
{
x = Pop ( ) ;
while ( x != '(' )
{
*( t ) = x ;
t++ ;
x = Pop ( ) ;
}
s++ ;
}
}
while ( top != -1 )/*While stack is not empty*/
{
x = Pop ( ) ;
*( t ) = x ;
t++ ;
}
t++ ;
}
int Evaluate(void)
{
int i,l,a,b,q,z;
l=strlen(eval);
for(i=0;i
if ( isdigit ( eval[i] ) )
{
Push(eval[i]);
}
else if ( eval[i] == '*' || eval[i] == '+' || eval[i] == '/' || eval[i] == '%'
|| eval[i] == '-' )
{
a = Pop ( );
b = Pop ( );
switch( eval[i] )
{
case '+' : q=b+a; break;
case '-' : q=b-a; break;
case '*' : q=b*a; break;
case '/' : q=b/a; break;
}
Push ( q );
}
}
z = Pop ( );
return z;
}
QUEUE
/* queue using arrays*/
#include
#include
#define max 5
int front=-1,rear=-1;
int q[max];
void main()
{
int rpt=1;
int ch;
clrscr();
while(rpt)
{
printf("1.qinsert\n");
printf("2.qdelete\n");
printf("3.display\n");
printf("4.exit\n");
printf("enter your choice\n");
scanf("%d",&ch);
switch(ch)
{
case 1:qinsert();
break;
case 2:qdelete();
break;
case 3:display();
break;
case 4:return;
}
printf("\nDo you want to continue press 1 for yes and 0 for no\n");
scanf("%d",&rpt);
}
}
qinsert()
{
int num;
if(rear==(max-1))
{
printf("queue is full\n");
return;
}
else
{
printf("enter the element\n");
scanf("%d",&num);
rear=rear+1;
q[rear]=num;
if(front==-1)
front=0;
}
}
qdelete()
{
int num;
if(front==-1)
{
printf("queue is empty\n");
return;
}
else
{
num=q[front];
printf("Deleted number is %d",num);
if(front==rear)
front=rear=-1;
else
front++;
}
}
display()
{
int i;
if(front==-1)
{
printf("queue is empty\n");
return;
}
else
{
for(i=front;i<=rear;i++)
{
printf("%d\n",q[i]);
}
}
}
STACK
#include
#include
/*stack using arrays*/
#define max 5
int top=-1;
int stack[max];
void main()
{
int rpt=1;
int ch;
clrscr();
while(rpt)
{
printf("1.push\n");
printf("2.pop\n");
printf("3.display\n");
printf("4.exit\n");
printf("enter your choice\n");
scanf("%d",&ch);
switch(ch)
{
case 1:push();
break;
case 2:pop();
break;
case 3:display();
break;
case 4:return;
}
printf("Do you want to continue press 1 for yes and 0 for no\n");
scanf("%d",&rpt);
}
}
push()
{
int item;
if(top==(max-1))
{
printf("stack is full\n");
return;
}
else
{
printf("enter the element to be inserted\n");
scanf("%d",&item);
top=top+1;
stack[top]=item;
}
}
pop()
{
if(top==-1)
{
printf("stack is empty\n");
return;
}
else
{
printf("Deleted element is %d",stack[top]);
top=top-1;
}
}
display()
{
int i;
if(top==-1)
{
printf("stack is empty\n");
return;
}
else
{
for(i=top;i>=0;i--)
printf("%d\n",stack[i]);
}
}
TREP
//trapezoidal method //
#include
#include
void main()
{
int a[40],i,j,k,n;
float h,x,b=0,a1,c1;
clrscr();
printf("Enter the no of terms\n enter integral values");
scanf("%d %f %f",&n,&a1,&c1);
h=(c1-a1)/n;
printf("ENTER %d VALUES",n+1);
for(i=0;i
for(j=0;j
x=(n/2)*(a[0]+a[n])+(2*b);
printf("%f",x);
getch();
}
CMX
/* Write a C program that uses functions to perform the following operations:
i) Reading a complex number
ii) Writing a complex number
iii) Addition of two complex numbers
iv) Multiplication of two complex numbers
(Note: represent complex number using a structure.) */
#include
#include
void arithmetic(int opern);
struct comp
{
double realpart;
double imgpart;
};
void main()
{
int opern;
clrscr();
printf("\n\n \t\t\t***** MAIN MENU *****");
printf("\n\n Select your option: \n 1 : ADD\n 2 : MULTIPLY\n 0 : EXIT \n\n\t\t Enter your Option [ ]\b\b");
scanf("%d",&opern);
switch(opern)
{
case 0:
exit(0);
case 1:
case 2:
arithmetic(opern);
default:
main();
}
}
void arithmetic(int opern)
{
struct comp w1, w2, w;
printf("\n Enter two Complex Numbers (x+iy):\n Real Part of First Number:");
scanf("%lf",&w1.realpart);
printf("\n Imaginary Part of First Number:");
scanf("%lf",&w1.imgpart);
printf("\n Real Part of Second Number:");
scanf("%lf",&w2.realpart);
printf("\n Imaginary Part of Second Number:");
scanf("%lf",&w2.imgpart);
switch(opern)
{
/*addition of complex number*/
case 1:
w.realpart = w1.realpart+w2.realpart;
w.imgpart = w1.imgpart+w2.imgpart;
break;
/*multiplication of complex number*/
case 2:
w.realpart=(w1.realpart*w2.realpart)-(w1.imgpart*w2.imgpart);
w.imgpart=(w1.realpart*w2.imgpart)+(w1.imgpart*w2.realpart);
break;
}
if (w.imgpart>0)
printf("\n Answer = %lf+%lfi",w.realpart,w.imgpart);
else
printf("\n Answer = %lf%lfi",w.realpart,w.imgpart);
getch();
main();
}
INSERT_SUBSTRINGS
/* Write a C program that uses functions to perform the following operations:
To insert a sub-string in to given main string from a given position.
*/
#include
#include
#include
void main()
{
char a[10];
char b[10];
char c[10];
int p=0,r=0,i=0;
int t=0;
int x,g,s,n,o;
clrscr();
puts("Enter First String:");
gets(a);
puts("Enter Second String:");
gets(b);
printf("Enter the position where the item has to be inserted: ");
scanf("%d",&p);
r = strlen(a);
n = strlen(b);
i=0;
// Copying the input string into another array
while(i <= r)
{
c[i]=a[i];
i++;
}
s = n+r;
o = p+n;
// Adding the sub-string
for(i=p;i
x = c[i];
if(t
a[i] = b[t];
t=t+1;
}
a[o]=x;
o=o+1;
}
printf("%s", a);
getch();
}
PYRAMID
/* Write a C program to construct a pyramid of numbers. */
#include
#include
void main()
{
int num,i,y,x=35;
clrscr();
printf("\nEnter the number to generate the pyramid:\n");
scanf("%d",&num);
for(y=0;y<=num;y++)
{
/*(x-coordinate,y-coordinate)*/
gotoxy(x,y+1);
/*for displaying digits towards the left and right of zero*/
for(i=0-y;i<=y;i++)
printf("%3d",abs(i));
x=x-3;
}
getch();
}
SEARCH STRING
/* Write a C program that displays the position or index in the string S
where the string T begins, or - 1 if S doesn't contain T.
*/
#include
#include
#include
void main()
{
char s[30], t[20];
char *found;
clrscr();
/* Entering the main string */
puts("Enter the first string: ");
gets(s);
/* Entering the string whose position or index to be displayed */
puts("Enter the string to be searched: ");
gets(t);
/*Searching string t in string s */
found=strstr(s,t);
if(found)
printf("Second String is found in the First String at %d position.\n",found-s);
else
printf("-1");
getch();
}
TOWER OF HANNOI
/* Write C programs that use both recursive and non-recursive functions
To solve Towers of Hanoi problem.*/
#include
#include
/* Non-Recursive Function*/
void hanoiNonRecursion(int num,char sndl,char indl,char dndl)
{
char stkn[100],stksndl[100],stkindl[100],stkdndl[100],stkadd[100],temp;
int top,add;
top=NULL;
one:
if(num==1)
{
printf("\nMove top disk from needle %c to needle %c ",sndl,dndl);
goto four;
}
two:
top=top+1;
stkn[top]=num;
stksndl[top]=sndl;
stkindl[top]=indl;
stkdndl[top]=dndl;
stkadd[top]=3;
num=num-1;
sndl=sndl;
temp=indl;
indl=dndl;
dndl=temp;
goto one;
three:
printf("\nMove top disk from needle %c to needle %c ",sndl,dndl);
top=top+1;
stkn[top]=num;
stksndl[top]=sndl;
stkindl[top]=indl;
stkdndl[top]=dndl;
stkadd[top]=5;
num=num-1;
temp=sndl;
sndl=indl;
indl=temp;
dndl=dndl;
goto one;
four:
if(top==NULL)
return;
num=stkn[top];
sndl=stksndl[top];
indl=stkindl[top];
dndl=stkdndl[top];
add=stkadd[top];
top=top-1;
if(add==3)
goto three;
else if(add==5)
goto four;
}
/* Recursive Function*/
void hanoiRecursion( int num,char ndl1, char ndl2, char ndl3)
{
if ( num == 1 ) {
printf( "Move top disk from needle %c to needle %c.", ndl1, ndl2 );
return;
}
hanoiRecursion( num - 1,ndl1, ndl3, ndl2 );
printf( "Move top disk from needle %c to needle %c.", ndl1, ndl2 );
hanoiRecursion( num - 1,ndl3, ndl2, ndl1 );
}
void main()
{
int no;
clrscr();
printf("Enter the no. of disks to be transferred: ");
scanf("%d",&no);
if(no<1)
printf("\nThere's nothing to move.");
else
printf("Non-Recursive");
hanoiNonRecursion(no,'A','B','C');
printf("\nRecursive");
hanoiRecursion(no,'A','B','C');
getch();
}
COMPLEMENT
/* 2’s complement of a number is obtained by scanning it from right to left and complementing all
the bits after the first appearance of a 1. Thus 2’s complement of 11100 is 00100.
Write a C program to find the 2’s complement of a binary number.*/
#include
#include
void complement (char *a);
void main()
{
char a[16];
int i;
clrscr();
printf("Enter the binary number");
gets(a);
for(i=0;a[i]!='\0';i++)
{
if(a[i]!='0' && a[i]!='1')
{
printf("NOT BINARY NUMBER");
exit(0);
}
}
complement(a);
getch();
}
void complement (char *a)
{
int l, i, c=0;
char b[16];
l=strlen(a);
for (i=l-1; i>=0; i--)
{
if (a[i]=='0')
b[i]='1';
else
b[i]='0';
}
for(i=l-1; i>=0; i--)
{
if(i==l-1)
{
if (b[i]=='0')
b[i]='1';
else
{
b[i]='0';
c=1;
}
}
else
{
if(c==1 && b[i]=='0')
{
b[i]='1';
c=0;
}
else if (c==1 && b[i]=='1')
{
b[i]='0';
c=1;
}
}
}
b[l]='\0';
printf("The 2's complement is %s", b);
}
PASCAL
/* Write a C program to generate Pascal's triangle. */
#include
#include
void main()
{
int bin,p,q,r,x;
clrscr();
bin=1;
q=0;
printf("Rows you want to input:");
scanf("%d",&r);
printf("\nPascal's Triangle:\n");
while(q
for(p=40-3*q;p>0;--p)
printf(" ");
for(x=0;x<=q;++x)
{
if((x==0)||(q==0))
bin=1;
else
bin=(bin*(q-x+1))/x;
printf("%6d",bin);
}
printf("\n");
++q;
}
getch();
}
ROM_DEC
/* Write a C program to convert a Roman numeral to its decimal equivalent. */
#include
#include
#include
#include
void main()
{
int *a,len,i,j,k;
char *rom;
clrscr();
printf("Enter the Roman Numeral:");
scanf("%s",rom);
len=strlen(rom);
for(i=0;i
if(rom[i]=='I')
a[i]=1;
else if(rom[i]=='V')
a[i]=5;
else if(rom[i]=='X')
a[i]=10;
else if(rom[i]=='L')
a[i]=50;
else if(rom[i]=='C')
a[i]=100;
else if(rom[i]=='D')
a[i]=500;
else if(rom[i]=='M')
a[i]=1000;
else
{
printf("\nInvalid Value");
getch();
exit(0);
}
}
k=a[len-1];
for(i=len-1;i>0;i--)
{
if(a[i]>a[i-1])
k=k-a[i-1];
else if(a[i]==a[i-1] || a[i] k=k+a[i-1];
}
printf("\nIts Decimal Equivalent is:");
printf("%d",k);
getch();
}
SERIES
//compute sum of geometric progression
// 1+x+x2+x3+....+xn
#include
#include
#include
void main()
{
int sum,i,x,n;
clrscr();
printf("enter values for x,n");
scanf("%d %d",&x,&n);
if(x<=0 ||n<=0)
{
printf("It is valid\n");
sum=1;
for(i=1;i<=n;i++)
{
sum=sum+pow(n,i);
}
printf("sum of series is %d\n",sum);
}
getch();
}
Sunday, April 19, 2009
EXTERNAL EXAM SCHEDULE 4R ECE 1/4
04-06-09(THURDAY) - APPLIED PHYSICS
06-06-09(SATURDAY)- EDC
08-06-09(MONDAY)-C&DS
10-06-09(WEDNESDAY)-NETWORK ANALYSIS
12-06-09(FRIDAY)-ENGLISH
15-06-09(MONDAY)-MMS
17-06-09(WEDNESDAY)-M1
NOTE:DATE OF EDP EXAM IS NOT SPECIFIED EXACTLY
06-06-09(SATURDAY)- EDC
08-06-09(MONDAY)-C&DS
10-06-09(WEDNESDAY)-NETWORK ANALYSIS
12-06-09(FRIDAY)-ENGLISH
15-06-09(MONDAY)-MMS
17-06-09(WEDNESDAY)-M1
NOTE:DATE OF EDP EXAM IS NOT SPECIFIED EXACTLY
Friday, April 17, 2009
TRTV
#include
#include
#include
#define newnode (tree_type *)malloc(sizeof(tree_type))
typedef struct tree
{
int info;
struct tree *left,*right;
}tree_type;
tree_type *insert(tree_type *t,int val)
{
if(t==NULL)
{
t=newnode;
t->info=val;
t->left=t->right=NULL;
}
else if(valinfo)
t->left=insert(t->left,val);
else if(val>t->info)
t->right=insert(t->right,val);
else
printf("\n Duplicate number");
return(t);
}
void preorder(tree_type *t)
{
if(t)
{
printf("%4d",t->info);
preorder(t->left);
preorder(t->right);
}
}
void inorder(tree_type *t)
{
if(t)
{
inorder(t->left);
printf("%4d",t->info);
inorder(t->right);
}
}
void postorder(tree_type *t)
{
if(t)
{
postorder(t->left);
postorder(t->right);
printf("%4d",t->info);
}
}
int menu()
{
int opt;
printf("\n 1.Create Tree\n 2. Insert Node\n 3. Delete Node\n");
printf("4.Search Item\n 5. Exit \nEnter Your Option");
scanf("%d",&opt);
return(opt);
}
int search(tree_type *t,int key)
{
if(t==NULL)
return NULL;
else if(keyinfo)
return(search(t->left,key));
else if(key>t->info)
return(search(t->right,key));
else
return(1);
}
tree_type *find(tree_type *t)
{
while(t->right!=NULL)
t=t->right;
return(t);
}
tree_type *Delete(tree_type *t,int val)
{
tree_type *temp,*child;
if(t==NULL)
printf("ELEMENT NOT FOUND");
else if(valinfo)
t->left=Delete(t->left,val);
else if(val > t->info)
t->right=Delete(t->right,val);
else if(t->left!=NULL && t->right!=NULL)
{
temp=find(t->left);
t->info=temp->info;
t->left=Delete(t->left,t->info);
}
else
{
temp=t;
if(t->left==NULL)
child=t->right;
if(t->right==NULL)
child=t->left;
free(temp);
return(child);
}
return(t);
}
void main()
{
tree_type *root=NULL ,*val;
int opt,num;
while(1)
{
clrscr();
printf("\n\n PREORDER :");
preorder(root);
printf("\n\n INORDER :");
inorder(root);
printf("\n\n POST ORDER :");
postorder(root);
opt=menu();
switch(opt)
{
case 1:printf("Enter value or 0 to end\n");
do
{
scanf("%d",&num);
if(num!=0)
root=insert(root,num);
}while(num!=0);
break;
case 2:printf("Enter the number :");
scanf("%d",&num);
root=insert(root,num);
break;
case 3:printf("Enter the number to br deleted:");
scanf("%d",&num);
root=Delete(root,num);
break;
case 4:printf("\n Enter Item to search:");
scanf("%d",&num);
if(search(root,num))
printf("Item %d Found\n",num);
else
printf("NOT FOUND");
getch();
break;
case 5:return;
}
}
}
#include
#include
#define newnode (tree_type *)malloc(sizeof(tree_type))
typedef struct tree
{
int info;
struct tree *left,*right;
}tree_type;
tree_type *insert(tree_type *t,int val)
{
if(t==NULL)
{
t=newnode;
t->info=val;
t->left=t->right=NULL;
}
else if(val
t->left=insert(t->left,val);
else if(val>t->info)
t->right=insert(t->right,val);
else
printf("\n Duplicate number");
return(t);
}
void preorder(tree_type *t)
{
if(t)
{
printf("%4d",t->info);
preorder(t->left);
preorder(t->right);
}
}
void inorder(tree_type *t)
{
if(t)
{
inorder(t->left);
printf("%4d",t->info);
inorder(t->right);
}
}
void postorder(tree_type *t)
{
if(t)
{
postorder(t->left);
postorder(t->right);
printf("%4d",t->info);
}
}
int menu()
{
int opt;
printf("\n 1.Create Tree\n 2. Insert Node\n 3. Delete Node\n");
printf("4.Search Item\n 5. Exit \nEnter Your Option");
scanf("%d",&opt);
return(opt);
}
int search(tree_type *t,int key)
{
if(t==NULL)
return NULL;
else if(key
return(search(t->left,key));
else if(key>t->info)
return(search(t->right,key));
else
return(1);
}
tree_type *find(tree_type *t)
{
while(t->right!=NULL)
t=t->right;
return(t);
}
tree_type *Delete(tree_type *t,int val)
{
tree_type *temp,*child;
if(t==NULL)
printf("ELEMENT NOT FOUND");
else if(val
t->left=Delete(t->left,val);
else if(val > t->info)
t->right=Delete(t->right,val);
else if(t->left!=NULL && t->right!=NULL)
{
temp=find(t->left);
t->info=temp->info;
t->left=Delete(t->left,t->info);
}
else
{
temp=t;
if(t->left==NULL)
child=t->right;
if(t->right==NULL)
child=t->left;
free(temp);
return(child);
}
return(t);
}
void main()
{
tree_type *root=NULL ,*val;
int opt,num;
while(1)
{
clrscr();
printf("\n\n PREORDER :");
preorder(root);
printf("\n\n INORDER :");
inorder(root);
printf("\n\n POST ORDER :");
postorder(root);
opt=menu();
switch(opt)
{
case 1:printf("Enter value or 0 to end\n");
do
{
scanf("%d",&num);
if(num!=0)
root=insert(root,num);
}while(num!=0);
break;
case 2:printf("Enter the number :");
scanf("%d",&num);
root=insert(root,num);
break;
case 3:printf("Enter the number to br deleted:");
scanf("%d",&num);
root=Delete(root,num);
break;
case 4:printf("\n Enter Item to search:");
scanf("%d",&num);
if(search(root,num))
printf("Item %d Found\n",num);
else
printf("NOT FOUND");
getch();
break;
case 5:return;
}
}
}
trapezoidal method //
//trapezoidal method //
#include
#include
void main()
{
int a[40],i,j,k,n;
float h,x,b=0,a1,c1;
clrscr();
printf("Enter the no of terms\n enter integral values");
scanf("%d %f %f",&n,&a1,&c1);
h=(c1-a1)/n;
printf("ENTER %d VALUES",n+1);
for(i=0;i scanf("%d",&a[i]);
for(j=0;j b+=a[j];
x=(n/2)*(a[0]+a[n])+(2*b);
printf("%f",x);
getch();
}
#include
#include
void main()
{
int a[40],i,j,k,n;
float h,x,b=0,a1,c1;
clrscr();
printf("Enter the no of terms\n enter integral values");
scanf("%d %f %f",&n,&a1,&c1);
h=(c1-a1)/n;
printf("ENTER %d VALUES",n+1);
for(i=0;i
for(j=0;j
x=(n/2)*(a[0]+a[n])+(2*b);
printf("%f",x);
getch();
}
Towers of Hanoi problem
/* Write C programs that use both recursive and non-recursive functions
To solve Towers of Hanoi problem.*/
#include
#include
/* Non-Recursive Function*/
void hanoiNonRecursion(int num,char sndl,char indl,char dndl)
{
char stkn[100],stksndl[100],stkindl[100],stkdndl[100],stkadd[100],temp;
int top,add;
top=NULL;
one:
if(num==1)
{
printf("\nMove top disk from needle %c to needle %c ",sndl,dndl);
goto four;
}
two:
top=top+1;
stkn[top]=num;
stksndl[top]=sndl;
stkindl[top]=indl;
stkdndl[top]=dndl;
stkadd[top]=3;
num=num-1;
sndl=sndl;
temp=indl;
indl=dndl;
dndl=temp;
goto one;
three:
printf("\nMove top disk from needle %c to needle %c ",sndl,dndl);
top=top+1;
stkn[top]=num;
stksndl[top]=sndl;
stkindl[top]=indl;
stkdndl[top]=dndl;
stkadd[top]=5;
num=num-1;
temp=sndl;
sndl=indl;
indl=temp;
dndl=dndl;
goto one;
four:
if(top==NULL)
return;
num=stkn[top];
sndl=stksndl[top];
indl=stkindl[top];
dndl=stkdndl[top];
add=stkadd[top];
top=top-1;
if(add==3)
goto three;
else if(add==5)
goto four;
}
/* Recursive Function*/
void hanoiRecursion( int num,char ndl1, char ndl2, char ndl3)
{
if ( num == 1 ) {
printf( "Move top disk from needle %c to needle %c.", ndl1, ndl2 );
return;
}
hanoiRecursion( num - 1,ndl1, ndl3, ndl2 );
printf( "Move top disk from needle %c to needle %c.", ndl1, ndl2 );
hanoiRecursion( num - 1,ndl3, ndl2, ndl1 );
}
void main()
{
int no;
clrscr();
printf("Enter the no. of disks to be transferred: ");
scanf("%d",&no);
if(no<1)
printf("\nThere's nothing to move.");
else
printf("Non-Recursive");
hanoiNonRecursion(no,'A','B','C');
printf("\nRecursive");
hanoiRecursion(no,'A','B','C');
getch();
}
To solve Towers of Hanoi problem.*/
#include
#include
/* Non-Recursive Function*/
void hanoiNonRecursion(int num,char sndl,char indl,char dndl)
{
char stkn[100],stksndl[100],stkindl[100],stkdndl[100],stkadd[100],temp;
int top,add;
top=NULL;
one:
if(num==1)
{
printf("\nMove top disk from needle %c to needle %c ",sndl,dndl);
goto four;
}
two:
top=top+1;
stkn[top]=num;
stksndl[top]=sndl;
stkindl[top]=indl;
stkdndl[top]=dndl;
stkadd[top]=3;
num=num-1;
sndl=sndl;
temp=indl;
indl=dndl;
dndl=temp;
goto one;
three:
printf("\nMove top disk from needle %c to needle %c ",sndl,dndl);
top=top+1;
stkn[top]=num;
stksndl[top]=sndl;
stkindl[top]=indl;
stkdndl[top]=dndl;
stkadd[top]=5;
num=num-1;
temp=sndl;
sndl=indl;
indl=temp;
dndl=dndl;
goto one;
four:
if(top==NULL)
return;
num=stkn[top];
sndl=stksndl[top];
indl=stkindl[top];
dndl=stkdndl[top];
add=stkadd[top];
top=top-1;
if(add==3)
goto three;
else if(add==5)
goto four;
}
/* Recursive Function*/
void hanoiRecursion( int num,char ndl1, char ndl2, char ndl3)
{
if ( num == 1 ) {
printf( "Move top disk from needle %c to needle %c.", ndl1, ndl2 );
return;
}
hanoiRecursion( num - 1,ndl1, ndl3, ndl2 );
printf( "Move top disk from needle %c to needle %c.", ndl1, ndl2 );
hanoiRecursion( num - 1,ndl3, ndl2, ndl1 );
}
void main()
{
int no;
clrscr();
printf("Enter the no. of disks to be transferred: ");
scanf("%d",&no);
if(no<1)
printf("\nThere's nothing to move.");
else
printf("Non-Recursive");
hanoiNonRecursion(no,'A','B','C');
printf("\nRecursive");
hanoiRecursion(no,'A','B','C');
getch();
}
displays the position or index in the string S where the string T begins, or - 1 if S doesn't contain T.
/* Write a C program that displays the position or index in the string S
where the string T begins, or - 1 if S doesn't contain T.
*/
#include
#include
#include
void main()
{
char s[30], t[20];
char *found;
clrscr();
/* Entering the main string */
puts("Enter the first string: ");
gets(s);
/* Entering the string whose position or index to be displayed */
puts("Enter the string to be searched: ");
gets(t);
/*Searching string t in string s */
found=strstr(s,t);
if(found)
printf("Second String is found in the First String at %d position.\n",found-s);
else
printf("-1");
getch();
}
where the string T begins, or - 1 if S doesn't contain T.
*/
#include
#include
#include
void main()
{
char s[30], t[20];
char *found;
clrscr();
/* Entering the main string */
puts("Enter the first string: ");
gets(s);
/* Entering the string whose position or index to be displayed */
puts("Enter the string to be searched: ");
gets(t);
/*Searching string t in string s */
found=strstr(s,t);
if(found)
printf("Second String is found in the First String at %d position.\n",found-s);
else
printf("-1");
getch();
}
to convert a Roman numeral to its decimal equivalent.
/* Write a C program to convert a Roman numeral to its decimal equivalent. */
#include
#include
#include
#include
void main()
{
int *a,len,i,j,k;
char *rom;
clrscr();
printf("Enter the Roman Numeral:");
scanf("%s",rom);
len=strlen(rom);
for(i=0;i {
if(rom[i]=='I')
a[i]=1;
else if(rom[i]=='V')
a[i]=5;
else if(rom[i]=='X')
a[i]=10;
else if(rom[i]=='L')
a[i]=50;
else if(rom[i]=='C')
a[i]=100;
else if(rom[i]=='D')
a[i]=500;
else if(rom[i]=='M')
a[i]=1000;
else
{
printf("\nInvalid Value");
getch();
exit(0);
}
}
k=a[len-1];
for(i=len-1;i>0;i--)
{
if(a[i]>a[i-1])
k=k-a[i-1];
else if(a[i]==a[i-1] || a[i] k=k+a[i-1];
}
printf("\nIts Decimal Equivalent is:");
printf("%d",k);
getch();
}
#include
#include
#include
#include
void main()
{
int *a,len,i,j,k;
char *rom;
clrscr();
printf("Enter the Roman Numeral:");
scanf("%s",rom);
len=strlen(rom);
for(i=0;i
if(rom[i]=='I')
a[i]=1;
else if(rom[i]=='V')
a[i]=5;
else if(rom[i]=='X')
a[i]=10;
else if(rom[i]=='L')
a[i]=50;
else if(rom[i]=='C')
a[i]=100;
else if(rom[i]=='D')
a[i]=500;
else if(rom[i]=='M')
a[i]=1000;
else
{
printf("\nInvalid Value");
getch();
exit(0);
}
}
k=a[len-1];
for(i=len-1;i>0;i--)
{
if(a[i]>a[i-1])
k=k-a[i-1];
else if(a[i]==a[i-1] || a[i] k=k+a[i-1];
}
printf("\nIts Decimal Equivalent is:");
printf("%d",k);
getch();
}
PUTWGETW
#include
void main()
{
FILE *fp;
int v;
clrscr();
fp=fopen("put.txt","w");
/*if(fp==NULL);
{
printf("\nfile does not exit\n");
exit();
}
else*/
printf("Enter nos\n");
while(1)
{
scanf("%d",&v);
if(v==0)
{
return;
}
putw(v,fp);
}
fclose(fp);
fp=fopen("put.txt","r");
while((v=getw(fp)!=EOF)
printf("%d",v)
fclose(fp);
}
void main()
{
FILE *fp;
int v;
clrscr();
fp=fopen("put.txt","w");
/*if(fp==NULL);
{
printf("\nfile does not exit\n");
exit();
}
else*/
printf("Enter nos\n");
while(1)
{
scanf("%d",&v);
if(v==0)
{
return;
}
putw(v,fp);
}
fclose(fp);
fp=fopen("put.txt","r");
while((v=getw(fp)!=EOF)
printf("%d",v)
fclose(fp);
}
PREV
#include
#include
//int n;
void main()
{
int rev(int *,int);
int i,n,a[20];
clrscr();
printf("enter n value:");
scanf("%d",&n);
for(i=0;i scanf("%d",&a[i]);
rev(&a[i],n);
}
rev(int *a,int n)
{
int i;
for(i=0;i {
printf("\n a[%d]=%d",n,*a);
}
return(NULL);
}
#include
//int n;
void main()
{
int rev(int *,int);
int i,n,a[20];
clrscr();
printf("enter n value:");
scanf("%d",&n);
for(i=0;i
rev(&a[i],n);
}
rev(int *a,int n)
{
int i;
for(i=0;i
printf("\n a[%d]=%d",n,*a);
}
return(NULL);
}
LAGRANGE INTERPOLATION
//LAGRANGE INTERPOLATION
#include
#include
#define max 90
void main()
{
float a[max+1],b[max+1],num,den,x,y=0;
int i,j,n;
clrscr();
printf("enter n value:");
scanf("%d",&n);
printf("Enter values of x,y");
for(i=0;i<=n;i++)
scanf("%f %f",&a[i],&b[i]);
printf("Enter value of x at which value of y is to be calculated");
scanf("%f",&x);
for(i=0;i<=n;i++)
{
num=1;
den=1;
for(j=0;j if(j!=i)
{
num*=x-a[j];
den*=a[i]-a[j];
}
y+=(num/den)*b[i];
}
printf("when x=4.1f | y=7.1f\n", x,y);
getch();
}
#include
#include
#define max 90
void main()
{
float a[max+1],b[max+1],num,den,x,y=0;
int i,j,n;
clrscr();
printf("enter n value:");
scanf("%d",&n);
printf("Enter values of x,y");
for(i=0;i<=n;i++)
scanf("%f %f",&a[i],&b[i]);
printf("Enter value of x at which value of y is to be calculated");
scanf("%f",&x);
for(i=0;i<=n;i++)
{
num=1;
den=1;
for(j=0;j
{
num*=x-a[j];
den*=a[i]-a[j];
}
y+=(num/den)*b[i];
}
printf("when x=4.1f | y=7.1f\n", x,y);
getch();
}
FUNREF
#include
void main()
{
int num1,num2;
int inter_change(int *n1,int *n2);
printf("Enter two nos\n");
scanf("%d%d",&num1,&num2);
printf("Before calling function\n");
printf("num1=%d num2=%d \n",num1,num2);
inter_change(&num1,&num2);
printf("After function is called\n");
printf("num1=%d num2=%d\n",num1,num2);
}
int inter_change(int *n1, int *n2)
{
int temp;
temp=*n1;
*n1=*n2;
*n2=temp;
return 0;
}
void main()
{
int num1,num2;
int inter_change(int *n1,int *n2);
printf("Enter two nos\n");
scanf("%d%d",&num1,&num2);
printf("Before calling function\n");
printf("num1=%d num2=%d \n",num1,num2);
inter_change(&num1,&num2);
printf("After function is called\n");
printf("num1=%d num2=%d\n",num1,num2);
}
int inter_change(int *n1, int *n2)
{
int temp;
temp=*n1;
*n1=*n2;
*n2=temp;
return 0;
}
FUNREF
#include
void main()
{
int num1,num2;
int inter_change(int *n1,int *n2);
printf("Enter two nos\n");
scanf("%d%d",&num1,&num2);
printf("Before calling function\n");
printf("num1=%d num2=%d \n",num1,num2);
inter_change(&num1,&num2);
printf("After function is called\n");
printf("num1=%d num2=%d\n",num1,num2);
}
int inter_change(int *n1, int *n2)
{
int temp;
temp=*n1;
*n1=*n2;
*n2=temp;
return 0;
}
void main()
{
int num1,num2;
int inter_change(int *n1,int *n2);
printf("Enter two nos\n");
scanf("%d%d",&num1,&num2);
printf("Before calling function\n");
printf("num1=%d num2=%d \n",num1,num2);
inter_change(&num1,&num2);
printf("After function is called\n");
printf("num1=%d num2=%d\n",num1,num2);
}
int inter_change(int *n1, int *n2)
{
int temp;
temp=*n1;
*n1=*n2;
*n2=temp;
return 0;
}
FTELL
#include
void main()
{
FILE *fp;
char ch;
clrscr();
fp=fopen("s1.txt","r");
fseek(fp,2,SEEK_SET);
ch=fgetc(fp);
while(!feof(fp))
{
printf("%c\t",ch);
printf("%d \n ",ftell(fp));
ch=fgetc(fp);
}
fclose(fp);
}
void main()
{
FILE *fp;
char ch;
clrscr();
fp=fopen("s1.txt","r");
fseek(fp,2,SEEK_SET);
ch=fgetc(fp);
while(!feof(fp))
{
printf("%c\t",ch);
printf("%d \n ",ftell(fp));
ch=fgetc(fp);
}
fclose(fp);
}
FSEEK
#include
void main()
{
FILE *fp;
int n,ch;
clrscr();
/*fp=fopen("s1.txt","w");
if(fp==NULL)
{
printf("file is not create\n");
exit();
}
printf("Enter text and press. to stop\n");
while(c!='.')
{
c=getchar();
fput(c,fp);
}
fclose(fp);*/
fp=fopen("s1.txt","r");
printf("Contents of ur file\n");
while(ch=fgetc(fp)!=EOF)
printf("%c",ch);
printf("How many u wnat to skip\n");
scanf("%d",&n);
fseek(fp,n,SEEK_SET);
printf("Information after %d skip=\n",n);
while(ch=fgetc(fp)!=EOF)
printf("%c",ch);
fclose(fp);
}
void main()
{
FILE *fp;
int n,ch;
clrscr();
/*fp=fopen("s1.txt","w");
if(fp==NULL)
{
printf("file is not create\n");
exit();
}
printf("Enter text and press. to stop\n");
while(c!='.')
{
c=getchar();
fput(c,fp);
}
fclose(fp);*/
fp=fopen("s1.txt","r");
printf("Contents of ur file\n");
while(ch=fgetc(fp)!=EOF)
printf("%c",ch);
printf("How many u wnat to skip\n");
scanf("%d",&n);
fseek(fp,n,SEEK_SET);
printf("Information after %d skip=\n",n);
while(ch=fgetc(fp)!=EOF)
printf("%c",ch);
fclose(fp);
}
FPUTFSGE
#include
#include
void main()
{
FILE *fp;
char text[50];
int i=0;
clrscr();
fp=fopen("fget1.txt","a+");
if(fp==NULL)
{
printf("File not found\n");
exit(0);
}
printf("Enter Text here\n");
/*scanf("%s",text);*/
gets(text);
fputs(text,fp);
rewind(fp);
printf("Ur file contains....\n");
if(fgets(text,50,fp)!=NULL)
/*
while(text[i]!='\0')
{
putchar(text[i]);
i++;
} */
puts(text);
getch();
}
#include
void main()
{
FILE *fp;
char text[50];
int i=0;
clrscr();
fp=fopen("fget1.txt","a+");
if(fp==NULL)
{
printf("File not found\n");
exit(0);
}
printf("Enter Text here\n");
/*scanf("%s",text);*/
gets(text);
fputs(text,fp);
rewind(fp);
printf("Ur file contains....\n");
if(fgets(text,50,fp)!=NULL)
/*
while(text[i]!='\0')
{
putchar(text[i]);
i++;
} */
puts(text);
getch();
}
reverse the first n characters in a file
/* Write a C program to reverse the first n characters in a file.
(Note: The file name and n are specified on the command line.)*/
#include
#include
#include
#include
void main(int argc, char *argv[])
{
char a[15];
char s[20];
char n;
int k;
int j=0;
int i;
int len;
FILE *fp;
if(argc!=3)
{
puts("Improper number of arguments.");
exit(0);
}
fp = fopen(argv[1],"r");
if(fp == NULL)
{
puts("File cannot be opened.");
exit(0);
}
k=*argv[2]-48;
n = fread(a,1,k,fp);
a[n]='\0';
len=strlen(a);
for(i=len-1;i>=0;i--)
{
s[j]=a[i];
printf("%c",s[j]);
j=j+1;
}
s[j+1]='\0';
getch();
}
(Note: The file name and n are specified on the command line.)*/
#include
#include
#include
#include
void main(int argc, char *argv[])
{
char a[15];
char s[20];
char n;
int k;
int j=0;
int i;
int len;
FILE *fp;
if(argc!=3)
{
puts("Improper number of arguments.");
exit(0);
}
fp = fopen(argv[1],"r");
if(fp == NULL)
{
puts("File cannot be opened.");
exit(0);
}
k=*argv[2]-48;
n = fread(a,1,k,fp);
a[n]='\0';
len=strlen(a);
for(i=len-1;i>=0;i--)
{
s[j]=a[i];
printf("%c",s[j]);
j=j+1;
}
s[j+1]='\0';
getch();
}
FILE COPY
#include
#include
void main()
{
FILE *fp1,*fp2;
char c;
int fpos;
clrscr();
fp1=fopen("d1.txt","w");
printf("write data and to stop press '.'FILE1:");
while(c!='.')
{
c=getche();
fputc(c,fp1);
}
fclose(fp1);
/*printf("\n contents read:");
fp1=fopen("d1.txt","r");
while(!feof(fp1))
{
printf("%c",getc(fp1));
}
fclose(fp1);*/
fp1=fopen("d1.txt","r");
fp2=fopen("d2.txt","a");
while(!feof(fp1))
{
c=fgetc(fp1);
fputc(c,fp2);
}
fclose(fp1);
fclose(fp2);
printf("\n contents read from file1 and file 2:");
fp2=fopen("d2.txt","r");
while(!feof(fp2))
printf("%c",getc(fp2));
fclose(fp2);
getch();
}
#include
void main()
{
FILE *fp1,*fp2;
char c;
int fpos;
clrscr();
fp1=fopen("d1.txt","w");
printf("write data and to stop press '.'FILE1:");
while(c!='.')
{
c=getche();
fputc(c,fp1);
}
fclose(fp1);
/*printf("\n contents read:");
fp1=fopen("d1.txt","r");
while(!feof(fp1))
{
printf("%c",getc(fp1));
}
fclose(fp1);*/
fp1=fopen("d1.txt","r");
fp2=fopen("d2.txt","a");
while(!feof(fp1))
{
c=fgetc(fp1);
fputc(c,fp2);
}
fclose(fp1);
fclose(fp2);
printf("\n contents read from file1 and file 2:");
fp2=fopen("d2.txt","r");
while(!feof(fp2))
printf("%c",getc(fp2));
fclose(fp2);
getch();
}
FERROR
#include
void main()
{
FILE *fp;
char name[20];int marks;
clrscr();
fp=fopen("s1.txt","r");
if(fp==NULL)
{
printf("file is not found\n");
exit();
}
printf("Enter name and marks\n");
scanf("%s%d",name,&marks);
fprintf(fp,"%s%d",name,marks);
if(ferror(fp))
{
printf(" unable to write date\n");
fclose(fp);
}
}
void main()
{
FILE *fp;
char name[20];int marks;
clrscr();
fp=fopen("s1.txt","r");
if(fp==NULL)
{
printf("file is not found\n");
exit();
}
printf("Enter name and marks\n");
scanf("%s%d",name,&marks);
fprintf(fp,"%s%d",name,marks);
if(ferror(fp))
{
printf(" unable to write date\n");
fclose(fp);
}
}
COMPLEMENT OF A BINARY NUMBER
/* 2’s complement of a number is obtained by scanning it from right to left and complementing all
the bits after the first appearance of a 1. Thus 2’s complement of 11100 is 00100.
Write a C program to find the 2’s complement of a binary number.*/
#include
#include
void complement (char *a);
void main()
{
char a[16];
int i;
clrscr();
printf("Enter the binary number");
gets(a);
for(i=0;a[i]!='\0';i++)
{
if(a[i]!='0' && a[i]!='1')
{
printf("NOT BINARY NUMBER");
exit(0);
}
}
complement(a);
getch();
}
void complement (char *a)
{
int l, i, c=0;
char b[16];
l=strlen(a);
for (i=l-1; i>=0; i--)
{
if (a[i]=='0')
b[i]='1';
else
b[i]='0';
}
for(i=l-1; i>=0; i--)
{
if(i==l-1)
{
if (b[i]=='0')
b[i]='1';
else
{
b[i]='0';
c=1;
}
}
else
{
if(c==1 && b[i]=='0')
{
b[i]='1';
c=0;
}
else if (c==1 && b[i]=='1')
{
b[i]='0';
c=1;
}
}
}
b[l]='\0';
printf("The 2's complement is %s", b);
}
the bits after the first appearance of a 1. Thus 2’s complement of 11100 is 00100.
Write a C program to find the 2’s complement of a binary number.*/
#include
#include
void complement (char *a);
void main()
{
char a[16];
int i;
clrscr();
printf("Enter the binary number");
gets(a);
for(i=0;a[i]!='\0';i++)
{
if(a[i]!='0' && a[i]!='1')
{
printf("NOT BINARY NUMBER");
exit(0);
}
}
complement(a);
getch();
}
void complement (char *a)
{
int l, i, c=0;
char b[16];
l=strlen(a);
for (i=l-1; i>=0; i--)
{
if (a[i]=='0')
b[i]='1';
else
b[i]='0';
}
for(i=l-1; i>=0; i--)
{
if(i==l-1)
{
if (b[i]=='0')
b[i]='1';
else
{
b[i]='0';
c=1;
}
}
else
{
if(c==1 && b[i]=='0')
{
b[i]='1';
c=0;
}
else if (c==1 && b[i]=='1')
{
b[i]='0';
c=1;
}
}
}
b[l]='\0';
printf("The 2's complement is %s", b);
}
i) Reading a complex number,Writing a complex number,Addition of two complex numbers,Multiplication of two complex numbers/USING STRUCTURES
/* Write a C program that uses functions to perform the following operations:
i) Reading a complex number
ii) Writing a complex number
iii) Addition of two complex numbers
iv) Multiplication of two complex numbers
(Note: represent complex number using a structure.) */
#include
#include
void arithmetic(int opern);
struct comp
{
double realpart;
double imgpart;
};
void main()
{
int opern;
clrscr();
printf("\n\n \t\t\t***** MAIN MENU *****");
printf("\n\n Select your option: \n 1 : ADD\n 2 : MULTIPLY\n 0 : EXIT \n\n\t\t Enter your Option [ ]\b\b");
scanf("%d",&opern);
switch(opern)
{
case 0:
exit(0);
case 1:
case 2:
arithmetic(opern);
default:
main();
}
}
void arithmetic(int opern)
{
struct comp w1, w2, w;
printf("\n Enter two Complex Numbers (x+iy):\n Real Part of First Number:");
scanf("%lf",&w1.realpart);
printf("\n Imaginary Part of First Number:");
scanf("%lf",&w1.imgpart);
printf("\n Real Part of Second Number:");
scanf("%lf",&w2.realpart);
printf("\n Imaginary Part of Second Number:");
scanf("%lf",&w2.imgpart);
switch(opern)
{
/*addition of complex number*/
case 1:
w.realpart = w1.realpart+w2.realpart;
w.imgpart = w1.imgpart+w2.imgpart;
break;
/*multiplication of complex number*/
case 2:
w.realpart=(w1.realpart*w2.realpart)-(w1.imgpart*w2.imgpart);
w.imgpart=(w1.realpart*w2.imgpart)+(w1.imgpart*w2.realpart);
break;
}
if (w.imgpart>0)
printf("\n Answer = %lf+%lfi",w.realpart,w.imgpart);
else
printf("\n Answer = %lf%lfi",w.realpart,w.imgpart);
getch();
main();
}
i) Reading a complex number
ii) Writing a complex number
iii) Addition of two complex numbers
iv) Multiplication of two complex numbers
(Note: represent complex number using a structure.) */
#include
#include
void arithmetic(int opern);
struct comp
{
double realpart;
double imgpart;
};
void main()
{
int opern;
clrscr();
printf("\n\n \t\t\t***** MAIN MENU *****");
printf("\n\n Select your option: \n 1 : ADD\n 2 : MULTIPLY\n 0 : EXIT \n\n\t\t Enter your Option [ ]\b\b");
scanf("%d",&opern);
switch(opern)
{
case 0:
exit(0);
case 1:
case 2:
arithmetic(opern);
default:
main();
}
}
void arithmetic(int opern)
{
struct comp w1, w2, w;
printf("\n Enter two Complex Numbers (x+iy):\n Real Part of First Number:");
scanf("%lf",&w1.realpart);
printf("\n Imaginary Part of First Number:");
scanf("%lf",&w1.imgpart);
printf("\n Real Part of Second Number:");
scanf("%lf",&w2.realpart);
printf("\n Imaginary Part of Second Number:");
scanf("%lf",&w2.imgpart);
switch(opern)
{
/*addition of complex number*/
case 1:
w.realpart = w1.realpart+w2.realpart;
w.imgpart = w1.imgpart+w2.imgpart;
break;
/*multiplication of complex number*/
case 2:
w.realpart=(w1.realpart*w2.realpart)-(w1.imgpart*w2.imgpart);
w.imgpart=(w1.realpart*w2.imgpart)+(w1.imgpart*w2.realpart);
break;
}
if (w.imgpart>0)
printf("\n Answer = %lf+%lfi",w.realpart,w.imgpart);
else
printf("\n Answer = %lf%lfi",w.realpart,w.imgpart);
getch();
main();
}
B TREE
#include
#include
#include
#include
struct node
{
int a;
struct node *left;
struct node *right;
};
struct tree *create(arr,l,h)
{
//int *arr,l,h;
struct tree *t;
int m,a;
m=(l+h)/2;
if(arr==NULL)
{
printf("\n Tree does not exist");
return(t);
}
//t= malloc(sizeof(struct tree));
//struct node *left,*right;
t->a=arr[m];
if(l>=h)
{
t->left=NULL;
t->right=NULL;
return(t);
}
if(l<=m-1)
t->left=create(arr,l,m-1);
else
t->left=NULL;
if(m+1<=h)
t->right=create(arr,m+1,h);
else
t->right=NULL;
return(t);
}
void tridis(btree,level)
{
struct tree *btree;
int level,i;
if(btree!=NULL)
{
tridis(btree->right,level+1);
printf("\n");
for(i=0;i printf(" ");
printf("\t %d",btree->a);
tridis(btree->left,level+1);
}
}
void diadis(btree,level)
{
struct tree *btree;
int level,i;
if(btree)
{
printf("\n");
for(i=0;i printf(" ");
printf("\t %d",btree->a);
diadis(btree->left,level+1);
diadis(btree->right,level+1);
}
}
void main()
{
int a[20],i,n,ch;
struct tree *x;
clrscr();
printf("enter no of elements in a list");
scanf("%d",&n);
printf("\n enter %d elements:",n);
for(i=0;i scanf("%d",&a[i]);
x=(struct tree *)malloc(sizeof(struct tree));
x=create(a,0,n-1);
clrscr();
printf("\n Binary tree\n");
printf("\nTriangular display\n");
tridis(x,1);
printf("\n Diagonal Display\n");
diadis(x,1);
getch();
}
#include
#include
#include
struct node
{
int a;
struct node *left;
struct node *right;
};
struct tree *create(arr,l,h)
{
//int *arr,l,h;
struct tree *t;
int m,a;
m=(l+h)/2;
if(arr==NULL)
{
printf("\n Tree does not exist");
return(t);
}
//t= malloc(sizeof(struct tree));
//struct node *left,*right;
t->a=arr[m];
if(l>=h)
{
t->left=NULL;
t->right=NULL;
return(t);
}
if(l<=m-1)
t->left=create(arr,l,m-1);
else
t->left=NULL;
if(m+1<=h)
t->right=create(arr,m+1,h);
else
t->right=NULL;
return(t);
}
void tridis(btree,level)
{
struct tree *btree;
int level,i;
if(btree!=NULL)
{
tridis(btree->right,level+1);
printf("\n");
for(i=0;i
printf("\t %d",btree->a);
tridis(btree->left,level+1);
}
}
void diadis(btree,level)
{
struct tree *btree;
int level,i;
if(btree)
{
printf("\n");
for(i=0;i
printf("\t %d",btree->a);
diadis(btree->left,level+1);
diadis(btree->right,level+1);
}
}
void main()
{
int a[20],i,n,ch;
struct tree *x;
clrscr();
printf("enter no of elements in a list");
scanf("%d",&n);
printf("\n enter %d elements
for(i=0;i
x=(struct tree *)malloc(sizeof(struct tree));
x=create(a,0,n-1);
clrscr();
printf("\n Binary tree\n");
printf("\nTriangular display\n");
tridis(x,1);
printf("\n Diagonal Display\n");
diadis(x,1);
getch();
}
TO DISPLAY ELEMENTS IN REVERSE ORDER
//wap to display array elements in reverse order
#include
#include
void main()
{
int a[20],n,i;
int *ptr,*big,*low;
clrscr();
printf("Enter n value");
scanf("%d",&n);
for(i=0;i scanf("%d",&a[i]);
ptr=a;
*big=0;
for(i=0;i {
if(*big<*ptr)
*big=*ptr;
if(*low>*ptr)
*low=*ptr;
ptr++;
}
printf("\n elements of array :\n");
for(i=0;i printf("%d\t",a[i]);
printf("\n Biggest value of array:%d",*big);
printf("\n Lowest value of an array : %d",*low);
getch();
}
#include
#include
void main()
{
int a[20],n,i;
int *ptr,*big,*low;
clrscr();
printf("Enter n value");
scanf("%d",&n);
for(i=0;i
ptr=a;
*big=0;
for(i=0;i
if(*big<*ptr)
*big=*ptr;
if(*low>*ptr)
*low=*ptr;
ptr++;
}
printf("\n elements of array :\n");
for(i=0;i
printf("\n Biggest value of array:%d",*big);
printf("\n Lowest value of an array : %d",*low);
getch();
}
ADDVAL
#include
void main()
{
int i=3;
int *j;
j=&i;
clrscr();
printf("Address of i= %u\n",&i);
printf("Address of j =%u\n",&j);
printf("Address of i= %u\n",&i);
printf("value of i= %d\n",i);
printf("value of j= %d\n",*j);
printf("value of i= %d\n",*(&i));
printf("value of i= %d\n",i);
getch();
}
void main()
{
int i=3;
int *j;
j=&i;
clrscr();
printf("Address of i= %u\n",&i);
printf("Address of j =%u\n",&j);
printf("Address of i= %u\n",&i);
printf("value of i= %d\n",i);
printf("value of j= %d\n",*j);
printf("value of i= %d\n",*(&i));
printf("value of i= %d\n",i);
getch();
}
LIST OF PROGRAMS
STACK USING LINKED LIST
STACK USING ARRAYS
SINGLE LINKED LIST
SERIES
QUEUE USING ARRAYS
INSERTION OF DATA USING LINKED LIST
CONSTRUCT A PYRAMID OF NUMBERS
POSTE
PASCAL'S TRIANGLE
MERGE SORT
INFIX TO POST FIX
USING CQUEUE
COMPLEX NUMBERS
C-PROGRAMS/CIRCLE LINKED LIST
STACK USING ARRAYS
SINGLE LINKED LIST
SERIES
QUEUE USING ARRAYS
INSERTION OF DATA USING LINKED LIST
CONSTRUCT A PYRAMID OF NUMBERS
POSTE
PASCAL'S TRIANGLE
MERGE SORT
INFIX TO POST FIX
USING CQUEUE
COMPLEX NUMBERS
C-PROGRAMS/CIRCLE LINKED LIST
STACK USING LINKED LIST
/*stack using linked list*/
#include
#include
#include
struct node
{
int data;
struct node *ptr;
};
typedef struct node NODE;
NODE *top=NULL,*nn,*temp;
main()
{
int ch,rpt=1;
clrscr();
while(rpt)
{
printf("1.push\n");
printf("2.pop\n");
printf("3.display\n");
printf("4.exit\n");
printf("enter your choice\n");
scanf("%d",&ch);
switch(ch)
{
case 1:push();
break;
case 2:pop();
break;
case 3:display();
break;
case 4:return;
}
printf("\nDo you want to continue press 1 for yes and 0 for no\n");
scanf("%d",&rpt);
}
}
push()
{
int num;
printf("enter the element to be inserted\n");
scanf("%d",&num);
nn=(NODE*)malloc(sizeof(NODE));
nn->data=num;
if(top==NULL)
{
nn->ptr=NULL;
top=nn;
}
else
{
nn->ptr=top;
top=nn;
}
}
pop()
{
if(top==NULL)
printf("stack is empty\n");
else
{
temp=top;
printf("Deleted element=%d",temp->data);
top=top->ptr;
free(temp);
}
}
display()
{
if(top==NULL)
printf("empty list\n");
else
{
printf("status of the stack is\n");
temp=top;
while(temp!=NULL)
{
printf("%d\n",temp->data);
temp=temp->ptr;
}
}
}
#include
#include
#include
struct node
{
int data;
struct node *ptr;
};
typedef struct node NODE;
NODE *top=NULL,*nn,*temp;
main()
{
int ch,rpt=1;
clrscr();
while(rpt)
{
printf("1.push\n");
printf("2.pop\n");
printf("3.display\n");
printf("4.exit\n");
printf("enter your choice\n");
scanf("%d",&ch);
switch(ch)
{
case 1:push();
break;
case 2:pop();
break;
case 3:display();
break;
case 4:return;
}
printf("\nDo you want to continue press 1 for yes and 0 for no\n");
scanf("%d",&rpt);
}
}
push()
{
int num;
printf("enter the element to be inserted\n");
scanf("%d",&num);
nn=(NODE*)malloc(sizeof(NODE));
nn->data=num;
if(top==NULL)
{
nn->ptr=NULL;
top=nn;
}
else
{
nn->ptr=top;
top=nn;
}
}
pop()
{
if(top==NULL)
printf("stack is empty\n");
else
{
temp=top;
printf("Deleted element=%d",temp->data);
top=top->ptr;
free(temp);
}
}
display()
{
if(top==NULL)
printf("empty list\n");
else
{
printf("status of the stack is\n");
temp=top;
while(temp!=NULL)
{
printf("%d\n",temp->data);
temp=temp->ptr;
}
}
}
STACK USING ARRAYS
#include
#include
/*stack using arrays*/
#define max 5
int top=-1;
int stack[max];
void main()
{
int rpt=1;
int ch;
clrscr();
while(rpt)
{
printf("1.push\n");
printf("2.pop\n");
printf("3.display\n");
printf("4.exit\n");
printf("enter your choice\n");
scanf("%d",&ch);
switch(ch)
{
case 1:push();
break;
case 2:pop();
break;
case 3:display();
break;
case 4:return;
}
printf("Do you want to continue press 1 for yes and 0 for no\n");
scanf("%d",&rpt);
}
}
push()
{
int item;
if(top==(max-1))
{
printf("stack is full\n");
return;
}
else
{
printf("enter the element to be inserted\n");
scanf("%d",&item);
top=top+1;
stack[top]=item;
}
}
pop()
{
if(top==-1)
{
printf("stack is empty\n");
return;
}
else
{
printf("Deleted element is %d",stack[top]);
top=top-1;
}
}
display()
{
int i;
if(top==-1)
{
printf("stack is empty\n");
return;
}
else
{
for(i=top;i>=0;i--)
printf("%d\n",stack[i]);
}
}
#include
/*stack using arrays*/
#define max 5
int top=-1;
int stack[max];
void main()
{
int rpt=1;
int ch;
clrscr();
while(rpt)
{
printf("1.push\n");
printf("2.pop\n");
printf("3.display\n");
printf("4.exit\n");
printf("enter your choice\n");
scanf("%d",&ch);
switch(ch)
{
case 1:push();
break;
case 2:pop();
break;
case 3:display();
break;
case 4:return;
}
printf("Do you want to continue press 1 for yes and 0 for no\n");
scanf("%d",&rpt);
}
}
push()
{
int item;
if(top==(max-1))
{
printf("stack is full\n");
return;
}
else
{
printf("enter the element to be inserted\n");
scanf("%d",&item);
top=top+1;
stack[top]=item;
}
}
pop()
{
if(top==-1)
{
printf("stack is empty\n");
return;
}
else
{
printf("Deleted element is %d",stack[top]);
top=top-1;
}
}
display()
{
int i;
if(top==-1)
{
printf("stack is empty\n");
return;
}
else
{
for(i=top;i>=0;i--)
printf("%d\n",stack[i]);
}
}
SINGLE LINKED LIST
#include
#include
#include
#include
struct node
{
int data;
struct node *ptr;
};
typedef struct node NODE;
NODE *head=NULL;
NODE *nn,*temp,*temp1,*temp2;
main()
{
int ch,r=1;
clrscr();
while(r)
{
printf("enter the choice\n");
printf("1.entering from the front\n");
printf("2.entering from the end\n");
printf("3.entering from the middle\n");
printf("4.deleting from the front\n");
printf("5.deleting from the end\n");
printf("6.display\n");
printf("7.deleting from the middle\n");
printf("8.exit\n");
scanf("%d",&ch);
switch(ch)
{
case 1:enterf();
break;
case 2: entere();
break;
case 3:enterm();
break;
case 4:delf();
break;
case 5:dele();
break;
case 6:display();
break;
case 7:delm();
break;
case 8:return;
}
printf("do you want to continue press 1 for yes and 0 for no\n");
scanf("%d",&r);
}
}
enterf()
{
int item;
nn=(NODE*)malloc(sizeof(NODE));
printf("enter the item to be inserted\n");
scanf("%d",&item);
if(head==NULL)
{
nn->data=item;
nn->ptr=NULL;
head=nn;
}
else
{
nn->data=item;
nn->ptr=head;
head=nn;
}
}
display()
{
if(head==NULL)
printf("empty list\n");
else
{
printf("elements in the list are\n");
temp=head;
while(temp!=NULL)
{
printf("%d->",temp->data);
temp=temp->ptr;
}
}
}
entere()
{
int item;
nn=(NODE*)malloc(sizeof(NODE));
printf("enter the item to be inserted\n");
scanf("%d",&item);
if(head==NULL)
{
nn->data=item;
nn->ptr=NULL;
head=nn;
}
else
{
temp=head;
while(temp->ptr!=NULL)
{
temp=temp->ptr;
}
nn->data=item;
temp->ptr=nn;
nn->ptr=NULL;
}
}
delf()
{
if(head==NULL)
printf("empty list\n");
else if(head->ptr==NULL)
{
printf("deleted element =%d",head->data);
free(head);
head=NULL;
}
else
{
temp=head;
head=head->ptr;
printf("deleted element=%d",temp->data);
free(temp);
}
}
dele()
{
if(head==NULL)
printf("empty list\n");
else if(head->ptr==NULL)
{
printf("deleted element=%d",head->data);
free(head);
head=NULL;
}
else
{
temp1=head;
temp2=NULL;
while(temp1->ptr!=NULL)
{
temp2=temp1;
temp1=temp1->ptr;
}
printf("deleted element=%d",temp1->data);
free(temp1);
temp2->ptr=NULL;
}
}
enterm()
{
int pos,item,i;
if(head==NULL)
{
nn=(NODE*)malloc(sizeof(NODE));
printf("enter the item to be inserted\n");
scanf("%d",&item);
nn->data=item;
nn->ptr=NULL;
head=nn;
}
else
{
printf("enter the position where you want to insert the element\n");
scanf("%d",&pos);
temp1=head;
temp2=NULL;
for(i=1;i {
temp2=temp1;
temp1=temp1->ptr;
}
printf("enter the item to be inserted\n");
scanf("%d",&item);
nn=(NODE*)malloc(sizeof(NODE));
nn->data=item;
temp2->ptr=nn;
nn->ptr=temp1;
}
}
delm()
{
int pos,i;
if(head==NULL)
{
printf("empty list\n");
}
else if(head->ptr==NULL)
{
printf("deleted element=%d",head->data);
free(head);
head=NULL;
}
else
{
printf("enter the position of the element to be deleted\n");
scanf("%d",&pos);
temp1=NULL;
temp2=head;
for(i=1;i {
temp1=temp2;
temp2=temp2->ptr;
}
printf("deleted element=%d\n",temp2->data);
temp1->ptr=temp2->ptr;
}
}
#include
#include
#include
struct node
{
int data;
struct node *ptr;
};
typedef struct node NODE;
NODE *head=NULL;
NODE *nn,*temp,*temp1,*temp2;
main()
{
int ch,r=1;
clrscr();
while(r)
{
printf("enter the choice\n");
printf("1.entering from the front\n");
printf("2.entering from the end\n");
printf("3.entering from the middle\n");
printf("4.deleting from the front\n");
printf("5.deleting from the end\n");
printf("6.display\n");
printf("7.deleting from the middle\n");
printf("8.exit\n");
scanf("%d",&ch);
switch(ch)
{
case 1:enterf();
break;
case 2: entere();
break;
case 3:enterm();
break;
case 4:delf();
break;
case 5:dele();
break;
case 6:display();
break;
case 7:delm();
break;
case 8:return;
}
printf("do you want to continue press 1 for yes and 0 for no\n");
scanf("%d",&r);
}
}
enterf()
{
int item;
nn=(NODE*)malloc(sizeof(NODE));
printf("enter the item to be inserted\n");
scanf("%d",&item);
if(head==NULL)
{
nn->data=item;
nn->ptr=NULL;
head=nn;
}
else
{
nn->data=item;
nn->ptr=head;
head=nn;
}
}
display()
{
if(head==NULL)
printf("empty list\n");
else
{
printf("elements in the list are\n");
temp=head;
while(temp!=NULL)
{
printf("%d->",temp->data);
temp=temp->ptr;
}
}
}
entere()
{
int item;
nn=(NODE*)malloc(sizeof(NODE));
printf("enter the item to be inserted\n");
scanf("%d",&item);
if(head==NULL)
{
nn->data=item;
nn->ptr=NULL;
head=nn;
}
else
{
temp=head;
while(temp->ptr!=NULL)
{
temp=temp->ptr;
}
nn->data=item;
temp->ptr=nn;
nn->ptr=NULL;
}
}
delf()
{
if(head==NULL)
printf("empty list\n");
else if(head->ptr==NULL)
{
printf("deleted element =%d",head->data);
free(head);
head=NULL;
}
else
{
temp=head;
head=head->ptr;
printf("deleted element=%d",temp->data);
free(temp);
}
}
dele()
{
if(head==NULL)
printf("empty list\n");
else if(head->ptr==NULL)
{
printf("deleted element=%d",head->data);
free(head);
head=NULL;
}
else
{
temp1=head;
temp2=NULL;
while(temp1->ptr!=NULL)
{
temp2=temp1;
temp1=temp1->ptr;
}
printf("deleted element=%d",temp1->data);
free(temp1);
temp2->ptr=NULL;
}
}
enterm()
{
int pos,item,i;
if(head==NULL)
{
nn=(NODE*)malloc(sizeof(NODE));
printf("enter the item to be inserted\n");
scanf("%d",&item);
nn->data=item;
nn->ptr=NULL;
head=nn;
}
else
{
printf("enter the position where you want to insert the element\n");
scanf("%d",&pos);
temp1=head;
temp2=NULL;
for(i=1;i
temp2=temp1;
temp1=temp1->ptr;
}
printf("enter the item to be inserted\n");
scanf("%d",&item);
nn=(NODE*)malloc(sizeof(NODE));
nn->data=item;
temp2->ptr=nn;
nn->ptr=temp1;
}
}
delm()
{
int pos,i;
if(head==NULL)
{
printf("empty list\n");
}
else if(head->ptr==NULL)
{
printf("deleted element=%d",head->data);
free(head);
head=NULL;
}
else
{
printf("enter the position of the element to be deleted\n");
scanf("%d",&pos);
temp1=NULL;
temp2=head;
for(i=1;i
temp1=temp2;
temp2=temp2->ptr;
}
printf("deleted element=%d\n",temp2->data);
temp1->ptr=temp2->ptr;
}
}
SERIES
/*Write a C program to read in two numbers, x and n, and then compute the sum of this geometric progression:
1+x+x2+x3+………….+xn For example: if n is 3 and x is 5, then the program computes 1+5+25+125.
Print x, n, the sum Perform error checking. For example, the formula does not make sense
for negative exponents - if n is less than 0. Have your program print an error message if n<0,
then go back and read in the next pair of numbers of without computing the sum.
Are any values of x also illegal ? If so, test for them too. */
#include
#include
#include
void main()
{
int s_sum,i,x,n;
clrscr();
printf("Enter the values for x and n:");
scanf("%d %d",&x,&n);
if(n<=0 || x<=0)
{
printf("Value is not valid\n");
}
else
{
printf("Value is valid\n");
s_sum=1;
for(i=1;i<=n;i++)
{
s_sum=s_sum+pow(x,i);
}
printf("Sum of series=%d\n",s_sum);
}
getch();
}
1+x+x2+x3+………….+xn For example: if n is 3 and x is 5, then the program computes 1+5+25+125.
Print x, n, the sum Perform error checking. For example, the formula does not make sense
for negative exponents - if n is less than 0. Have your program print an error message if n<0,
then go back and read in the next pair of numbers of without computing the sum.
Are any values of x also illegal ? If so, test for them too. */
#include
#include
#include
void main()
{
int s_sum,i,x,n;
clrscr();
printf("Enter the values for x and n:");
scanf("%d %d",&x,&n);
if(n<=0 || x<=0)
{
printf("Value is not valid\n");
}
else
{
printf("Value is valid\n");
s_sum=1;
for(i=1;i<=n;i++)
{
s_sum=s_sum+pow(x,i);
}
printf("Sum of series=%d\n",s_sum);
}
getch();
}
QUEUE USING ARRAYS
/* queue using arrays*/
#include
#include
#define max 5
int front=-1,rear=-1;
int q[max];
void main()
{
int rpt=1;
int ch;
clrscr();
while(rpt)
{
printf("1.qinsert\n");
printf("2.qdelete\n");
printf("3.display\n");
printf("4.exit\n");
printf("enter your choice\n");
scanf("%d",&ch);
switch(ch)
{
case 1:qinsert();
break;
case 2:qdelete();
break;
case 3:display();
break;
case 4:return;
}
printf("\nDo you want to continue press 1 for yes and 0 for no\n");
scanf("%d",&rpt);
}
}
qinsert()
{
int num;
if(rear==(max-1))
{
printf("queue is full\n");
return;
}
else
{
printf("enter the element\n");
scanf("%d",&num);
rear=rear+1;
q[rear]=num;
if(front==-1)
front=0;
}
}
qdelete()
{
int num;
if(front==-1)
{
printf("queue is empty\n");
return;
}
else
{
num=q[front];
printf("Deleted number is %d",num);
if(front==rear)
front=rear=-1;
else
front++;
}
}
display()
{
int i;
if(front==-1)
{
printf("queue is empty\n");
return;
}
else
{
for(i=front;i<=rear;i++)
{
printf("%d\n",q[i]);
}
}
}
#include
#include
#define max 5
int front=-1,rear=-1;
int q[max];
void main()
{
int rpt=1;
int ch;
clrscr();
while(rpt)
{
printf("1.qinsert\n");
printf("2.qdelete\n");
printf("3.display\n");
printf("4.exit\n");
printf("enter your choice\n");
scanf("%d",&ch);
switch(ch)
{
case 1:qinsert();
break;
case 2:qdelete();
break;
case 3:display();
break;
case 4:return;
}
printf("\nDo you want to continue press 1 for yes and 0 for no\n");
scanf("%d",&rpt);
}
}
qinsert()
{
int num;
if(rear==(max-1))
{
printf("queue is full\n");
return;
}
else
{
printf("enter the element\n");
scanf("%d",&num);
rear=rear+1;
q[rear]=num;
if(front==-1)
front=0;
}
}
qdelete()
{
int num;
if(front==-1)
{
printf("queue is empty\n");
return;
}
else
{
num=q[front];
printf("Deleted number is %d",num);
if(front==rear)
front=rear=-1;
else
front++;
}
}
display()
{
int i;
if(front==-1)
{
printf("queue is empty\n");
return;
}
else
{
for(i=front;i<=rear;i++)
{
printf("%d\n",q[i]);
}
}
}
INSERTION OF DATA USING LINKED LIST
/*insertion of data using linked list*/
#include
#include
#include
struct node
{
int data;
struct node *next;
};
typedef struct node node;
node *FRONT=NULL;
node *REAR=NULL;
void insert()
{
node *t;
int data;
printf("enter the data");
scanf("%d",&data);
t=(node *)malloc(sizeof(node));
t->data=data;
t->next=NULL;
if(REAR==NULL)
{
REAR=FRONT=t;
}
else
{
REAR->next=t;
REAR=t;
}
}
int delete()
{
int temp;
node *t;
if(FRONT==NULL)
{
puts("\n\nQ underflows");
return -999999;
}
else
{
t=FRONT;
FRONT=FRONT->next;
temp=t->data;
if(FRONT==NULL)
{
REAR=NULL;
}
free(t);
return temp;
}
}
void main()
{
int i;
clrscr();
for(i=0;i<5;i++)
insert();
for(i=0;i<5;i++)
printf("\n%d-deleted",delete());
getch();
}
#include
#include
#include
struct node
{
int data;
struct node *next;
};
typedef struct node node;
node *FRONT=NULL;
node *REAR=NULL;
void insert()
{
node *t;
int data;
printf("enter the data");
scanf("%d",&data);
t=(node *)malloc(sizeof(node));
t->data=data;
t->next=NULL;
if(REAR==NULL)
{
REAR=FRONT=t;
}
else
{
REAR->next=t;
REAR=t;
}
}
int delete()
{
int temp;
node *t;
if(FRONT==NULL)
{
puts("\n\nQ underflows");
return -999999;
}
else
{
t=FRONT;
FRONT=FRONT->next;
temp=t->data;
if(FRONT==NULL)
{
REAR=NULL;
}
free(t);
return temp;
}
}
void main()
{
int i;
clrscr();
for(i=0;i<5;i++)
insert();
for(i=0;i<5;i++)
printf("\n%d-deleted",delete());
getch();
}
CONSTRUCT A PYRAMID OF NUMBERS
/* Write a C program to construct a pyramid of numbers. */
#include
#include
void main()
{
int num,i,y,x=35;
clrscr();
printf("\nEnter the number to generate the pyramid:\n");
scanf("%d",&num);
for(y=0;y<=num;y++)
{
/*(x-coordinate,y-coordinate)*/
gotoxy(x,y+1);
/*for displaying digits towards the left and right of zero*/
for(i=0-y;i<=y;i++)
printf("%3d",abs(i));
x=x-3;
}
getch();
}
#include
#include
void main()
{
int num,i,y,x=35;
clrscr();
printf("\nEnter the number to generate the pyramid:\n");
scanf("%d",&num);
for(y=0;y<=num;y++)
{
/*(x-coordinate,y-coordinate)*/
gotoxy(x,y+1);
/*for displaying digits towards the left and right of zero*/
for(i=0-y;i<=y;i++)
printf("%3d",abs(i));
x=x-3;
}
getch();
}
POSTE
#include
#include
#include
#include
#define MAX 50
char stack[MAX] ;
char infix[MAX] ;
char postfix[MAX] ;
char eval[MAX] ;
char *s, *t ; /*pointers to input and output strings*/
int top; /*Stack top*/
/*Function Prototypes*/
void Initialize (void);
void SetExpression (char *);
char Pop (void );
void Push (char);
int priority (char);
void Convert (void);
int Evaluate(void);
void main( )
{
int m;
clrscr( ) ;
Initialize ( ) ;
printf ( "\nEnter an infix expression: " ) ;
gets ( infix ) ;
SetExpression (infix) ;
Convert( ) ;
printf ( "\nThe Postfix expression is: " ) ;
puts(postfix);
strcpy(eval,postfix);
m=Evaluate( );
printf("answer: %d", m );
getch( ) ;
}
void Initialize (void)
{
top = -1 ;/*Make stack empty*/
}
void SetExpression ( char *str )
{
s = str ;
t = postfix;
}
/* adds operator to the stack */
void Push ( char c )
{
if ( top == MAX - 1 )
printf ( "\nStack is full.\n" ) ;
else
{
top++ ;
stack[top] = c ;
}
}
/* pops an operator from the stack */
char Pop ( void )
{
if ( top == -1 ) /* Stack is empty*/
return -1 ;
else
{
char item = stack[top] ;
top-- ;
return item ;
}
}
int priority(char c)
{
if ( c == '*' || c == '/' || c == '%' )
return 2;
else if ( c == '+' || c == '-' )
return 1;
else
return 0;
}
/* converts the infix expr. to postfix form */
void Convert (void)
{
char x ;
while ( *( s ) )
{ /*Skip white spaces, if any*/
if ( *( s ) == ' ' || *( s ) == '\t' )
{
s++ ;
continue ;
}
if ( isdigit ( *( s ) ) )/*Operands*/
{
while ( isdigit ( *( s ) ) )
{
*( t ) = *( s ) ;
s++ ;
t++ ;
}
}
if ( *( s ) == '(' )/*Opening Parenthesis*/
{
Push ( *( s ) ) ;
s++ ;
}
if ( *( s ) == '*' || *( s ) == '+' || *( s ) == '/' || *( s ) == '%' || *( s )
== '-' ) /*operators*/
{
if ( top != -1 )
{
x = Pop ( ) ;
while ( priority ( x ) >= priority ( *( s ) ) )
{
*( t ) = x ;
t++ ;
x = Pop ( ) ;
}
Push( x ) ;
Push ( *( s ) ) ;
}
else Push( *( s ) ) ;
s++ ;
}
if ( *( s ) == ')' )/*Closing Parenthesis*/
{
x = Pop ( ) ;
while ( x != '(' )
{
*( t ) = x ;
t++ ;
x = Pop ( ) ;
}
s++ ;
}
}
while ( top != -1 )/*While stack is not empty*/
{
x = Pop ( ) ;
*( t ) = x ;
t++ ;
}
t++ ;
}
int Evaluate(void)
{
int i,l,a,b,q,z;
l=strlen(eval);
for(i=0;i
if ( isdigit ( eval[i] ) )
{
Push(eval[i]);
}
else if ( eval[i] == '*' || eval[i] == '+' || eval[i] == '/' || eval[i] == '%'
|| eval[i] == '-' )
{
a = Pop ( );
b = Pop ( );
switch( eval[i] )
{
case '+' : q=b+a; break;
case '-' : q=b-a; break;
case '*' : q=b*a; break;
case '/' : q=b/a; break;
}
Push ( q );
}
}
z = Pop ( );
return z;
}
PASCAL'S TRIANGLE
/* Write a C program to generate Pascal's triangle. */
#include
#include
void main()
{
int bin,p,q,r,x;
clrscr();
bin=1;
q=0;
printf("Rows you want to input:");
scanf("%d",&r);
printf("\nPascal's Triangle:\n");
while(q {
for(p=40-3*q;p>0;--p)
printf(" ");
for(x=0;x<=q;++x)
{
if((x==0)||(q==0))
bin=1;
else
bin=(bin*(q-x+1))/x;
printf("%6d",bin);
}
printf("\n");
++q;
}
getch();
}
#include
#include
void main()
{
int bin,p,q,r,x;
clrscr();
bin=1;
q=0;
printf("Rows you want to input:");
scanf("%d",&r);
printf("\nPascal's Triangle:\n");
while(q
for(p=40-3*q;p>0;--p)
printf(" ");
for(x=0;x<=q;++x)
{
if((x==0)||(q==0))
bin=1;
else
bin=(bin*(q-x+1))/x;
printf("%6d",bin);
}
printf("\n");
++q;
}
getch();
}
MERGE SORT
/* Write C program that implement the following sorting methods to sort a given list of integers in ascending order:
ii) Merge sort */
#include
#include
//#define MAX_ARY 10
void merge_sort(int x[], int end, int start);
int n;
int main(void)
{
int ary[20];
int j = 0,n;
printf("enter n value\n");
scanf("%d",&n);
printf("\n\nEnter the elements to be sorted: \n");
for(j=0;j scanf("%d",&ary[j]);
/* array before mergesort */
printf("Before :");
for(j = 0; j < n; j++)
printf(" %d", ary[j]);
printf("\n");
merge_sort(ary, 0, n - 1);
/* array after mergesort */
printf("After Merge Sort :");
for(j = 0; j < n; j++)
printf(" %d", ary[j]);
printf("\n");
getch();
}
/* Method to implement Merge Sort*/
void merge_sort(int x[], int end, int start) {
int j = 0;
const int size = start - end + 1;
int mid = 0;
int mrg1 = 0;
int mrg2 = 0;
int executing[20];
if(end == start)
return;
mid = (end + start) / 2;
merge_sort(x, end, mid);
merge_sort(x, mid + 1, start);
for(j = 0; j < size; j++)
executing[j] = x[end + j];
mrg1 = 0;
mrg2 = mid - end + 1;
for(j = 0; j < size; j++) {
if(mrg2 <= start - end)
if(mrg1 <= mid - end)
if(executing[mrg1] > executing[mrg2])
x[j + end] = executing[mrg2++];
else
x[j + end] = executing[mrg1++];
else
x[j + end] = executing[mrg2++];
else
x[j + end] = executing[mrg1++];
}
}
ii) Merge sort */
#include
#include
//#define MAX_ARY 10
void merge_sort(int x[], int end, int start);
int n;
int main(void)
{
int ary[20];
int j = 0,n;
printf("enter n value\n");
scanf("%d",&n);
printf("\n\nEnter the elements to be sorted: \n");
for(j=0;j
/* array before mergesort */
printf("Before :");
for(j = 0; j < n; j++)
printf(" %d", ary[j]);
printf("\n");
merge_sort(ary, 0, n - 1);
/* array after mergesort */
printf("After Merge Sort :");
for(j = 0; j < n; j++)
printf(" %d", ary[j]);
printf("\n");
getch();
}
/* Method to implement Merge Sort*/
void merge_sort(int x[], int end, int start) {
int j = 0;
const int size = start - end + 1;
int mid = 0;
int mrg1 = 0;
int mrg2 = 0;
int executing[20];
if(end == start)
return;
mid = (end + start) / 2;
merge_sort(x, end, mid);
merge_sort(x, mid + 1, start);
for(j = 0; j < size; j++)
executing[j] = x[end + j];
mrg1 = 0;
mrg2 = mid - end + 1;
for(j = 0; j < size; j++) {
if(mrg2 <= start - end)
if(mrg1 <= mid - end)
if(executing[mrg1] > executing[mrg2])
x[j + end] = executing[mrg2++];
else
x[j + end] = executing[mrg1++];
else
x[j + end] = executing[mrg2++];
else
x[j + end] = executing[mrg1++];
}
}
INFIX TO POST FIX
/*Infix To Postfix*/
#include
#include
struct stack
{
int top;
char item[30];
};
void push(struct stack *ps,char n)
{
ps->top++;
ps->item[ps->top]=n;
}
void pop(struct stack *ps)
{
ps->top--;
}
int isempty(struct stack *ps)
{
if(ps->top==-1)
{
return(1);
}
else
{
return(0);
}
}
int prcd(char stktop,char op)
{
if(((stktop=='+')||(stktop=='-'))&&((op=='*')||(op=='/')))
{
return(0);
}
if(stktop=='(')
{
return(0);
}
if((stktop!=')')&&(op=='('))
{
return(0);
}
if((stktop!='(')&&(op==')'))
{
return(1);
}
}
void main()
{
struct stack s;
char input[30],output[30];
char optop,ans;
int i=0,j=0,k=0;
s.top=-1;
clrscr();
printf("enter an expression in infix form:\n");
scanf("%s",input);
while(input[i]!='\0')
{
if(isalpha(input[i]))
{
output[j]=input[i];
j++;
}
else
{
while(!isempty(&s)&&(prcd(s.item[s.top],input[i])))
{
optop=s.item[s.top];
pop(&s);
output[j]=optop;
j++;
}
if(isempty(&s)||input[i]!=')')
{
push(&s,input[i]);
}
else
{
pop(&s);
}
}
i++;
}
while(!isempty(&s))
{
optop=s.item[s.top];
pop(&s);
output[j]=optop;
j++;
}
printf("\npostfix form:\t");
output[j]='\0';
while(output[k]!='\0')
{
printf("%c",output[k]);
k++;
}
scanf("%c",&ans);
getch();
}
#include
#include
struct stack
{
int top;
char item[30];
};
void push(struct stack *ps,char n)
{
ps->top++;
ps->item[ps->top]=n;
}
void pop(struct stack *ps)
{
ps->top--;
}
int isempty(struct stack *ps)
{
if(ps->top==-1)
{
return(1);
}
else
{
return(0);
}
}
int prcd(char stktop,char op)
{
if(((stktop=='+')||(stktop=='-'))&&((op=='*')||(op=='/')))
{
return(0);
}
if(stktop=='(')
{
return(0);
}
if((stktop!=')')&&(op=='('))
{
return(0);
}
if((stktop!='(')&&(op==')'))
{
return(1);
}
}
void main()
{
struct stack s;
char input[30],output[30];
char optop,ans;
int i=0,j=0,k=0;
s.top=-1;
clrscr();
printf("enter an expression in infix form:\n");
scanf("%s",input);
while(input[i]!='\0')
{
if(isalpha(input[i]))
{
output[j]=input[i];
j++;
}
else
{
while(!isempty(&s)&&(prcd(s.item[s.top],input[i])))
{
optop=s.item[s.top];
pop(&s);
output[j]=optop;
j++;
}
if(isempty(&s)||input[i]!=')')
{
push(&s,input[i]);
}
else
{
pop(&s);
}
}
i++;
}
while(!isempty(&s))
{
optop=s.item[s.top];
pop(&s);
output[j]=optop;
j++;
}
printf("\npostfix form:\t");
output[j]='\0';
while(output[k]!='\0')
{
printf("%c",output[k]);
k++;
}
scanf("%c",&ans);
getch();
}
Subscribe to:
Posts (Atom)