C Program File
C Program File
C programming
and
Data Structure
AFRAZ KHAN
AFRAZ KHAN
1616028
2
INDEX
P.NO PROGRAM NAME
1. GENERATE THE FIBONACCI SERIES AND ALSO PRINT THE NUMBER
OF ELEMENT IN SERIES
2. FINDING MINIMUM AND MAXIMUM OF N NUMBER
AFRAZ KHAN
1616028
3
PROGRAM – 1
AIM- TO PRINT FIRST 10 ELEMENTS OF FIBONACCI SERIES
#include<stdio.h>
void main()
{
int x[10],i=1;
printf("ENTER THE FIRST TWO ELEMENTS OF SERIES : \nx[0]=");
scanf("%d",&x[0]);
printf("x[1]=");
scanf("%d",&x[1]);
printf("THE REST OF THE ELMENTS OF SERIES ARE : \n");
while (i<9)
{
x[i+1]=x[i]+x[i-1];
printf("x[%d]=%d\n",i+1,x[i+1]);
i++;
}
}
OUTPUT----
ENTER THE FIRST TWO ELEMENTS OF SERIES :
x[0]=0
x[1]=1
THE REST OF THE ELMENTS OF SERIES ARE :
x[2]=1
x[3]=2
x[4]=3
x[5]=5
x[6]=8
x[7]=13
x[8]=21
x[9]=34
PROGRAM-2
AIM - FINDING MINIMUM AND MAXIMUM OF n NUMBER
#include<stdio.h>
void main()
{
int a[10],i,max=0,min=10000;
printf("Enter the ten number:\n");
for(i=0;i<10;i++)
{
printf("a[%d]=",i);
scanf("%d",&a[i]);
}
for(i=0;i<10;i++)
{
if(max<a[i])
{
max=a[i];
}
if(min>a[i])
{
min=a[i];
}
}
printf("\nThe max. number is =%d\n",max);
printf("The min. number is =%d\n",min);
}
OUTPUT:
AFRAZ KHAN
1616028
4
PROGRAM – 3
AIM – CALCULATE FACTORIAL OF A GIVEN NUMBER
#include<stdio.h>
void main()
{
int a,i,n,fact=1;
printf("Enter the number a=");
scanf("%d",&a);
printf("\nEnter the number n=");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
fact=fact*i;
}
printf("\nFactorial of given number is %d\n",fact);
}
OUTPUT:
Enter the number a=6
Enter the number n=6
Factorial of given number is 720
PROGRAM - 4
AIM - FINDING THE GCD OF TWO NUMBER
#include<stdio.h>
void main()
{
int n1,n2;
printf("Enter the number n1 & n2 respectively:\n");
scanf("%d %d",&n1,&n2);
while(n1!=n2)
{
if(n1>n2)
{
n1=n1-n2;
}
else
{
n2=n2-n1;
}
}
printf("\nGCD of two number is %d\n",n1);
}
OUTPUT:
Enter the number n1 & n2 respectively:
63
14
GCD of two number is 7
AFRAZ KHAN
1616028
5
PROGRAM - 5
AIM- FINDING ALL THE ROOTS OF A QUADRATIC EQUATION
#include<stdio.h>
#include<math.h>
void main()
{
int a,b,c;
printf("Given Quadratic Equation is ax^2+bx+c=0\n");
printf("Enter the value of a,b & c respectively\n");
scanf("%d %d %d",&a,&b,&c);
double r1,r2,D;
D=(b*b-4*a*c);
r1=((-b-sqrt(D))/(2*a));
r2=((-b+sqrt(D))/(2*a));
printf("\nr1=%f r2=%f\n",r1,r2);
if(D>0)
{
printf("Roots are real and unequal\n");
}
else if(D==0)
{
printf("Roots are real and equal\n");
}
else
{
printf("roots are real and imaginary\n");
}
}
OUTPUT:
(A).Given Quadratic Equation is ax^2+bx+c=0
Enter the value of a,b & c respectively
1 -4 4
r1=2.000000 r2=2.000000
Roots are real and equal
PROGRAM - 6
AIM - GENERATE & PRINT PRIME NO. UPTO AN INTEGER N
#include<stdio.h>
void main()
{
int n,i,j,prime;
printf("n=");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
prime=0;
AFRAZ KHAN
1616028
6
for(j=2;j<i;j++)
{
if(i%j==0)
prime=1;
}
if(prime==0)
{
printf("%d\t",i);
}
}
printf("\n");
}
OUTPUT:
n=100
2 3 5 7 11 13 17 19 23
29 31 37 41 43 47 53 59 61 67
71 73 79 83 89 97
PROGRAM – 7
AIM – SORT GIVEN N NUMBERS IN ASCENDING ORDER
#include<stdio.h>
void main()
{
int a[5],i,j,t;
printf("Enter the array elements:\n");
for(i=0;i<5;i++)
{
printf("a[%d]=",i);
scanf("%d",&a[i]);
}
for(i=0;i<5;i++)
{
for(j=0;j<4;j++)
{
if(a[j]>a[j+1])
{
t=a[j+1];
a[j+1]=a[j];
a[j]=t;
}
}
}
OUTPUT:
Enter the array elements:
a[0]=90
a[1]=70
a[2]=20
a[3]=10
a[4]=30
The sorted elements are:
10
20
AFRAZ KHAN
1616028
7
30
70
90
PROGRAM-8
AIM – CALCULATE THE VALUE OF SIN(X) AND COS(X) USING
THE SERIES. ALSO PRINT SIN(X) AND COS(X) USING LIBRARY
FUNCTION
rad=(3.14/180)*deg;
x=rad;
for(i=0;i<n;i++)
{
z=factorial((2*i)+1);
sum+=(pow(-1,i)/z)*pow(x,(2*i)+1);
}
printf("\nValue of sin(%d) using sine series is %f\n",deg,sum);
m=sin(rad);
printf("\nValue of sin(%f) without using sine series is %f\n",rad,m);
}
OUTPUT:
Enter the degree=30
Enter the value of n=13
int factorial(int y)
{
int fact=1,k;
AFRAZ KHAN
1616028
8
for(k=1;k<=y;k++)
{
fact*=k;
}
return(fact);
}
void main()
{
int deg,z,n,i;
double rad,x,sum=0,m;
printf("Enter the degree=");
scanf("%d",°);
rad=(3.14/180)*deg;
x=rad;
for(i=0;i<n;i++)
{
z=factorial((2*i));
sum+=(pow(-1,i)/z)*pow(x,(2*i));
}
printf("\nValue of cos(%d) using sine series is %f\n",deg,sum);
m=cos(rad);
printf("\nValue of cos(%f) without using sine series is %f\n",rad,m);
}
OUTPUT:
Enter the degree=30
Enter the value of n=14
PROGRAM - 9
AIM- MATRICES PROGRAM
//MATRIX PROGRAM
#include<stdio.h>
void main()
{
int i,j,n,m,x,a,b,d,dgn=0;
int m1[100][100],m2[100][100],m3[100][100],m4[100][100],m5[100][100],tp[100][100];
printf("Enter the value of n=");
scanf("%d",&n);
printf("Enter the value of m=");
scanf("%d",&m);
printf("MATRIX 1:\n");
for(i=0;i<n;++i)
{
printf("Enter row number=%d",i);
for(j=0;j<m;++j)
{
scanf("%d",&m1[i][j]);
}
printf("\n");
}
AFRAZ KHAN
1616028
9
else
{
printf("\n NO ADDTION WOULD TAKE PLACE:");
}
break;
AFRAZ KHAN
1616028
10
else
{
printf("\nNO SUBTRACTION WOULD TAKE PLACE:");
}
break;
for(i=0;i<m;++i)
{
for(j=0;j<n;++j)
{
printf("%d\t",tp[i][j]);
}
printf("\n");
}
break;
for(i=0;i<n;++i)
{
for(j=0;j<b;++j)
{
AFRAZ KHAN
1616028
11
printf("%d\t",m5[i][j]);
}
printf("\n");
}
}
else
{
printf("As column of 1st matrix is not equal to column of 2nd matrix,
so the multipliction would npt be exist.\n ");
}
break;
MATRIX 2:
Enter value of a=2
Enter value of b=2
Enter row number=0
2
2
Enter row number=1
3
3
DISPLAY THE MATRIX 2:
AFRAZ KHAN
1616028
12
2 2
3 3
Enter the Menu:
1:MATRIX ADDITION
2:MATRIX SUBTRACTION
3:TRANSPOSE OF MATRIX
4:MATRIX MULTIPLICATION
5:SUM OF DIAGONAL ELEMENT
PROGRAM – 10
AIM- CALCULATE THE SUBJECT WISE & STUDENT WISE
TOTALS & STORE THEM AS A PART OF THE STRUCTURE
#include<stdio.h>
struct college
{
char name[20];
int roll_no;
AFRAZ KHAN
1616028
13
int marks[5];
}student[5];
void main()
{
int i,j,sum;
float avg;
for(i=0;i<5;i++)
{
printf("Student name:");
scanf("%s",&student[i].name);
printf("Roll no: ");
scanf("%d",&student[i].roll_no);
printf("Marks obtained:\n");
sum=0;
for(j=0;j<5;j++)
{
scanf("%d",&student[j].marks[j]);
sum+=student[j].marks[j];
}
printf("\nSum=%d",sum);
avg=(sum/5);
printf("\nThe Average Marks is %f",avg);
if(avg>=90)
{
printf("\nGrade obtained is O");
}
else if(avg<=89&&avg>=70)
{
printf("\nGrade obtained is A");
}
else if(avg<=69&&avg>=50)
{
printf("\nGrade obtained is B");
}
else if(avg<=49&&avg>=33)
{
printf("\nGrade obtained is C");
}
else
{
printf("\nGrade obtained is E and student is fail");
}
printf("\n\n");
}
}
OUTPUT:
(A).Student name:Avaneesh_Awasthi
Roll no: 1616017
Marks obtained:
90
99
95
91
92
Sum=467
The Average Marks is 93.000000
Grade obtained is O
(B).Student name:pranav_pandey
Roll no: 1616018
Marks obtained:
90
65
AFRAZ KHAN
1616028
14
87
77
81
Sum=400
The Average Marks is 80.000000
Grade obtained is A
(C).Student name:Mayank_aggrawal
Roll no: 1616002
Marks obtained:
100
44
54
40
46
Sum=284
The Average Marks is 56.000000
Grade obtained is B
(D).Student name:kamal_nayan
Roll no: 1616020
Marks obtained:
45
44
43
41
42
Sum=215
The Average Marks is 43.000000
Grade obtained is C
PROGRAM-10(A)
TYPE OF LINK LISTS:-
(A):- single linked list
#include<stdio.h>
#include<stdlib.h>
#define NULL 0
struct node
{
int item;
struct node *next;
};
void create(node *);
void display(node *);
node *insert(node *);
node *remove(node *);
void main()
{
int choice;
node *start;
AFRAZ KHAN
1616028
15
do
{
printf("\nEnter the choice\n\nMenu:\n");
printf("\n1:Create\n2:Display\n3:Insert\n4:Delete\n5:End\n");
scanf("%d",&choice);
switch(choice)
{
case 1: printf("\nTo create single linked list:\n");
start=(node *)malloc(sizeof(node));
create(start);
break;
case 2:printf("\nTo display single linked list:\n");
display(start);
break;
case 3: start=insert(start);
printf("The new list after inserting of data is:\n");
display(start);
break;
case 4:start=remove(start);
printf("The new list after deleting of data is\n");
display(start);
break;
}
}while(choice<=4);
printf("\nEND\n");
}
void create(node *record)
{
scanf("\n%d",&record->item);
if(record->item==0)
{
record->next=NULL;
return;
}
else
{
record->next=(node *)malloc(sizeof(node));
record=record->next;
create(record);
}
}
void display(node *record)
{
printf("\n%d",record->item);
if(record->next==NULL)
return;
else
record=record->next;
display(record);
}
node *insert(node *record)
{
node *first,*ptr;
ptr=(node*)malloc(sizeof(node));
int choice,x,y;
printf("Menu for insertion of data:\n");
printf("1:Insert at the beginning\n2:Insert anywhere:\n");
printf("Enter the choice:\n\n");
scanf("%d",&choice);
switch(choice)
{
case 1: printf("Insert at the beginning:\n");
first=(node*)malloc(sizeof(node));
AFRAZ KHAN
1616028
16
switch(choice)
{
case 1: printf("delete at the beginning:\n");
first=record;
record=record->next;
break;
case 2: printf("Delete anywhere:\n");
printf("after what to delete:\n");
scanf("%d",&y);
first=record;
while(record->item!=y)
record=record->next;
record->next=record->next->next;
return(first);
break;
}
return(record);
}
OUTPUT:
Enter the choice:
Menu:
1:Create
2:Display
3:Insert
4:Delete
5:End
1
To create single linked list:
9
2
3
4
5
6
0
AFRAZ KHAN
1616028
17
12
AFRAZ KHAN
1616028
18
9
2
3
4
5
6
7
0
Enter the choice
Menu:
1:Create
2:Display
3:Insert
4:Delete
5:End
4
Menu for deletion of data:
1:Delete 1st node
2:Delete anywhere:
Enter the choice:
1
delete at the beginning:
The new list after deleting of data is
9
2
3
4
5
6
7
0
Enter the choice
Menu:
1:Create
2:Display
3:Insert
4:Delete
5:End
4
Menu for deletion of data:
1:Delete 1st node
2:Delete anywhere:
Enter the choice:
2
Delete anywhere:
after what to delete:
6
The new list after deleting of data is
9
2
3
4
5
6
0
Enter the choice
Menu:
1:Create
2:Display
3:Insert
4:Delete
5:End
AFRAZ KHAN
1616028
19
case 3:start=insert(start);
printf("The new list after inserting of data is:\n");
printf("\nDisplay link list from front:\n");
displayforward(start);
printf("\n\nDisplay link list from back:\n");
displaybackward(start);
break;
case 4:start=remove(start);
printf("The new list after deleting of data is\n");
printf("Display link list from front:\n");
displayforward(start);
printf("\n\nDisplay link list from back:\n");
displaybackward(start);
break;
}
}while(choice<=4);
printf("\nEND\n");
}
void create(node *record)
{
scanf("\n%d",&record->item);
if(record->item==0)
{
record->next=NULL;
return;
AFRAZ KHAN
1616028
20
}
else
{
record->next=(node *)malloc(sizeof(node));
record->next->prev=record;
record=record->next;
create(record);
}
}
void displayforward(node *record)
{
printf("\n%d",record->item);
if(record->next==NULL)
return;
else
record=record->next;
displayforward(record);
}
void displaybackward(node *record)
{
while(record->item!=0)
record=record->next;
while(record!=NULL)
{
printf("%d\n",record->item);
record=record->prev;
}
}
node *insert(node *record)
{
node *first,*ptr;
ptr=(node*)malloc(sizeof(node));
int choice,x,y;
printf("Menu for insertion of data:\n");
printf("1:Insert at the beginning\n2:Insert anywhere:\n");
printf("Enter the choice:\n\n");
scanf("%d",&choice);
switch(choice)
{
case 1: printf("Insert at the beginning:\n");
first=(node*)malloc(sizeof(node));
printf("Enter the element to be inserted:\n");
scanf("%d",&first->item);
first->next=(node*)malloc(sizeof(node));
first->next=record;
first->next->prev=first;
first->prev=NULL;
break;
case 2: printf("Insert anywhere:\n");
first=record;
printf("Enter the element to be insert:\n");
scanf("%d",&y);
printf("After to be inserted\n");
scanf("%d",&x);
while(record->item!=x)
record=record->next;
ptr->item=y;
ptr->next=record->next;
record->next->prev=ptr;
record->next=ptr;
AFRAZ KHAN
1616028
21
ptr->prev=record;
break;
}return(first);
}
node *remove(node *record)
{
node *first;
int choice,y;
printf("Menu for deletion of data:\n");
printf("1:Delete 1st node\n2:Delete anywhere:\n");
printf("Enter the choice:\n\n");
scanf("%d",&choice);
switch(choice)
{
case 1: printf("delete at the beginning:\n");
first=record;
first->next->prev=NULL;
first=first->next;
free(record);
break;
case 2: printf("Delete anywhere:\n");
printf("after what to delete:\n");
scanf("%d",&y);
first=record;
while(record->item!=y)
record=record->next;
record->next->next->prev=record;
record->next=record->next->next;
break;
}
return(first);
}
OUTPUT:
Enter the choice
Menu:
1:Create
2:Display
3:Insert
4:Delete
5:End
1
To create double linked list:
1
3
5
7
9
0
Enter the choice
Menu:
1:Create
2:Display
3:Insert
4:Delete
5:End
2
To display double linked list:
Display link list from front:
1
AFRAZ KHAN
1616028
22
3
5
7
9
0
Display link list from back:
0
9
7
5
3
1
Enter the choice
Menu:
1:Create
2:Display
3:Insert
4:Delete
5:End
3
Menu for insertion of data:
1:Insert at the beginning
2:Insert anywhere:
Enter the choice:
1
Insert at the beginning:
Enter the element to be inserted:
12
The new list after inserting of data is:
Display link list from front:
12
1
3
5
7
9
0
AFRAZ KHAN
1616028
23
9
The new list after inserting of data is:
Display link list from front:
12
1
3
5
7
9
10
0
1
3
5
7
9
10
0
Display link list from back:
0
10
9
7
5
3
1
Enter the choice
Menu:
1:Create
2:Display
3:Insert
4:Delete
5:End
4
Menu for deletion of data:
1:Delete 1st node
AFRAZ KHAN
1616028
24
2:Delete anywhere:
Enter the choice: 2
Delete anywhere:
after what to delete:
9
The new list after deleting of data is
Display link list from front:
1
3
5
7
9
0
AFRAZ KHAN
1616028
25
case 3: start=insert(start);
printf("The new list after inserting of data is:\n");
display(start);
break;
case 4:start=remove(start);
printf("The new list after deleting of data is\n");
display(start);
break;
}
}while(choice<=4);
printf("\nEND\n");
}
while(n!=0)
{
record->next=(node*)malloc(sizeof(node));
scanf("%d",&record->item);
temp=record;
record=record->next;
n--;
}
temp->next=first;
}
void display(node *record)
{
node *first;
first=(node*)malloc(sizeof(node));
first=record;
do
{
printf("%d\n",record->item);
record=record->next;
}while(record!=first);
void display_anywhere(node*record)
{
int y;
printf("Display the list form the element entered");
scanf("%d",&y);
while(record->item!=y)
record=record->next;
do
{
printf("%d",record->item);
record=record->next;
}
while(record->item!=y);
AFRAZ KHAN
1616028
26
AFRAZ KHAN
1616028
27
first=record;
while(record->item!=y)
record=record->next;
record->next=record->next->next;
return(first);
break;
case 3: printf("Delete at the end:\n");
first=record;
while(record->next->next!=first)
record=record->next;
record->next=first;
return(first);
break;
}
return(record);
}
OUTPUT:
Enter the choice
Menu:
1:Create
2:Display
3:Insert
4:Delete
5:End
Enter the choice=1
To create circular linked list:
Enter the number of elements in a circular linked list:6
1
2
3
4
5
6
Enter the choice
Menu:
1:Create
2:Display
3:Insert
4:Delete
5:End
Enter the choice=2
To display circular linked list:
1
2
3
4
5
6
Enter the choice
Menu:
1:Create
2:Display
3:Insert
4:Delete
5:End
Enter the choice=3
Menu for insertion of data:
1:Insert at the beginning
2:Insert anywhere:
Enter the choice:
2
AFRAZ KHAN
1616028
28
Insert anywhere:
Enter the element to be insert:
10
After to be inserted
4
The new list after inserting of data is:
1
2
3
4
10
5
6
Enter the choice
Menu:
1:Create
2:Display
3:Insert
4:Delete
5:End
Enter the choice=
4
Menu for deletion of data:
1:Delete 1st node
2:Delete anywhere:
Enter the choice:
2
Delete anywhere:
after what to delete:
3
The new list after deleting of data is
1
2
3
10
5
6
PROGRAM-12
AFRAZ KHAN
1616028
29
top++;
stack[top]=x;
}
int pop()
{
int m;
if(top==-1)
printf("stack is empty\n");
else
m=stack[top];
top--;
return m;
}
void display()
{
int n;
n=top;
while(n!=-1)
{
printf("%d\n",stack[n]);
n--;
};
}
OUTPUT:
Menu
1:Push
2:Pop
3:Display
Enter your choice:
1
Enter the element to be pushed:21
Menu
1:Push
2:Pop
3:Display
Enter your choice:
1
Enter the element to be pushed:22
Menu
1:Push
2:Pop
3:Display
Enter your choice:
1
Enter the element to be pushed:23
AFRAZ KHAN
1616028
30
Menu
1:Push
2:Pop
3:Display
Enter your choice:
1
Enter the element to be pushed:24
Menu
1:Push
2:Pop
3:Display
Enter your choice:
3
Dispaly the Element of stacks
24
23
22
21
Menu
1:Push
2:Pop
3:Display
Enter your choice:
2
Element popped is 24
Menu
1:Push
2:Pop
3:Display
Enter your choice:
2
Element popped is 23
Menu
1:Push
2:Pop
3:Display
Enter your choice:
3
Dispaly the Element of stacks
22
21
Menu
1:Push
2:Pop
3:Display
Enter your choice:
AFRAZ KHAN
1616028
31
do
{
printf("\nMenu\n1:Push\n2:Pop\n3:Display\n\n");
printf("Enter your choice:\n");
scanf("%d",&choice);
switch(choice)
{
case 1:printf("\nEnter the element to be pushed:");
scanf("%d",&num);
push(num);
break;
case 2:num=pop();
printf("\nElement popped is %d\n",num);
break;
case 3:printf("\nDispaly the Element of stacks\n");
display();
break;
default:printf("INVLAID CHOICE\n\n");
break;
}
}while(choice<=3);
}
void push(int x)
{
node *ptr;
ptr=(node *)malloc(sizeof(node));
ptr->data=x;
ptr->link=stack;
stack=ptr;
}
int pop()
{
node *temp=NULL;
if(stack==NULL)
printf("STACK IS EMPTY\n");
else
{
int i=stack->data;
temp=stack;
stack=stack->link;
free(temp);
return(i);
}
}
void display()
{
node *temp=stack;
while(temp!=NULL)
{
printf("%d\n",temp->data);
temp=temp->link;
};
}
OUTPUT:
Menu
1:Push
2:Pop
3:Display
Enter your choice:
1
Enter the element to be pushed:12
AFRAZ KHAN
1616028
32
Menu
1:Push
2:Pop
3:Display
Enter your choice:
1
Enter the element to be pushed:13
Menu
1:Push
2:Pop
3:Display
Enter your choice:
1
Enter the element to be pushed:14
Menu
1:Push
2:Pop
3:Display
Enter your choice:
1
Enter the element to be pushed:15
Menu
1:Push
2:Pop
3:Display
Enter your choice:
3
Dispaly the Element of stacks
15
14
13
12
Menu
1:Push
2:Pop
3:Display
Enter your choice:
2
Element popped is 15
Menu
1:Push
2:Pop
3:Display
Enter your choice:
2
Element popped is 14
Menu
1:Push
2:Pop
3:Display
Enter your choice:
3
Dispaly the Element of stacks
13
12
Menu
1:Push
2:Pop
3:Display
Enter your choice:
PROGRAM-13
AFRAZ KHAN
1616028
33
do
{
printf("\nMenu\n1:INSERT AT REAR\n2:DELETE AT FRONT\n3:Display\n\n");
printf("Enter your choice:\n");
scanf("%d",&choice);
switch(choice)
{
case 1:printf("\nEnter the element to be pushed:");
scanf("%d",&num);
push(num);
break;
case 2:num=pop();
printf("\nElement popped is %d\n",num);
break;
case 3:printf("\nDispaly the Element of Queue\n");
display();
break;
default:printf("INVLAID CHOICE\n\n");
break;
}
}while(choice<=3);
}
void push(int x)
{
if(rear==size-1)
printf("QUEUE IS FULL\n");
else
{
rear++;
queue[rear]=x;
}
if(front==-1)
front=0;
}
int pop()
{
int m;
if(front==-1)
printf("queue is empty\n");
else
m=queue[front];
queue[front]=0;
if(front==rear)
front=rear=-1;
else
front++;
return m;
}
void display()
AFRAZ KHAN
1616028
34
{
int i;
if(front==rear==1)
printf("QUEUE IS EMPTY\n");
else
{
for(i=front;i<=rear;i++)
printf("%d\n",queue[i]);
}
}
OUTPUT:
Menu
1:INSERT AT REAR
2:DELETE AT FRONT
3:Display
Enter your choice:
1
Enter the element to be pushed:10
Menu
1:INSERT AT REAR
2:DELETE AT FRONT
3:Display
Enter your choice:
1
Enter the element to be pushed:20
Menu
1:INSERT AT REAR
2:DELETE AT FRONT
3:Display
Enter your choice:
1
Enter the element to be pushed:30
Menu
1:INSERT AT REAR
2:DELETE AT FRONT
3:Display
AFRAZ KHAN
1616028
35
AFRAZ KHAN
1616028
36
temp=(node*)malloc(sizeof(node));
temp->data=x;
temp->next=NULL;
if(front==NULL)
front=rear=temp;
else
{
rear->next=temp;
rear=rear->next;
}
}
int pop()
{
int m;
node *temp;
temp=front;
if(rear==NULL)
printf("QUEUE IS EMPTY\n");
else
{
m=temp->data;
temp=temp->next;
free(front);
front=temp;
}
return m;
}
void display()
{
node *temp;
temp=front;
if(rear==NULL)
printf("\nQueue is empty\n");
else
{
while(temp!=NULL)
{
printf("\n%d\n",temp->data);
temp=temp->next;
};
}
}
OUTPUT:
Menu
1:INSERT AT REAR
2:DELETE AT FRONT
3:DISPLAY
Enter your choice:
1
Enter the element to be pushed:10
Menu
1:INSERT AT REAR
2:DELETE AT FRONT
3:DISPLAY
Enter your choice:
1
Enter the element to be pushed:20
Menu
1:INSERT AT REAR
2:DELETE AT FRONT
3:DISPLAY
AFRAZ KHAN
1616028
37
AFRAZ KHAN
1616028
38
{
printf("a[%d]=",i);
scanf("%d",&a[i]);
}
do
{
printf("\nEnter the element to the searched:");
scanf("%d",&num);
for(i=0;i<n;i++)
{
if(a[i]==num)
{
printf("The element is found at position %d",i+1);
break;
}
}
if(i==n)
printf("\nThe element not found");
printf("\n\nType 1 to continue and 0 to exit:");
scanf("%d",&ch);
}while(ch==1);
}
OUTPUT:
Enter the no. of elements in an array:5
enter the elements:
a[0]=51
a[1]=61
a[2]=71
a[3]=81
a[4]=91
Enter the element to the searched:61
The element is found at position 2
Type 1 to continue and 0 to exit:1
Enter the element to the searched:91
The element is found at position 5
for(i=0;i<n;i++)
{
printf("a[%d]=",i);
scanf("%d",&a[i]);
}
do
{
printf("\nEnter the element to the searched:");
scanf("%d",&num);
AFRAZ KHAN
1616028
39
start=0;
end=n-1;
mid=(start+end)/2;
while(num!=a[mid]&&start<=end)
{
if(num>a[mid])
start=mid+1;
else
end=mid-1;
mid=(start+end)/2;
}
if(num==a[mid])
printf("\nThe element %d is found at position %d",num,mid+1);
if(start>end)
printf("\nThe element %d is not found",num);
printf("\n\nType 1 to continue and 0 to exit:");
scanf("%d",&ch);
}while(ch==1);
}
OUTPUT:
Enter the no. of elements in an array:5
enter the elements:
a[0]=11
a[1]=12
a[2]=13
a[3]=14
a[4]=15
Enter the element to the searched:11
The element 11 is found at position 1
Type 1 to continue and 0 to exit:1
Enter the element to the searched:12
The element 12 is found at position 2
Type 1 to continue and 0 to exit:1
Enter the element to the searched:15
The element 15 is found at position 5
Type 1 to continue and 0 to exit
PROGRAM- 15
AIM- TO SORT NUMBERS IN ASCENDING ORDER
AFRAZ KHAN
1616028
40
if(a[j]<a[minpos])
minpos=j;
}
if(minpos!=i)
{
temp=a[i];
a[i]=a[minpos];
a[minpos]=temp;
}
}
printf("array after %d iteration",i);
for(int k=0;k<n;k++)
printf("%d\t",a[k]);
printf("\n");
}
printf("The elements of sorted array are:\n");
for(i=0;i<n;i++)
printf("a[%d]=%d\n",i,a[i]);
}
OUTPUT:
Enter the elements to be sorted:5
Enter the elements:
a[0]=50
a[1]=40
a[2]=10
a[3]=90
a[4]=30
array after 0 iteration10 40 50 90 30
array after 1 iteration10 30 50 90 40
array after 2 iteration10 30 40 90 50
array after 3 iteration10 30 40 50 90
The elements of sorted array are:
a[0]=10
a[1]=30
a[2]=40
a[3]=50
a[4]=90
AFRAZ KHAN
1616028
41
{
if(temp<a[j])
a[j+1]=a[j];
else
break;
}
a[j+1]=temp;
printf("array after %d iteration",i);
for(int k=0;k<n;k++)
printf("%d\t",a[k]);
printf("\n");
}
printf("The elements of sorted array are:\n");
for(i=0;i<n;i++)
printf("a[%d]=%d\n",i,a[i]);
}
OUTPUT:
Enter the elements to be sorted:5
Enter the elements:
a[0]=25
a[1]=14
a[2]=26
a[3]=23
a[4]=24
array after 0 iteration25 14 26 23 24
array after 1 iteration14 25 26 23 24
array after 2 iteration14 25 26 23 24
array after 3 iteration14 23 25 26 24
array after 4 iteration14 23 24 25 26
The elements of sorted array are:
a[0]=14
a[1]=23
a[2]=24
a[3]=25
a[4]=26
PROGRAM- 16
AIM- TO CREATE A BINARY SEARCH TREE
#include<stdio.h>
#include<stdlib.h>
typedef struct BST {
int data;
struct BST *Ichild , *rchild;
}
node;
void insert(node *,node *);
void inorder(node *) ;
void preorder(node *) ;
void postorder(node *) ;
node *search(node *,int, node **);
void main()
{
int choice , ans , key;
node *new_node,*root,*tmp,*parent;
node *get_node();
root = NULL;
printf("\n Program For Binary Search Tree");
AFRAZ KHAN
1616028
42
do{
printf("\n1.CREATE \n2.SEARCH \n3.RECURSIVE \n4.EXIT ");
printf("\n Enter your choice : " );
scanf("%d",&choice);
switch(choice)
{
case 1:
do{
new_node=get_node();
printf("\nEnter the Element ");
scanf("%d",&new_node->data);
if(root==NULL) /* tree is not Created */
root=new_node*
else
insert(root,new_node);
printf("\n Want to enter More Elements? press 1 for yes and 0 for no");
scanf("%d",&ans);
}while(ans==1);
break;
case 2 :
printf("\n Enter element to be searched : ");
scanf("%d",&key);
tmp=search(root,key,&parent);
printf("\n Parent of node %d is %d",tmp->data,parent->data);
break;
case 3:
if(root==NULL)
printf("Tree is not created");
else
{
printf("\n The inorder display : ");
inorder(root);
printf("\n The preorder display : ");
preorder(root);
printf("\n The postorder display : ");
postorder(root);
}
break;
}
}while(choice!=4);
}
//GET NEW NODE
node *get_node()
{
node *temp;
temp=(node*)malloc(sizeof(node));
temp->Ichild=NULL;
temp->rchild=NULL;
return temp;
}
//THIS FUNCTION IS FOR CREATING A BINARY SEACH TREE
AFRAZ KHAN
1616028
43
if(rootx->Ichild==NULL)
rootx->Ichild=new_nodex;
else
insert(rootx->Ichild,new_nodex);
}
if(new_nodex->data>rootx->data)
{
if(rootx->rchild==NULL)
rootx->rchild=new_nodex;
else
insert(rootx->rchild,new_nodex);
}
}
node *search(node *rootx,int key,node **parentx)
{
node *temp;
temp=rootx;
while(temp!=NULL)
{
if(temp->data==key)
{
printf("\nThe %d Element is Present",temp->data);
return temp;
}
*parentx=temp;
if(temp->data>key)
temp=temp->Ichild;
else
temp=temp->rchild;
}
return NULL;
}
//THIS FUNCTION DISPLAYS THE TREE IN INORDER FASHION
AFRAZ KHAN
1616028
44
if(temp!=NULL){
postorder(temp->Ichild);
postorder(temp->rchild);
printf("%d",temp->data);
}
}
output :
Program For Binary Search Tree
1.CREATE
2.SEARCH
3.RECURSIVE
4.EXIT
Enter your choice : 1
Want to enter More Elements? press 1 for yes and 0 for no1
Want to enter More Elements? press 1 for yes and 0 for no1
Want to enter More Elements? press 1 for yes and 0 for no1
Want to enter More Elements? press 1 for yes and 0 for no0
1.CREATE
2.SEARCH
3.RECURSIVE
4.EXIT
Enter your choice : 2
AFRAZ KHAN
1616028