Вы находитесь на странице: 1из 106

PROGRAM 1:

//ADDITION OF TWO NUMBERS

#include<stdio.h>

#include<conio.h>

void main()

clrscr();

int a,b,c;

c=0;

printf("enter two numbers for addition");

scanf("%d",&a);

scanf("%d",&b);

c=a+b;

printf("the sum is %d",c);

getch();

OUTPUT :

enter two number for addition 10 20

the sum is 30

PROGRAM:2
//CIRCUMFERENCE AND AREA OF CIRCLE

#include<stdio.h>

int main()

int rad;

float PI = 3.14, area, ci;

printf("\nEnter radius of circle: ");

scanf("%d", &rad);

area = PI * rad * rad;

printf("\nArea of circle : %f ", area);

ci = 2 * PI * rad;

printf("\nCircumference : %f ", ci);

return (0);

OUTPUT:

Enter radius of a circle : 1

Area of circle : 3.14

Circumference : 6.28

PROGRAM –3

//SWAPPING OF TWO NUMBERS


#include<stdio.h>

#include<conio.h>

void main()

int a,b;

clrscr();

printf("enter 2 numbers to swap \n");

printf("enter number a \n");

scanf("%d",&a);

printf("enter number b\n");

scanf("%d",&b);

a=a+b;

b=a-b;

a=a-b;

printf(" swapped!!!");

printf("a=%d",a);

printf("b=%d",b);

getch();

OUTPUT:
enter 2 numbers to swap

enter number a

10

Enter number b

20

swapped! ! ! 20 10

PROGRAM 4:

//GREATEST OF THREE NUMBERS

#include<stdio.h>

#include<conio.h>

void main()

{
int a,b,c;

clrscr();

printf("enter three numbers");

scanf("%d%d%d",&a,&b,&c);

if (a>b&&a>c)

printf("\n a is greatest");

else if(b>c)

printf("\n b is greatest");

else

printf("\n c is greatest");

getch();

}
OUTPUT:

enter three numbers 10 20 30

c is greatest

PROGRAM:5

//ODD OR EVEN

#include <stdio.h>

int main()

int number;

printf("Enter an integer: ");

scanf("%d", &number);

if(number % 2 == 0)

printf("%d is even.", number);

else
printf("%d is odd.", number);

return 0;

OUTPUT 1:

Enter an integer: 7

7 is odd.

OUTPUT 2:

Enter an integer:8

8 is even

PROGRAM:6

//PROGRAM TO CHECK LEAP YEAR

#include <stdio.h>

int main()

int year;

printf("Enter a year: ");

scanf("%d",&year);

if(year%4 == 0)

if( year%100 == 0)

if ( year%400 == 0)

printf("%d is a leap year.", year);


else

printf("%d is not a leap year.", year);

else

printf("%d is a leap year.", year );

else

printf("%d is not a leap year.", year);

return 0;

OUTPUT 1:

Enter a year: 1900

1900 is not a leap year.

OUTPUT 2:

Enter a year: 2012

2012 is a leap year.


PROGRAM -7

//SUM OF N NUMBERS

#include<stdio.h>

#include<conio.h>

void main()

int i,num,sum;

clrscr();

sum=0;

printf("enter a number");

scanf("%d",&num);

for(i=0;i<=num;i++)

sum=sum+i;

printf("the sum is %d",sum);

getch();

}
OUTPUT:

enter a number5
the sum is 15

PROGRAM:8

//QUADRATIC ROOTS

#include<stdio.h>

#include<math.h>

int main()

float a,b,c;

float d,root1,root2;

printf("Enter quadratic equation in the format ax^2+bx+c: ");

scanf("%fx^2%fx%f",&a,&b,&c);

d = b * b - 4 * a * c;

if(d < 0)

printf("Roots are complex number.\n");

return 0;

root1 = ( -b + sqrt(d)) / (2* a);

root2 = ( -b - sqrt(d)) / (2* a);

printf("Roots of quadratic equation are: %.3f , %.3f",root1,root2);


return 0;

OUTPUT:

quadratic equation in the format ax^2+bx+c: 2x^2+4x+-1

Roots of quadratic equation are: 0.000, -2.000

PROGRAM:9

//SUM OF N DIGITS

#include<stdio.h>

#include<conio.h>

void main()

int n,s,r;

clrscr();

printf("enter the number");

scanf("%d",&n);

s=0;

while(n>0)

r=n%10;

s=s+r;

n=n/10;

printf("\n the sum of digits is %d",s);

getch();

}
OUTPUT:
enter the number22
the sum of digits is 4

PROGRAM:10

//PRIME NUMBER

#include <stdio.h>

int main()

int n, i, flag = 0;

printf("Enter a positive integer: ");

scanf("%d", &n);

for(i = 2; i <= n/2; ++i)

if(n%i == 0)

flag = 1;

break;

if (n == 1)

printf("1 is neither a prime nor a composite number.");

}
else

if (flag == 0)

printf("%d is a prime number.", n);

else

printf("%d is not a prime number.", n);

return 0;

OUTPUT 1:

Enter a positive integer: 29

29 is a prime number.

OUTPUT 2:

Enter a positive integer:4

4 is not a prime number.


PROGRAM:11

//ARMSTRONG NUMBER

#include<stdio.h>

#include<conio.h>

void main()

int n,t,s,r;

clrscr();

printf(“Enter the number”);

scanf(“%d”, &n);

t=n;

s=0;

while(n>0)

r=n%10;

s=s+(r*r*r);

n=n/10;

if(t==s)

printf(“\n%d is an Armstrong number”, t);


else

printf(“\n%d is not an Armstrong number”, t);

getch();

OUTPUT 1:

Enter the number : 153

153 is an Armstrong number

OUTPUT 2:

Enter the number : 154

154 is hot an Armstrong number


PROGRAM:12

//PALINDROME

#include <stdio.h>

int main()

int n, reverse = 0, t;

printf("Enter a number to check if it is a palindrome or not\n");

scanf("%d", &n);

t = n;

while (t != 0)

reverse = reverse * 10;

reverse = reverse + t%10;

t = t/10;

if (n == reverse)

printf("%d is a palindrome number.\n", n);

else

printf("%d isn't a palindrome number.\n", n);

return 0;

OUTPUT 1:
Enter a number to check if it is a palindrome or not

121

121 is a palindrome number.

OUTPUT 2:

Enter a number to check if it is a palindrome or not

122

122 is not a palindrome number.

PROGRAM 13 :

//FACTORIAL OF A NUMBER

#include<stdio.h>
#include<conio.h>

void main()

Int i,n,fact=1;

clrscr();

printf(“Enter the number:\n”);

scanf(“%d”,&n);

for(i=1;i<=n;i++)

fact=fact*i;

printf(“\n The factorial is %d”, fact);

getch();

OUTPUT:

Enter the number: 5

The factorial is 120

PROGRAM:14

//FIBONACCI SERIES

#include<conio.h>

#include<stdio.h>

void main()

int a=-1,b=1,c,i,n;

clrscr();

printf("enter the number of terms;");

scanf("%d",&n);
for(i=0;i<+n;i++)

c=a+b;

printf("\n%d",c);

a=b;

b=c;

getch();

OUTPUT :

enter the number of terms; 10

13

21
34

PROGRAM:15

//STRING PALINDROME

#include<stdio.h>

#include<string.h>

#include<conio.h>

void main()

char s[20],a[20];

clrscr();

printf("enter a string");

scanf("%s",s);

strcpy(a,strrev(s));

if(strcmp(a,strrev(s))==0)
printf("the string is palindrome");

else

printf("the string is not a palindrome");

getch();

OUTPUT 1:

enter a string madam

the sting is palindrome

OUTPUT 2:

enter a string madan

the sting is not a palindrome

PROGRAM:16 (A)

//LINEAR SEARCH

#include<stdio.h>

#include<conio.h>

void main()

Int i,n,a[10],se;

clrscr();

printf(“Get the size of an array”);

scanf(“%d”,&n);
printf(“Enter the values”);

for(i=0;i<n:i++)

scanf(“%d”,&a[i]);

printf(“Elements to be searched”);

scanf(“%d\t”,&se);

for(i=0;i<n;i++)

if(se==a[i])

printf(“%d is present at the location %d”,se,i+1);

break;

if(i==n)

printf(“\n%d is not present in the array”,se);

getch();

OUTPUT 1:

Get the size of an array : 5

Enter the values : 50, 4, 35, 60, 100

Element to be searched : 60

60 is present at the location 4


OUTPUT 2:

Get the size of an array : 5

Enter the values : 50, 4, 35, 60, 100

Element to be searched : 11

11 is not present in the array.

PROGRAM:16 (b)

//BINARY SEARCH

#include<stdio.h>

#include<conio.h>

void main()

int a[10],i,n,se,u,l,mid,c=0;

clrscr();

printf(“Enter the size of array”);

scanf(“%d”,&n);

printf(“Enter the elements of array”);

for(i=0;i<n;i++)

scanf(“%d”,&a[i]);
l=0;

u=n-1;

while(l<=u)

mid=(l+u)/2;

if(se==a[mid])

C=1;

break;

else if(se<a[mid])

u=mid-1;

else

l=mid+1;

if(c==0)

printf(“Element not found”);

else

printf(“Element found”);

}
getch();

OUTPUT 1:

Enter the size of array : 5

Enter the element of array : 20, 30, 50, 80, 90

Enter the search element : 50

Element found

OUTPUT 2:

Enter the size of array : 5

Enter the element of array : 52, 65, 53, 5, 85

Enter the search element : 90

Element not found


PROGRAM:17

//SORTING

#include<stdio.h>

#include<conio.h>

void main()

int i,j,n,temp,a[20];

clrscr();

printf("enter the size of your array");

scanf("%d",&n);

printf("enter the elements of array");

for(i=0;i<n;i++)

scanf("%d",&a[i]);

printf("your array is");

for(i=0;i<n;i++)

printf("%d",a[i]);

}
for(i=0;i<n;i++)

for(j=i+1;j<n;j++)

if(a[i]>a[j])

temp=a[i];

a[i]=a[j];

a[j]=temp;

printf("your sorted array is");

for(i=0;i<n;i++)

printf("%d \t",a[i]);

getch();

OUTPUT:

enter the size of your array 6

enter the elements of array

13 7 42 98 35 63
your sorted array is 7 13 35 42 63 98

PROGRAM:18

//MATRIX ADDITION

#include<stdio.h>

#include<conio.h>

void main()

int a[10][10],b[10][10],c[10][10],r1,r2,c1,c2,i,j;

clrscr();

printf("enter the rows and columns of first matrix");

scanf("%d %d",&r1,&c1);

printf("enter the rows and columns of second matrix");

scanf("%d %d",&r2,&c2);

printf("enter the elements of first matrix");

for(i=0;i<r1;i++)

for(j=0;j<c1;j++)

scanf("%d",&a[i][j]);

for (i=0;i<r1;i++)

for(j=0;j<c1;j++)
{

printf("%d \t",a[i][j]);

printf("\n");

printf("enter the elements of second matrix");

for(i=0;i<r2;i++)

for(j=0;j<c2;j++)

scanf("%d",&b[i][j]);

for (i=0;i<r2;i++)

for(j=0;j<c2;j++)

printf("%d \t",b[i][j]);

printf("\n");

for(i=0;i<r1;i++)

for(j=0;j<c1;j++)

c[i][j]=a[i][j]+b[i][j];
}

printf("the final matrix is ---->\n");

for (i=0;i<r1;i++)

for(j=0;j<c1;j++)

printf("%d \t",c[i][j]);

printf("\n");

getch();

OUTPUT:
enter the rows and columns of first matix2
2
enter the rows and columns of second matrix2
2
enter the elements of the first matrix1
2
3
4
enter the elements of the second matrix5
6
2
1
the final matrix is --->
6 8
5 5

PROGRAM:19

//MATRIX MULTIPLICATION

#include<stdio.h>
#include<conio.h>

void main()

int a[10][10],b[10][10],c[10][10],i,j,k,r1,c1,r2,c2;

clrscr();

printf("enter the rows and columns of matrix a and b");

scanf("%d%d%d%d",&r1,&c1,&r2,&c2);

if(c1!=r2)

printf("matrix multiplication is not possible");

printf("\n column of matrix a must be equal to row of matrix b");

else

printf("enter the elements of matrix a one by one");

for(i=0;i<r1;i++)

for(j=0;j<c1;j++)

scanf("%d",&a[i][j]);

}}

printf("enter the elements of matrix b one by one");

for(i=0;i<r2;i++)

for(j=0;j<c2;j++)

scanf("%d",&b[i][j]);

}
for(i=0;i<r1;i++)

for(j=0;j<c1;j++)

c[i][j]=0;

for(k=0;k<c1;k++)

c[i][j]=c[i][j]+a[i][k]*b[k][j];

}}

for(i=0;i<r1;i++)

for(j=0;j<c1;j++)

printf("resultant matrix is\n");

printf("%d\t",c[i][j]);

printf("\n");

getch();

OUTPUT 1:

enter the rows and columns of matrix a and b 3543

matrix multiplication is not possible

column of matrix a must be equal to row of matrix b

OUTPUT 2:
enter the rows and columns of matrix a and b 3333

enter the elements of matrix a one by one 111111111

enter the elements of matrix b one by one 222222222

resultant matrix is

6 6 6

6 6 6

6 6 6

PROGRAM:21

//CALL BY VALUE

#include<stdio.h>

#include<conio.h>

void swap(int x,int y);

void main()

int i,j;

printf("get values of i & j ");

scanf("%d %d",&i,&j);
printf("inside the main function before swapping \n");

printf("X=%d Y=%d",i,j);

swap(i,j);

printf("inside main function after swapping\n");

printf("X=%d Y=%d",i,j);

getch();

void swap(int x,inty)

int t;

t=x;

x=y;

y=t;

printf("\n swapped values X=%d, Y=%d",*x,*y);

OUTPUT:
get values of i&j10
20
inside the main function before swapping x=10 y=20
swapped values x=20,y=10
inside main function after swapping x=10 y=20
PROGRAM:22

//CALL BY REFERENCE

#include<stdio.h>

#include<conio.h>

void swap(int *x,int *y);

void main()

int i,j;

printf("get values of i & j ");

scanf("%d %d",&i,&j);

printf("inside the main function before swapping \n");

printf("X= %d, Y=%d",i,j);

swap(&i,&j);

printf("inside main function after swapping \n");


printf("X=%d Y=%d",i,j);

getch();

void swap(int *x,int*y)

int t;

t=*x;

*x=*y;

*y=t;

printf("\n swapped values X=%d, Y=%d",*x,*y);

OUTPUT:
get values of i&j10
32
inside the main function before swapping x=10 y=32
swapped values x=32,y=10
inside main function after swapping x=32 y=10
PROGRAM:23

//FACTORIAL USING RECURSIVE FUNCTION

#include<stdio.h>

#include<conio.h>

int fact(int);

void main()

int num,f;

printf("enter the number:");

scanf("%d",&num);

f=fact(num);

printf("\nfactorial of %d is %d",num,f);

getch();

int fact(int n)

if(n==1)

return 1;
else

return(n*fact(n-1));

OUTPUT:

enter a number:6

factorial of 6 is:720

PROGRAM:24

//STUDENT DATABASE USING STRUCTURE

#include<stdio.h>

#include<conio.h>

struct student

int rno,m1,m2,m3,age;

float avg;

char name[20],dept[10];

};

void main()

struct student s;

printf("enter the student details:\n");

printf("enter the student roll no.\n");

scanf("%d", &s.rno);

printf("enter the student name:\n");

scanf("%s", &s.name);

printf("enter the student dept:\n");


scanf("%s", &s.dept);

printf("enter the student age:\n");

scanf("%s", &s.age);

printf("enter the 3 marks:\n");

scanf("%d%d%d",&s.m1,&s.m2,&s.m3);

s.avg=(s.m1+s.m2+s.m3)/3;

printf("the student average is:%f\n", s.avg);

getch();

OUTPUT:

enter the student details:

enter the student roll no:

12

enter the student name:

karthik

enter the student dept:

CSE

enter the student age:

20

enter the student marks

40

18

90

the student average is :49.000000


PROGRAM:25

//UNION-BOOK DETAILS

#include<stdio.h>

#include<conio.h>

union book

char name[20];

int bookno;

float price;

void main()

union book b1;

printf("enter the book details");

printf("enter the book name \n");

scanf("%s",&b1.name);

printf("the book name is %s\n",b1.name);

printf("enter th book mo\n");

scanf("%s",&b1.bookno);

printf("the book no is %d\n",b1.bookno);

printf("enter the book price\n");

scanf("%f",&b1," price\n");

printf("the book price is %f\f",b1.price);

getch();
}

OUTPUT:
enter the book details
enter the book name
wings of fire
the book name is wings of fire
enter the bookno
22
the bookno is 22
enter the book price
250
the book price is 250

DATA STRUCTURES
PROGRAM NO : 1(a)
//array implementation of list
#include<stdio.h>
#include<conio.h>
int a[20],n,i,j,ch,elem,pos;
void create();
void insert();
void del();
void display();
void insertend();
void insertanypos();
void delend();
void delanypos();
void delanyele();
void main()
{
clrscr();
do
{
printf("\n mainmenu");
printf("\n *********");
printf("\n 1.create");
printf("\n 2.insert");
printf("\n 3.del");
printf("\n 4.display");
printf("\n 5.exit");
printf("\n enter the choice:\t");
scanf("%d",&ch);
switch(ch)
{
case 1:
{create();
break;}
case 2:
{insert();
break;}
case 3:
{del();
break;}
case 4:
{display();
break;}
case 5:
printf("\n end");
getch();
break;
}}
while(ch<5);
getch();
}
void create()
{printf("\n enter the size of the list:\t");
scanf("%d",&n);
printf("enter the elements in the list:\t");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
}
void display()
{printf("\n the elements in the list are:\t");
for(i=0;i<n;i++)
printf("%d\t",a[i]);
}
void insert()
{clrscr();
do
{
printf("\n 1.insert at the end");
printf("\n 2.insert at any pos");
printf("\n enter your choice:\t");
scanf("%d",&ch);
switch(ch)
{
case 1:
insertend();
break;
case 2:
insertanypos();
break;
}}
while(ch<3);
}
void del()
{clrscr();
do
{
printf("\n 1.delete at the end");
printf("\n 2.delete at any pos");
printf("\n 3.delete any given element");
printf("\n enter your choice:\t");
scanf("%d",&ch);
switch(ch)
{
case 1:
delend();
break;
case 2:
delanypos();
break;
case 3:
delanyele();
break;
}}
while(ch<4);
}
void insertend()
{
n=n+1;
printf("\n enter the element to be inserted:\t");
scanf("%d",&elem);
a[n-1]=elem;
display();
}
void insertanypos()
{
n=n+1;
printf("\n enter the position to be inserted:\t");
scanf("%d",&pos);
printf("\n enter the element to be inserted:\t");
scanf("%d",&elem);
for(j=n-1;j>=pos-1;j--)
a[j]=a[j-1];
a[pos-1]=elem;
display();
}
void delend()
{
elem=a[n-1];
printf("\n the delete element is %d",elem);
n=n-1;
display();
}
void delanypos()
{printf("\n enter the position to be deleted:\t");
scanf("%d",&pos);
printf("\n the deleted element is%d",a[pos-1]);
for(j=pos-1;j<n;j++)
a[j]=a[j+1];
n=n-1;
display();
}
void delanyele()
{
display();
printf("\n enter the element to be deleted:\t");
scanf("%d",&elem);
for(i=0;i<n;i++)
{
if(a[i]==elem)
{
pos=1;
for(j=pos;j<n;j++)
a[j]=a[j+1];
}}
n=n-1;
display();
}
OUTPUT :
main menu
***********
1.create
2.insert
3.delete
4.display
5.exit
Enter the choice:1
Enter the size of the list:3
Enter the elements in the list: 5 6 9
main menu
***********
1.create
2.insert
3.delete
4.display
5.exit
Enter the choice:2
1.insert at the end
2.insert at any pos
enter the choice:1
enter the elements to be inserted 7
main menu
***********
1.create
2.insert
3.delete
4.display
5.exit
Enter the choice:4
the elements in the list are 5 6 9 7
main menu
***********
1.create
2.insert
3.delete
4.display
5.exit
Enter the choice:3
1.delete at end
2.delete at any pos
3.delete any given element
enter the chioce:2
enter the position to be deleted:3
the deleted element is 9
main menu
***********
1.create
2.insert
3.delete
4.display
5.exit
Enter the choice:4
the elements in the list are 5 6 7
main menu
***********
1.create
2.insert
3.delete
4.display
5.exit
Enter the choice:5
end

PROGRAM NO: 1 (b)


//linked list implementation of list
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
struct node
{
int info;
struct node*next;
}*head=NULL;
typedef struct node NODE;
void create();
void display();
void modify();
void del();
void main()
{
int ch;
clrscr();
do
{
clrscr();
printf("main menu\n");
printf("*********");
printf("\n 1.creation");
printf("\n 2.modification");
printf("\n 3.deletion");
printf("\n 4.display");
printf("\n 5.exit");
printf("\n enter your choice:\t");
scanf("%d",&ch);
switch(ch)
{
case 1:
{create();
break;}
case 2:
{modify();
break;}
case 3:
{del();
break;}
case 4:
{display();
break;}
case 5:
printf("\nbye");
getch();
break;
}
getch();
}
while(ch<5);
}
void create()
{
NODE*temp,*t,*prev;
int i,j,ch,d,pos,n;
printf("\n enter the size of elements:\t");
scanf("%d",&n);
for(i=0;i<n;i++)
{
clrscr();
printf("\n enter the elements:\t");
scanf("%d",&d);
temp=(NODE*)malloc(sizeof(NODE));
temp->next=NULL;
temp->info=d;
if(head==NULL)
head=temp;
else
{
printf("\n 1.insert at the beginning");
printf("\n 2.insert at the mid");
printf("\n 3.insert at the end");
printf("\n enter the choice:\t");
scanf("%d",&ch);
switch(ch)
{
case 1:
{
temp->next=head;
head=temp;
break;
}
case 2:
{
t=head;
printf("\n enter the position to insert:\t");
scanf("%d",&pos);
for(j=1;j<pos;j++)
{
prev=t;
t=t->next;
}
temp->next=prev->next;
prev->next=temp;
break;
}
case 3:
{
t=head;
while(t->next!=NULL)
t=t->next;
t->next=temp;
break;
}
}
printf("\n the current list:\t");
display();
}
}
}
void modify()
{
NODE *t,*prev;
int old,neww;
t=head;
printf("\n the current list:\t");
display();
printf("\n enter the item to be modified:\t");
scanf("%d",&old);
while(t!=NULL)
{
if(t->info==old)
{
printf("\n enter the new value:\t");
scanf("%d",&neww);
t->info=neww;
}
else
{
t=t->next;
}
printf("\n no match found");
printf("\n after mofification");
display();
}
}
NODE*t,*prev;
int pos,ch,i;
t=head;
printf("\n delete at the beginning");
printf("\n delete at mid");
printf("\n delete at end");
printf("\n enter the choice");
printf("%d",&ch);
t=head;
switch(ch)
{
case 1:
{
head=head->next;
free(t);
break;
}
case 2:
{
printf("\n enter he position to delete\t");
scanf("%d",&pos);
for(i=1;i<pos;i++)
{
prev=t;
t=t->next;
}
prev->next=t->next;
free(t);
break;
}
case 3:
{
while(t->next!=NULL)
{
prev=t;
t=t->next;
}
prev->next=NULL;
free(t);
}
}
printf("\n after deletion\t");
display();
}
void display()
{
NODE*t;
t=head;
if(t==NULL)
printf("\n list is empty");
else
while(t->next!=NULL)
{
printf("%d\t",t->info);
t=t->next;
}
}
OUTPUT:
Main menu
***************
1.creation
2.modification
3.deletion
4.display
5.exit
Enter your choice : 1
Enter the size of elements : 3
Enter the elements 10
Enter the elements 15
• Insert at the beginning
• Insert at the mid
• Insert at the end
Enter the choice : 3
1.insert at the beginning
2.insert at the mid
3.insert at the end
Enter the element to be inserted 20
1.Insert at the beginning
2.Insert at the mid
3.Insert at the end
Enter your choice 3
The current list: 10 15 20

Main menu
**********
• Creation
• Modification
• Deletion
• Display
• Exit
Enter your choice : 3
• Delete at the beginning
• Delete at the mid
• Delete at the end
• Enter your choice: 3
After deletion: 10 15
Main menu
**********
• Creation
• Modification
• Deletion
• Display
• Exit
Enter your choice : 4
10 15
Main menu
*********
• Creation
• Modification
• Deletion
• Display
• Exit
Enter your choice : 5
PROGRAM N0 : 2(a)
//array implementation of stack ADT

#include<stdio.h>
#include<stdio.h>
#define SIZE 100
void main()
{
int top=-1,i,stack[SIZE],n,ch,item;
clrscr();
printf("enter the size of the stack:\n");
scanf("%d",&n);
do
{
printf("\n main menu\n");
printf("*********");
printf("\n1.push");
printf("\n2.pop");
printf("\n3.display");
printf("\n4.exit");
printf("\nenter your choice:\t");
scanf("%d",&ch);
switch(ch)
{
case 1:
if(top>=n-1)
{
printf("\n stack overflow");
getch();
break;
}
printf("\n enter the item:\t");
scanf("%d",&item);
top++;
stack[top]=item;
break;
case 2:
if(top<0)
{
printf("\n stack underflow");
getch();
break;
}
item=stack[top];
top--;
printf("the popped element is%d",item);
getch();
break;
case 3:
if(top<0)
{
printf("\nstack empty!");
getch();
break;
}
for(i=top;i>=0;i--)
printf("%d\n",stack[i]);
getch();
break;
case 4:
printf("\nbye");
getch();
break;
}
}
while(ch<4);
}

OUTPUT :
Main menu
****************
1.push
2.pop
3.display
4.exit
Enter your choice: 1
Enter the elements to be pushed : 12
Stack after pushing an element
12
Main menu
****************
1.push
2.pop
3.display
4.exit
Enter your choice: 1
Enter the elements to be pushed : 65
Stack after pushing an element
65
12
Main menu
****************
1.push
2.pop
3.display
4.exit
Enter your choice: 1
Enter the elements to be pushed : 76
Stack after pushing an element
76
65
12
Main menu
****************
1.push
2.pop
3.display
4.exit
Enter your choice: 3
76
65
12
Main menu
****************
1.push
2.pop
3.display
4.exit
Enter your choice: 2
stack after poping an element
65
12
Main menu
****************
1.push
2.pop
3.display
4.exit
Enter your choice: 2
Stack after poping an element
12
Main menu
****************
1.push
2.pop
3.display
4.exit
Enter your choice: 1
Enter the elements to be pushed : 12
Stack after pushing an element 12

Main menu
****************
1.push
2.pop
3.display
4.exit
Enter your choice: 2
Stack after popng an element
Stack is empty
Main menu
****************
1.push
2.pop
3.display
4.exit
Enter your choice: 4
Exiting !!!!
PROGRAM NO :2(B)
//linked list implementation of list
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
struct node
{
int info;
struct node*next;
}*head=NULL;
typedef struct node NODE;
void create();
void display();
void modify();
void del();
void main()
{
int ch;
clrscr();
do
{
clrscr();
printf("main menu\n");
printf("*********");
printf("\n 1.creation");
printf("\n 2.modification");
printf("\n 3.deletion");
printf("\n 4.display");
printf("\n 5.exit");
printf("\n enter your choice:\t");
scanf("%d",&ch);
switch(ch)
{
case 1:
{create();
break;}
case 2:
{modify();
break;}
case 3:
{del();
break;}
case 4:
{display();
break;}
case 5:
printf("\nbye");
getch();
break;
}
getch();
}
while(ch<5);
}
void create()
{
NODE*temp,*t,*prev;
int i,j,ch,d,pos,n;
printf("\n enter the size of elements:\t");
scanf("%d",&n);
for(i=0;i<n;i++)
{
clrscr();
printf("\n enter the elements:\t");
scanf("%d",&d);
temp=(NODE*)malloc(sizeof(NODE));
temp->next=NULL;
temp->info=d;
if(head==NULL)
head=temp;
else
{
printf("\n 1.insert at the beginning");
printf("\n 2.insert at the mid");
printf("\n 3.insert at the end");
printf("\n enter the choice:\t");
scanf("%d",&ch);
switch(ch)
{
case 1:
{
temp->next=head;
head=temp;
break;
}
case 2:
{
t=head;
printf("\n enter the position to insert:\t");
scanf("%d",&pos);
for(j=1;j<pos;j++)
{
prev=t;
t=t->next;
}
temp->next=prev->next;
prev->next=temp;
break;
}
case 3:
{
t=head;
while(t->next!=NULL)
t=t->next;
t->next=temp;
break;
}
}
printf("\n the current list:\t");
display();
}
}
}
void modify()
{
NODE *t,*prev;
int old,neww;
t=head;
printf("\n the current list:\t");
display();
printf("\n enter the item to be modified:\t");
scanf("%d",&old);
while(t!=NULL)
{
if(t->info==old)
{
printf("\n enter the new value:\t");
scanf("%d",&neww);
t->info=neww;
}
else
{
t=t->next;
}
printf("\n no match found");
printf("\n after mofification");
display();
}
}
NODE*t,*prev;
int pos,ch,i;
t=head;
printf("\n delete at the beginning");
printf("\n delete at mid");
printf("\n delete at end");
printf("\n enter the choice");
printf("%d",&ch);
t=head;
switch(ch)
{
case 1:
{
head=head->next;
free(t);
break;
}
case 2:
{
printf("\n enter he position to delete\t");
scanf("%d",&pos);
for(i=1;i<pos;i++)
{
prev=t;
t=t->next;
}
prev->next=t->next;
free(t);
break;
}
case 3:
{
while(t->next!=NULL)
{
prev=t;
t=t->next;
}
prev->next=NULL;
free(t);
}
}
printf("\n after deletion\t");
display();
}
void display()
{
NODE*t;
t=head;
if(t==NULL)
printf("\n list is empty");
else
while(t->next!=NULL)
{
printf("%d\t",t->info);
t=t->next;
}
}

OUTPUT:
Main menu
1.push
2.pop
3.display
4.edit
Enter your choice : 1
Enter the elements to be pushed : 12
Stack after pushing an element
12
Main menu
****************
1.push
2.pop
3.display
4.exit
Enter your choice: 1
Enter the elements to be pushed : 65
Stack after pushing an element
65
12
Main menu
****************
1.push
2.pop
3.display
4.exit
Enter your choice: 1
Enter the elements to be pushed : 76
Stack after pushing an element
76
65
12
Main menu
****************
1.push
2.pop
3.display
4.exit
Enter your choice: 3
76
65
12
Main menu
****************
1.push
2.pop
3.display
4.exit
Enter your choice: 2
stack after poping an element
65
12
Main menu
****************
1.push
2.pop
3.display
4.exit
Enter your choice: 2
Stack after poping an element
12
Main menu
****************
1.push
2.pop
3.display
4.exit
Enter your choice: 1
Enter the elements to be pushed : 12
Stack after pushing an element 12

Main menu
****************
1.push
2.pop
3.display
4.exit
Enter your choice: 2
Stack after popng an element
Stack is empty
Main menu
****************
1.push
2.pop
3.display
4.exit
Enter your choice: 4
Exiting !!!!

PROGRAM NO:3 (a)


//array implementation of queue
#include<stdio.h>
#include<conio.h>
#define size 100
void main()
{
int front=-1,rear=-1,i,q[size],n,ch,item;
clrscr();
printf("enter the size of queue");
scanf("%d",&n);
do
{
printf("\n main menu\n");
printf("\n *********\n");
printf("1. insert\n");
printf("2. delete\n");
printf("3. display\n");
printf("4. exit\n");
scanf("%d",&ch);
switch(ch)
{
case 1:
{
if(rear>=n-1)
{
printf("overflow");
getch();
break;
}
printf("enter the item");
scanf("%d",&item);
rear++;
q[rear]=item;
if(front==-1)
front=0;
getch();
break;
}
case 2:
{
if(front==-1)
{
printf("underflow");
}
else
{
if(front==rear)
front=rear=-1;
else
front=front++;
}
getch();
break;
}
case 3:
{
if(front==-1)
{
printf("underflow");
}
else
{
for(i=front;i<=rear;i++)
printf("%d",q[i]);
}
getch();
break;
}
}
}
while(ch<4);
getch();
}

OUTPUT:
Main menu
****************
1.insert
2.delete
3.display
4.exit
Enter your choice: 1
Enter the item 2
Main menu
****************
1.insert
2.delete
3.display
4.exit
Enter your choice: 1
Enter the item 3
Main menu
****************
1.insert
2.delete
3.display
4.exit
Enter your choice: 1
Overflow
Main menu
****************
1.insert
2.delete
3.display
4.exit
Enter your choice: 2
Main menu
****************
1.insert
2.delete
3.display
4.exit
Enter your choice: 3
2
Main menu
****************
1.insert
2.delete
3.display
4.exit
Enter your choice: 4
Exiting
PROGRAM NO : 3(B)
//linked list implementation of queue
#include<stdio.h>
#include<conio.h>
#inlcude<stdlib.h>
int d;
struct queue
{
struct queue*next;
}*front=NULL*rear=NULL;
typedef struct queue q;
void create();
void display();
void del();
void main()
{
int ch;
clrscr();
do
{
printf("main menu\n");
printf("************");
printf("\n 1.insert");
printf("\n 2.deletion");
printf("\n 3.display");
printf("\n 4.exit");
printf("\n enter your choice :\t");
scanf("%d",&ch);
switch(ch)
{
case 1:
create();
break;
case 2:
del();
break;
case 3:
display();
break;
case 4:
printf("\n exit");
break;
}
}
while (ch<4);
getch();
}
void create ()
{
q*temp,*t;
printf("\n enter the data:\t");
scanf("%d",&d);
temp=(q*)malloc(sizeof(q));
temp->data=d;
temp->next=NULL;
if(rear==NULL)
{
front=temp;
rear=temp;
}
else
{
rear->next=temp;
rear=temp;
}
void del()
{
q*t;
if(front==NULL)
printf("underflow");
else
t=front;
front=front->next;
}
void display()
{
q*t;
if(front==NULL)
printf("\n queue is underflow");
else
{
t=front;
while(t!=NULL)
{
printf("%d\t",t->data);
t=t->next;
}
}
}
Output :
Main menu
**********
1.insert
2.delete
3.display
4.exit
1
Enter the data 1
Main menu
**********
1.insert
2.delete
3.display
4.exit
1
Enter the data 2
Main menu
**********
1.insert
2.delete
3.display
4.exit
1
Enter the data 3
Main menu
**********
1.insert
2.delete
3.display
4.exit
2
Main menu
**********
1.insert
2.delete
3.display
4.exit
3
23
Main menu
**********
1.insert
2.delete
3.display
4.exit
4
Exit
PROGRAM NO: (4)
//infix to postfix conversion
#include<stdio.h>
#include<conio.h>
charinf[40],post[40];
int top=0,st[20];
void postfix();
void push(int);
char pop();
int main()
{
clrscr();
printf("\n enter the infix expression:");
scanf("%s",&inf);
postfix();
getch();
return 0;
}
void postfix()
{
inti,j=0;
for(i=0;inf[i]!='\0';i++)
{
switch(inf[i])
{
case '+':
while(st[top]>=1)
post[j++]=pop();
push(1);
break;
case '-':
while (st[top]>=1)
post[j++]=pop();
push(2);
break;
case'*':
while(st[top]>=3)
post[j++]=pop();
push(3);
break;
case '/':
while(st[top]>=3)
post[j++]=pop();
push(4);
break;
case'^':
while(st[top]>=4)
post[j++]=pop();
push(5);
break;
case '(':
push(0);
break;
case')':
while(st[top]!=0)
post[j++]=pop();
top--;
break;
default:
post[j++]=inf[i];
}
}
while(top>0)
post[j++]=pop();
printf("\n the evaluated postfix expression is :%s",post);
}
void push(int element)
{
top++;
st[top]=element;
}
char pop()
{
int e1;
char e;
e1=st[top];
top--;
switch(e1)
{
case 1:e='+';
break;
case 2:e='-';
break;
case 3:e='*';
break;
case 4:e='/';
break;
case 5:e='^';
break;
}
return e;
}

OUTPUT:
enter the infix expression:3-6(6+8/5*7)+8
the evaluated postfix expression is :36685/7*+-8+
PROGRAM NO :5
//TREE TRAVERSAL

#include <stdio.h>
#include <stdlib.h>
struct node {
int data;
struct node* left;
struct node* right;
};
void inorder(struct node* root){
if(root == NULL) return;
inorder(root->left);
printf("%d ->", root->data);
inorder(root->right);
}
void preorder(struct node* root){
if(root == NULL) return;
printf("%d ->", root->data);
preorder(root->left);
preorder(root->right);
}
void postorder(struct node* root) {
if(root == NULL) return;
postorder(root->left);
postorder(root->right);
printf("%d ->", root->data);
}
struct node* createNode(value){
struct node* newNode = malloc(sizeof(struct node));
newNode->data = value;
newNode->left = NULL;
newNode->right = NULL;
return newNode;
}
struct node* insertLeft(struct node *root, int value) {
root->left = createNode(value);
return root->left;
}
struct node* insertRight(struct node *root, int value){
root->right = createNode(value);
return root->right;
}
int main(){
struct node* root = createNode(1);
insertLeft(root, 12);
insertRight(root, 9);

insertLeft(root->left, 5);
insertRight(root->left, 6);

printf("Inorder traversal \n");


inorder(root);
printf("\nPreorder traversal \n");
preorder(root);
printf("\nPostorder traversal \n");
postorder(root);
}

OUTPUT:

Inorder traversal
5 ->12 ->6 ->1 ->9 ->
Preorder traversal
1 ->12 ->5 ->6 ->9 ->
Postorder traversal
5 ->6 ->12 ->9 ->1 ->
PROGRAM NO :6
//BINARY SEARCH TREE
#include<stdio.h>
#include<conio.h>
struct node
{
int data;
struct node*rlink;
struct node*llink;
}*tmp=NULL;
typedefstruct node NODE;
NODE*create();
void insert();
void preorder(NODE*);
voidinorder(NODE*);
voidpostorder(NODE*);
void del();
void main()
{
intn,i,m,key;
//struct node *tmp=NULL;
clrscr();
do
{
printf("\n\n0.create\n\n1.insert\n\n2.preorder\n\n3.postorder\n\n4.inorder\n\n5.exit\
n6.Delete\n");
printf("\n\nenterur choice");
scanf("%d",&m);
switch(m)
{
case 0:
tmp=create();
break;
case 1:
insert (tmp);
break;
case 2:
printf("\n\n display tree in preorde traversal\n\n");
preorder(tmp);
break;
case 3:
printf("\n\n display tree in postorder\n\n");
postorder(tmp);
break;
case 4:
printf("\n\n inorder\n\n");
inorder(tmp);
break;
case 5:
exit(0);
case 6:
{
del();break;
}
} }
while(n!=5);
getch();
}
void insert(NODE*root)
{
NODE *newnode;
if(root==NULL)
{
newnode=create();
root=newnode;
}
else
{
newnode=create();
while(1)
{
if(newnode->data<root->data)
{
if(root->llink==NULL)
{
root->llink=newnode;
break;
}
root=root->llink;
}
if(newnode->data>root->data)
{
if(root->rlink==NULL)
{
root->rlink=newnode;
break;
}
root=root->rlink;
}
}
}
}
NODE*create()
{
NODE*newnode;
int n;
newnode=(NODE*)malloc(sizeof(NODE));
printf("\n\nenter the data");
scanf("%d",&n);
newnode->data=n;
newnode->llink=NULL;
newnode->rlink=NULL;
return(newnode);
}
voidpostorder(NODE*tmp)
{
if(tmp!=NULL)
{
postorder(tmp->llink);
postorder(tmp->rlink);
printf("%d->",tmp->data);
}
}
voidinorder(NODE*tmp)
{
if(tmp!=NULL)
{
inorder(tmp->llink);
printf("%d->",tmp->data);
inorder(tmp->rlink);
}
}
void preorder(NODE*tmp)
{
if(tmp!=NULL)
{
printf("%d->",tmp->data);
preorder(tmp->llink);
preorder(tmp->rlink);
}
}
void del()
{
NODE *temp,*prev,*curr,*prev2;
intele;
printf("enter the numbers to be deleted:");
scanf("%d",&ele);
temp=tmp;
while(temp->data!=ele)
{
if(ele<=temp->data)
{
prev=temp;
temp=temp->llink;
}
else
{
prev=temp;
temp=temp->rlink;
}
}
printf("element identified %d",temp->data);
if(temp->llink==NULL && temp->rlink== NULL)
{
puts("0node");
if(temp==prev->rlink)
prev->rlink=NULL;
else
prev->llink=NULL;
}
else if((temp->llink!=NULL)&&(temp->rlink!=NULL))
{
puts("2nodes");
curr=temp->rlink;
while(curr->llink!=NULL)
{
prev2=curr;
curr=curr->llink;
}
if(temp==prev->rlink)
prev->rlink = curr;
else
prev->rlink=curr;
prev2->llink=NULL;
}
else
{
puts("1 node");
printf("%d",prev->data);
if(prev->rlink==temp)
{
if(temp->llink==NULL)
{puts("tar is right child");
prev->rlink=temp->rlink; }
else
prev->rlink=temp->llink;
}
else
{
if (temp->llink==NULL)
prev->llink=temp->rlink;
else
prev->llink=temp->llink;
}
}
}

OUTPUT :

0.create
1.insert
2.preorder
3.postorder
4.inorder
5.exit
6.delete

enter ur choice 0

enter the data 6

0.create
1.insert
2.preorder
3.postorder
4.inorder
5.exit
6.delete

enter ur choice 1

enter the data 34

0.create
1.insert
2.preorder
3.postorder
4.inorder
5.exit
6.delete

enter ur choice 1

enter the data 27

0.create
1.insert
2.preorder
3.postorder
4.inorder
5.exit
6.delete

enter ur choice 2

display tree in preorder traversal

6->34->27->

0.create
1.insert
2.preorder
3.postorder
4.inorder
5.exit
6.delete

enter ur choice 3
display tree in postorder

27->34->6->

0.create
1.insert
2.preorder
3.postorder
4.inorder
5.exit
6.delete

enter ur choice 4

inorder
6->27->34->

0.create
1.insert
2.preorder
3.postorder
4.inorder
5.exit
6.delete

Enter your choice 5


Exiting

PROGRAM : 7(A)

//INSERTION-SORT
#include<stdio.h>
#include<conio.h>
void main()
{
int a[50],i,j,n,temp;
clrscr();
printf("Get the size of the array");
scanf("%d",&n);
printf("Enter the elements in the array");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
for(i=0;i<n;i++)
{
for(j=0;j<=n-i;j++)
{
if(a[j]>a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
for(i=0;i<n;i++)
printf("%d",a[i]);
getch();
}
OUTPUT:
Get the size of the array5
Enter the elements in the array2
7
5
1
9
The sorted array is
1
2
5
7
9

PROGRAM NO : 7(B)
//BUBBLE SORT
#include<stdio.h>
#include<conio.h>
void main()
{
int I,j,temp,a[20],n;
clrscr();
printf(“get the size of the array”);
scanf(“%d”,&n);
printf(“get the element one by one “);
for(i=0;i<n;i++)
{
scanf(“%d”,&a[i]);
}
for(i=1;I,=n-1;i++)
{
temp=a[i];
for(j=I;j>=1;j--)
{
if(temp<a[j-1])
a[j]=a[j-1];
else
break;
}
a[j]=temp;
}
for(i=0;i<n;i++)
{
printf(“%d”,a[i]);
}
getch();
}

OUTPUT:
get the size of array
6
get the elements one by one
28
91
12
54
33
26
12 26 28 33 54 91

PROGRAM NO : 8(A)
//quick sort
#include<stdio.h>
#include<conio.h>
int i,j,n,pivot,a[20];
void quick(int a[],int left,int right);
void swap(int a[],int i,int j);
void main ()
{
int i,n,a[20];
clrscr();
printf(“enter the limit:”);
scanf(“%d”,&n);
printf(“enter the elements:”);
for(i=0;i<n;i++);
scanf(“%d”,a[i]);
quick(a,0,n-1);
printf(“the sorted list is:”);
for(i=0;i<n;i++)
printf(“\n %d “,a[i]);
getch();
}
void quick(int a[],int first,int last)
{
if (first<last)
{
pivot=a[first];
i=first;
j=last;
while(i<j)
{
while(a[i]<=pivot&&i<last)
i++;
while(a[j]>=pivot&&j>first)
j--;
if(i<j)
swap(a,i,j);
}
swap(a,first,j);
quick(a,first,j-1);
quick(a,j+1,last);
}}
void swap(int a[],int i,int j)
{
int temp;
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
OUTPUT:

enter the limit


6
enter the elements
28
91
12
54
33
26
12 26 28 33 54 91
PROGRAM N0 :8(b)
//MERGE SORT
#include<stdio.h>
#include<conio.h>
Void merge_split(int a[],intfirs,int last);
void merge(int a[],int f1,int l1,int f2,int l2);
int a[25],b[25];
void main()
{
inti,n;
clrscr();
printf("enter the limit:");
scanf("%d",&n);
printf("enter the elements:");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
merge_split(a,0,n-1);
printf("the sorted list is:");
for(i=0;i<n;i++)
printf("\n%d",a[i]);
getch();
}
voidmerge_split(int a[],intfirst,int last)
{
int mid;
if(first<last)
{
mid=(first+last)/2;
merge_split(a,first,mid);
merge_split(a,mid+1,last);
merge(a,first,mid,mid+1,last);
}
}
void merge(int a[],int f1,int l1,int f2,int l2)
{
inti,j,k=0;
i=f1;
j=f2;
while(i<=l1&&j<=l2)
{
if(a[i]<a[j])
b[k]=a[i++];
else
b[k]=a[j++];
k++;
}
while(i<=l1)
b[k++]=a[i++];
while(j<=l2)
b[k++]=a[j++];
i=f1;
j=0;
while(i<=l2&&j<k)
a[i++]=b[j++];
}

OUTPUT:

enter the limit


6
enter the elements
65
46
99
32
59
23
the sorted list is :
23 32 46 59 65 99

PROGRAM NO: 9
//hashing
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#define max 10
int create(int);
voidlinear_probing(int[],int,int);
void display(int[]);
void main()
{
int a[max],num,key,i;
charans;
clrscr();
printf("\nCollision handling by linear probing\n");
for(i=0;i<max;i++)
a[i]=-1;
do
{
printf("\nEnter the next element in the list:");
scanf("%d",&num);
key=create(num);
linear_probing(a,key,num);
printf("\nDo you want to continue?(y/n)");
scanf("%s",&ans);
}
while(ans=='y'||ans=='y');
if(ans=='n')
display(a);
getch();
}
int create(intnum)
{
int key=num%max;
return key;
}
voidlinear_probing(int a[max],intkey,intnum)
{
int probe;
if(a[key]==-1)
{
a[key]=num;
probe=-1;
}
else
{
if(key==max-1)
probe=0;
else
probe=key+1;
}
while((probe!=-1)&&(probe!=key))
{
if(a[probe]==-1)
{
a[probe]=num;
probe=-1;
}
else
{
if(probe==max-1)
probe=0;
else
probe++;
}
}
if(probe==-1)
printf("\nElements successfully added...");
else
printf("\nHash table is full...");
}
void display(int a[max])
{
int i;
printf("\nThe hash table is:\n");
for(i=0;i<max;i++)
printf("\na[%d]\t%d",i,a[i]);
}
OUTPUT:
collision handling by linear probing
enter the next element in the list:1
elements successfully added…..
Do you want to continue? (y/n) y
Enter the next element in the list:17
Elements successfully added…
Do you want to continue? (y/n) y
Enter the next element in the list:35
Elements successfully added…..
Do you want to continue? (y/n) y
Enter the next element in the list:87
Elements successfully added…
Do you want to continue? (y/n) y
Enter the next element in the list:90
Elements successfully added…
Do you want to continue? (y/n) y
Enter the next element in the list:66
Elements successfully added…
Do you want to continue? (y/n) n
The hash table is:
a[0] 90
a[1] 1
a[2] -1
a[3] -1
a[4] -1
a[5] 35
a[6] 66
a[7] 17
a[8] 87
a[9] -1

Вам также может понравиться