Professional Documents
Culture Documents
Copy A String To A String
Copy A String To A String
#include<stdio.h>
#include<conio.h>
#include<string.h>
void copy(char[],char[],int);
void main()
{char s1[50],s2[50];
clrscr();
printf("Enter a string:\n");
scanf("%s",s1);
copy(s1,s2,0);
printf("second string is\n%s",s2);
getch();
}
void copy(char s1[],char s2[],int i)
{s2[i]=s1[i];
if(s1[i]=='\0')
return;
copy (s1,s2,i+1);
}
MAGIC SQUARE
#include<stdio.h>
void main()
{int i,j,x,n,a[10][10];
printf("enter the size of magic square:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
a[i][j]=0;
}
i=0;j=(n-1)/2;
a[i][j]=1;
for(x=1;x<n*n;x++)
{
if(a[i-1][j-1]==0&&i-1>=0&&j-1>=0)
{i--;j--;
a[i][j]=x+1;
}
else if(a[i-1][j-1]!=0&&i-1>=0&&j-1>=0)
{
i++;
a[i][j]=x+1;
}
else if(i-1<0||j-1<0)
{ if(i==0&&j==0)
{
i=1;j=0;
}
else if(i-1<0)
{
i=i+n-1;
j--;
}
else if(j-1<0)
{
j=j+n-1;
i--;
}
a[i][j]=x+1;
}
}
for(i=0;i<n;i++)
{printf("\n");
for(j=0;j<n;j++)
printf("%d\t",a[i][j]);
}
}
Fibonacci series
#include<stdio.h>
void fib(int n)
{int a=0,b=1,c,i;
printf("%d\t%d\t",a,b);
for(i=2;i<n;i++)
{
c=a+b;
a=b;
b=c;
printf("%d\t",c);
}
}
void main()
{int n;
printf("Fseries\nEnter the no. of terms:");
scanf("%d",&n);
fib(n);
getch();
}
STACK
#include<stdio.h>
#define sz 50
struct stack
{int top;
int items[50];
}s;
void push(int x)
{if(s.top==sz-1)
{printf("overflow\n");
return;
}
s.top++;
s.items[s.top]=x;
printf("the element has been pushed\n");
}
void pop(int x)
{if(s.top==-1)
{ printf("overflow");
return;
}
x=s.items[s.top];
s.top--;
printf("the element has been poped");
}
void main()
{
int n,x;
s.top=-1;
while(1);
{
printf("enter choice\n\t1........for push\n\t2..........for pop\n");
scanf("%d",&x);
if(n==1)
{
printf("enter the no. to be pushed\n");
scanf("%d",&x);
push(x);
}
else pop(x);
}
}
TOWER OF HANOI
#include<stdio.h>
void toh(int n,char sour ,char temp ,char dest )
{if(n>0)
{
toh(n-1,sour , dest ,temp );
printf(" move disk from %c to %c\n",sour ,dest);
toh(n-1,temp,sour,dest);
}
}
void main()
{ char sour= 'A', temp='B',dest='C';
int n;
printf("\n Enter numb of disks:");
scanf("%d",&n);
printf("\n sequence of disks:\n");
toh(n,sour,temp,dest);
}
MAGIC SQUARE
#include<stdio.h>
void main()
{int i,j,x,n,a[10][10];
printf("enter the size of magic square:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
a[i][j]=0;
}
i=0;j=(n-1)/2;
a[i][j]=1;
for(x=1;x<n*n;x++)
{
if(a[i-1][j-1]==0&&i-1>=0&&j-1>=0)
{i--;j--;
a[i][j]=x+1;
}
else if(a[i-1][j-1]!=0&&i-1>=0&&j-1>=0)
{
i++;
a[i][j]=x+1;
}
else if(i-1<0||j-1<0)
{ if(i==0&&j==0)
{
i=1;j=0;
}
else if(i-1<0)
{
i=i+n-1;
j--;
}
else if(j-1<0)
{
j=j+n-1;
i--;
}
a[i][j]=x+1;
}
}
for(i=0;i<n;i++)
{printf("\n");
for(j=0;j<n;j++)
printf("%d\t",a[i][j]);
}
}
#include<stdio.h>
char stack[20];
int top = -1;
void push(char x)
{
stack[++top] = x;
}
char pop()
{
if(top == -1)
return -1;
else
return stack[top--];
}
int priority(char x)
{
if(x == '(')
return 0;
if(x == '+' || x == '-')
return 1;
if(x == '*' || x == '/')
return 2;
}
main()
{
char exp[20];
char *e, x;
printf("Enter the expression :: ");
scanf("%s",exp);
e = exp;
while(*e != '\0')
{
if(isalnum(*e))
printf("%c",*e);
else if(*e == '(')
push(*e);
else if(*e == ')')
{
while((x = pop()) != '(')
printf("%c", x);
}
else
{
while(priority(stack[top]) >= priority(*e))
printf("%c",pop());
push(*e);
}
e++;
}
while(top != -1)
{
printf("%c",pop());
}
}
OUTPUT:
Enter the expression :: a+b*c
abc*+
else if(isalnum(ch))
prfx[k++]=ch;
else if( ch == '(')
{
while( s[top] != ')')
prfx[k++]=pop();
elem=pop(); /* Remove ) */
}
else
{ /* Operator */
while( pr(s[top]) >= pr(ch) )
prfx[k++]=pop(); push(ch);
}
}
while( s[top] != '#') /* Pop from stack till empty */
prfx[k++]=pop();
prfx[k]='\0'; /* Make prfx as valid string */
strrev(prfx);
strrev(infx);
printf("\n\nGiven Infix Expn: %s \nPrefix Expn: %s\n",infx,prfx);
}
OUTPUT:
9. IMPLEMENT QUEUE
#include <stdio.h>
#include <stdlib.h>
void push1(int);
void push2(int);
int pop1();
int pop2();
void enqueue();
void dequeue();
void display();
void create();
int st1[100], st2[100];
int top1 = -1, top2 = -1;
int count = 0;
void main()
{
int ch;
printf("\n1 - Enqueue element into queue");
printf("\n2 - Dequeu element from queue");
printf("\n3 - Display from queue");
printf("\n4 - Exit");
create();
while (1)
{
printf("\nEnter choice");
scanf("%d", &ch);
switch (ch)
{
case 1:
enqueue();
break;
case 2:
dequeue();
break;
case 3:
display();
break;
case 4:
exit(0);
default:
printf("Wrong choice");
}
}
}
/*Function to create a queue*/
void create()
{
top1 = top2 = -1;
}
push1(data);
count++;
}
/*Function to delete an element from the queue using stack*/
void dequeue()
{
int i;
for (i = 0;i <= count;i++)
{
push2(pop1());
}
pop2();
count--;
for (i = 0;i <= count;i++)
{
push1(pop2());
}
}
/*Function to display the elements in the stack*/
void display()
{
int i;
for (i = 0;i <= top1;i++)
{printf(" %d ", st1[i]);
}
}
OUTPUT:
1 - Enqueue element into queue
2 - Dequeu element from queue
3 - Display from queue
4 - Exit
Enter choice1
Enter data into queue10
Enter choice1
Enter data into queue20
Enter choice1
Enter data into queue30
Enter choice1
Enter data into queue40
Enter choice3
10 20 30 40
Enter choice2
Enter choice3
20 30 40
Enter choice4
}
}
}
void insert()
{
int x;
if((front==0&&rear==max-1)||(front>0&&rear==front-1))
printf("Queue is overflow\n");
else
{
printf("Enter element to be insert:");
scanf("%d",&x);
if(rear==max-1&&front>0)
{
rear=0;
q[rear]=x;
}
else
{
if((front==0&&rear==-1)||(rear!=front-1))
q[++rear]=x;
}
}
}
void delet()
{
int a;
if((front==0)&&(rear==-1))
{
printf("Queue is underflow\n");
getch();
exit();
}
if(front==rear)
{
a=q[front];
rear=-1;
front=0;
}
else
if(front==max-1)
{
a=q[front];
front=0;
}
else a=q[front++];
printf("Deleted element is:%d\n",a);
}
void display()
{
int i,j;
if(front==0&&rear==-1)
{
printf("Queue is underflow\n");
getch();
exit();
}
if(front>rear)
{
for(i=0;i<=rear;i++)
printf("\t%d",q[i]);
for(j=front;j<=max-1;j++)
printf("\t%d",q[j]);
printf("\nrear is at %d\n",q[rear]);
printf("\nfront is at %d\n",q[front]);
}
else
{
for(i=front;i<=rear;i++)
{
printf("\t%d",q[i]);
}
printf("\nrear is at %d\n",q[rear]);
printf("\nfront is at %d\n",q[front]);
}
printf("\n");
}
getch();
OUTPUT:
}
void addafter(int num, int loc)
{
int i;
struct node *temp,*left,*right;
right=head;
for(i=1;i<loc;i++)
{
left=right;
right=right->next;
}
temp=(struct node *)malloc(sizeof(struct node));
temp->data=num;
left->next=temp;
left=temp;
left->next=right;
return;
}
void insert(int num)
{
int c=0;
struct node *temp;
temp=head;
if(temp==NULL)
{
add(num);
}
else
{
while(temp!=NULL)
{
if(temp->data<num)
c++;
temp=temp->next;
}
if(c==0)
add(num);
else if(c<count())
addafter(num,++c);
else
append(num);
}
}
int delete(int num)
{
struct node *temp, *prev;
temp=head;
while(temp!=NULL)
{
if(temp->data==num)
{
if(temp==head)
{
head=temp->next;
free(temp);
return 1;
}
else
{
prev->next=temp->next;
free(temp);
return 1;
}
}
else
{
prev=temp;
temp= temp->next;
}
}
return 0;
}
void display(struct node *r)
{
r=head;
if(r==NULL)
{
return;
}
while(r!=NULL)
{
printf("%d ",r->data);
r=r->next;
}
printf("\n");
}
int count()
{
struct node *n;
int c=0;
n=head;
while(n!=NULL)
{
n=n->next;
c++;
}
return c;
}
int main()
{
int i,num;
struct node *n;
head=NULL;
while(1)
{
printf("\nList Operations\n");
printf("===============\n");
printf("1.Insert\n");
printf("2.Display\n");
printf("3.Size\n");
printf("4.Delete\n");
printf("5.Exit\n");
printf("Enter your choice : ");
if(scanf("%d",&i)<=0){