Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 32

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]);
}
}

6. CONVERT INFIX TO POSTFIX

#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*+

7. CONVERT INFIX TO PREFIX


#define SIZE 50 /* Size of Stack */
#include<string.h>
#include <ctype.h>
#include<stdio.h>
char s[SIZE]; int top=-1; /* Global declarations */
push(char elem)
{ /* Function for PUSH operation */
s[++top]=elem;
}
char pop()
{ /* Function for POP operation */
return(s[top--]);
}
int pr(char elem)
{ /* Function for precedence */
switch(elem)
{
case '#': return 0;
case ')': return 1;
case '+':
case '-': return 2;
case '*':
case '/':return 3;
}
}
main()
{ /* Main Program */
char infx[50],prfx[50],ch,elem;
int i=0,k=0;
printf("\n\nRead the Infix Expression ? ");
scanf("%s",infx);
push('#');
strrev(infx);
while( (ch=infx[i++]) != '\0')
{
if( ch == ')')
push(ch);

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:

8.EVALUTE POSTFIX EXPRESSION


#define SIZE 50
/* Size of Stack */
#include <ctype.h>
int s[SIZE];
int top=-1;
/* Global declarations */
push(int elem)
{
/* Function for PUSH operation */
s[++top]=elem;
}
int pop()
{
/* Function for POP operation */
return(s[top--]);
}
main()
{
/* Main Program */
char pofx[50],ch;
int i=0,op1,op2;
printf("\n\nRead the Postfix Expression ? ");
scanf("%s",pofx);
while( (ch=pofx[i++]) != '\0')
{
if(isdigit(ch)) push(ch-'0'); /* Push the operand */
else
{
/* Operator,pop two operands */
op2=pop();
op1=pop();
switch(ch)
{
case '+':push(op1+op2);break;
case '-':push(op1-op2);break;
case '*':push(op1*op2);break;
case '/':push(op1/op2);break;
}
}
}

printf("\n Given Postfix Expn: %s\n",pofx);


printf("\n Result after Evaluation: %d\n",s[top]);
}
OUTPUT:
Read the postfix Expression? +54
Given Postfix Expn: + 5 4
Result after Evaluation: 9

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;
}

/*Function to push the element on to the stack*/


void push1(int data)
{
st1[++top1] = data;
}
/*Function to pop the element from the stack*/
int pop1()
{
return(st1[top1--]);
}
/*Function to push an element on to stack*/
void push2(int data)
{
st2[++top2] = data;
}
/*Function to pop an element from th stack*/
int pop2()
{
return(st2[top2--]);
}
/*Function to add an element into the queue using stack*/
void enqueue()
{
int data, i;
printf("Enter data into queue");
scanf("%d", &data);

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

10.IMPLEMENT CIRCULAR QUEUE


#include<stdio.h>
#define max 3
int q[10],front=0,rear=-1;
void main()
{
int ch;
void insert();
void delet();
void display();
clrscr();
printf("\nCircular Queue operations\n");
printf("1.insert\n2.delete\n3.display\n4.exit\n");
while(1)
{
printf("Enter your choice:");
scanf("%d",&ch);
switch(ch)
{
case 1: insert();
break;
case 2: delet();
break;
case 3:display();
break;
case 4:exit();
default:printf("Invalid option\n");

}
}
}
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:

11. LINKED LIST


#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *next;
}*head;
void append(int num)
{
struct node *temp,*right;
temp= (struct node *)malloc(sizeof(struct node));
temp->data=num;
right=(struct node *)head;
while(right->next != NULL)
right=right->next;
right->next =temp;
right=temp;
right->next=NULL;
}
void add( int num )
{
struct node *temp;
temp=(struct node *)malloc(sizeof(struct node));
temp->data=num;
if (head== NULL)
{
head=temp;
head->next=NULL;
}
else
{
temp->next=head;
head=temp;
}

}
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){

printf("Enter only an Integer\n");


exit(0);
} else {
switch(i)
{
case 1:
printf("Enter the number to insert : ");
scanf("%d",&num);
insert(num);
break;
case 2: if(head==NULL)
{
printf("List is Empty\n");
}
else
{
printf("Element(s) in the list are : ");
}
display(n);
break;
case 3: printf("Size of the list is %d\n",count());
break;
case 4: if(head==NULL)
printf("List is Empty\n");
else{
printf("Enter the number to delete : ");
scanf("%d",&num);
if(delete(num))
printf("%d deleted successfully\n",num);
else
printf("%d not found in the list\n",num);
}
break;
case 5: return 0;
default: printf("Invalid option\n");
}
}
}
return 0;
}
#include <stdio.h>

12. BUBBLE SORT


int main()
{
int array[100], n, c, d, swap;
printf("Enter number of elements\n");
scanf("%d", &n);
printf("Enter %d integers\n", n);
for (c = 0; c < n; c++)
scanf("%d", &array[c]);
for (c = 0 ; c < ( n - 1 ); c++)
{
for (d = 0 ; d < n - c - 1; d++)
{
if (array[d] > array[d+1]) /* For decreasing order use < */
{
swap
= array[d];
array[d] = array[d+1];
array[d+1] = swap;
}
}
}
printf("Sorted list in ascending order:\n");
for ( c = 0 ; c < n ; c++ )
printf("%d\n", array[c]);
return 0;
}
OUTPUT:

You might also like