Ex No: 1(a) Simple Calculator
#include <stdio.h>
#include <stdlib.h>
main()
{
int n1,n2,result;
char op;
printf("/n simple calculator");
printf("/n + summation:");
printf("/n - difference:");
printf("/n * product:");
printf("/n / quotient:");
printf("/n % remainder:");
printf("/n enter the
operator:"); op=getchar();
printf("enter operand1 and
operand2:"); scanf("%d
%d",&n1,&n2);
switch(op)
{
case'+':
result=n1+n2;
break;
case'-':
result=n1-n2;
break;
case'*':
result=n1*n2;
break;
case'/':
result=n1/n2;
break;
case'%':
result=n1%n2;
break;
default:
printf("invalid operator");
exit(-1);
}
printf("%d %c %d=%d",n1,op,n2,result);
}
ouput:
SimpleCalculator
+ Summation
- Difference
Product
/ Quotient
% Remainder
Enter the
operator:-
Enter operand 1 and operand 2:242-4=-
2 SimpleCalculator
+ Summation
- Difference
Product
/ Quotient
% Remainder
Enter the operator:
Enter operand 1 and operand 2:525%2=1
Ex No: 1(b) Armstrong Numbers
#include <stdio.h>
main()
{
int i,n,d,sum;
printf("armstrong numbers:");
for(i=2;i<=1000;i++)
{
sum=0;
n=i;
while(n)
{
d=n%10;
sum=sum+(d*d*d);
n=n/10;
}
if(sum==i)
printf("%d",i);
}
}
Output:
Armstrong numbers: 153 370 371 407
Ex No: 1(c) Sum of Digits
#include <stdio.h>
main()
{
int n,d,sum;
printf("enter a
number:");
scanf("%d",&n);
sum=0;
while(n)
{
d=n%10;
sum=sum+d;
n=n/10;
}
printf("sum of digit: %d",sum);
}
Output
Enter a number:
58349 Sum of digits:
29
Ex No: 1(d) First N numbers
#include<stdio.h>
main()
{
int i=0,j,n;
printf("\n enter value for
n:"); scanf("%d",&n);
printf("first n number div by
3:"); for(j=1; ;j++)
{
if(j%3==0)
{
i++;
printf("%d",j);
}
if(i==n)
break;
}
}
Output
Enter value for n:8
First N numbers divisible by 3 :3 6 9 12 15 18 21 24
Ex No: 1(e) Factorial Value
#include
<stdio.h> long
factorial(int);
main()
{
int n;
long f;
printf("enter a
number:");
scanf("%d",&n);
f=factorial(n);
printf("factorial value: %1d",f);
}
long factorial(int n)
{
int i;
long f=1;
for(i=n; i>=1; i--)
f=f*i;
return f;
}
Output
Enter a number : 6
Factorial
value:720
Enter a number : 12
Factorial
value:479001600
Ex No: 2(a) Array Maximum
#include <stdio.h>
main()
{
int a[10];
int i,max,n;
printf("enter the number of
element:"); scanf("%d",&n);
printf("enter array element /n");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
max=a[0];
for(i=1;i<n;i++)
{
if(max<a[i])
max=a[i];
}
printf("maximum value=%d",max);
}
Output
Enter number of
elements:6 Enter Array
Elements
3
8
-7
11
-9
0
Maximum value = 11
Ex No: 2(b) Matrix Addition
#include <stdio.h>
main()
{
int a[5][5],b[5][5],c[5][5];
int i,j,row,col;
printf("\n enter order for
matrix:"); scanf("%d
%d",&row,&col);
printf("\n enter element for A matrix\
n"); for(i=0;i<row;i++)
for(j=0;j<col;j++)
scanf("%d",&a[i][j]);
printf("\n enter element for B matrix\
n"); for(i=0;i<row;i++)
for(j=0;j<col;j++)
scanf("%d",&b[i][j]);
for(i=0;i<row;i++)
for(j=0;j<col;j++) c[i][j]=a[i]
[j]+b[i][j];
printf("\n contents of c matrix\
n"); for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
{
printf("%3d",c[i][j]);
}
printf("\n");
}
}
Output
Enter order for matrix:22
Enter elements for A
matrix 11
11
Enter elements for B
matrix 22
34
Contents of C
matrix 33
45
Ex No: 2(c) Palindrome
#include <stdio.h>
int main()
{
char s[1000];
int i,n,c=0;
printf("enter the
string:"); gets(s);
n=strlen(s);
for(i=0;i<n/2;i++)
{
if(s[i]==s[n-i-1])
c++;
}
if(c==i)
printf("string is
palindrome"); else
printf("string is not a
plaindrome"); return 0;
}
Output
Enter the string: Malayalam
Reversed string is:
malayalam
Given string is a palindrome
Enter the string: Computer
Reversed string is:
retupmoC
Given string is not a palindrome
Ex No: 2(d) Alphabetical Ordering
#include <stdio.h>
main()
{
char name[20][15],t[15];
int i,j,n;
printf("enter number of
students:"); scanf("%d",&n);
printf("\n enter student names \
n"); for(i=0;i<n;i++)
scanf("%s",name[i]);
for(i=0;i<n-1;i++)
{
for(j=i+1;j<n;j++)
{
if(strcmp(name[i],name[j])>0)
{
strcpy(t,name[i]);
strcpy(name[i],name[j]);
strcpy(name[j],t);
}
}
}
printf("\n alphabetical order \
n"); for(i=0;i<n;i++) printf("%s\
n",name[i]);
}
Output
Enter number of students:10
Enter student
names Raghu
Praba
Gopal
Anand
Saravana
Naresh
Christo
Vasanth
Alphabetical Order
Anand
Christo
Gopal
Naresh
Praba
Raghu
Saravana
Vasant
Ex No: 3(a) Pass By Value/Reference
#include <stdio.h>
main()
{
int a,b;
printf("enter value for a:");
scanf("%d",&a);
printf("enter value for b:");
scanf("%d",&b);
swapref(&a,&b);
printf("\n value after pass by reference \
n"); printf("value of a :%d \n",a);
printf("value of b :%d \n",b);
swapval(a,b);
printf("\n value after pass by value \
n"); printf("value of a :%d \n",a);
printf("value of b :%d \n",b);
}
void swapval(int p,int q)
{
int t;
t=p;
p=q;
q=t;
}
void swapref(int*x,int*y)
{
int t;
t=*x;
*x=*y;
*y=t;
}
Output
Enter value for
A:10 Enter value
for B:20
Values after Pass by Reference
Value of A:20
ValueofB:10
ValuesafterPassbyValue Value
ofA:20
ValueofB:10
Ex No: 3(b) Payroll Application
#include <stdio.h>
struct employee
{
int empid;
char name[10];
int basic;
float hra;
float da;
float it;
float gross;
float netpay;
};
main()
{
struct employee
emp[50]; int i,j,n;
printf("\n enter no.of
employee:"); scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\n enter employee details \
n"); printf("enter employee id:");
scanf("%d",&emp[i].empid);
printf("enter employee name:");
scanf("%s",&emp[i].name);
printf("enter basic salary:");
scanf("%d",&emp[i].basic);
}
for(i=0;i<n;i++)
{
emp[i].hra=0.02*emp[i].basic;
emp[i].da=0.01*emp[i].basic;
emp[i].it=0.05*emp[i].basic;
emp[i].gross=emp[i].basic+emp[i].hra+emp[i].da;
emp[i].netpay=emp[i].gross-emp[i].it;
}
printf("\n\n\n\t\t\txyz& co.payroll\n\n");
for(i=0;i<80;i++)
printf("*");
printf("empid\tname\t\tbasic\thra\t da \t it \t gross \t\t net pay\n");
for(i=0;i<80;i++)
printf("*");
for(i=0;i<n;i++)
{
printf("\n%d\t%- 15s\t%d\t%.2f\t%.2f\t%.2f\t%.2f\t
%.2f",emp[i].empid,emp[i].name,emp[i].basic,emp[i].hra,emp
[i].da,emp[i].it,emp[i].gross,emp[i].netpay);
}
printf("\n");
for(i=0;i<80;i++)
printf("*");
}
Output
Enter No.of Employees:2
Enter Employee Details
Enter Employee Id: 436
Enter Employee Name :
Gopal Enter Basic
Salary:10000
Enter Employee Details
Enter Employee Id : 463
Enter Employee Name :
Rajesh Enter Basic
Salary:22000
XYZ&Co.Payroll
******************************************************************
EmpId Name Basic HRA DA IT Gross NetPay
******************************************************************
436 Gopal 10000 200.00 100.00 500.00 10300.00 9800.00
463 Rajesh 22000 440.00 220.00 1100.00 22660.00 21560.00
Ex.No.4(a) PROGRAM TO READ A TEXT FILE USING FILE POINTER
#include <stdio.h>
#include <stdlib.h>
int main()
{
int num;
FILE *fptr;
if ((fptr = fopen("C:\\program.txt","r")) == NULL){
printf("Error! opening file");
// Program exits if the file pointer returns
NULL. exit(1);
}
fscanf(fptr,"%d", &num);
printf("Value of n=%d", num);
fclose(fptr);
return 0;
}
OUTPUT:
program.txt
C Programming is easy to Learn
Ex.No.4(b) PROGRAM TO WRITE A TEXT FILE USING FILE POINTER
#include <stdio.h>
#include <stdlib.h>
int main()
{
intnum;
FILE *fptr;
fptr = fopen("C:\\program.txt","w");
if(fptr == NULL)
{
printf("Error!");
exit(1);
}
printf("Enter num: ");
scanf("%d",&num);
fprintf(fptr,"%d",num);
fclose(fptr);
return 0;
}
OUTPUT:
C Programming is easy to Le
Ex.No.5 DEVELOPMENT OF REAL TIME C APPLICATIONS
#include <stdio.h>
#include <conio.h>
#include <graphics.h>
#include <stdlib.h>
#include<dos.h>
Void drawhouse()
{ int i;
rectangle(200,200,400,400); // Window Rect
rectangle(100,200,400,400); // Door Rect
rectangle(125,300,175,400); // Door
rectangle(250,250,350,300); // Window #1
rectangle(250,350,350,300); // Window #2
line(300,250,300,350); // Window Line Vertical
line(150,100,100,200);
line(150,100,200,200);
line(150,100,400,100);
line(400,100,400,200);
circle(150,160,20);
rectangle(450,200,460,400); // Lamp Rod
fillellipse(455,180,20,20);} // Light Bulb
}
void main()
{
intgd=0,gm;
initgraph(&gd,&gm,"c:\\tc\\bgi");
setbkcolor(WHITE);
setcolor(RED);
moveto(200,200);
outtext("This is the house in red");
delay(2000);
cleardevice();
drawhouse();
delay(2000);
cleardevice();
moveto(200,200);
setcolor(GREEN);
outtext("This is the house in green");
delay(2000);
cleardevice();
setbkcolor(WHITE);
setcolor(GREEN);
drawhouse();
getch();
closegraph();
}
OUTPUT:
Ex No: 6 Stack Array
#include <stdio.h>
#define max 5
static int stack[max];
int top=-1;
void push(int x)
{
stack[++top]=x;
}
int pop()
{
return(stack[top--]);
}
void view()
{
int i;
if(top<0)
printf("\n stack empty \
n"); else
{
printf("\n top-->");
for(i=top;i>=0;i--)
{
printf("%4d",stack [i]);
}
printf("\n");
}
}
main()
{
int ch=0,val;
while(ch!=4)
{
printf("\n STACK OPERATION \
n"); printf("1.PUSH ");
printf("2.POP ");
printf("3.VIEW ");
printf("2.QUIT \n");
printf("enter choice:");
scanf("%d",&ch);
switch(ch)
{
case 1:
if(top<max-1)
{
printf("\n enter stack
element:"); scanf("%d",&val);
push(val);
}
else
printf("\n stack overflow \
n"); break;
case 2:
if(top<0)
printf("\n stack underflow \n
"); else
{
val=pop();
printf("\n popped element is %d\n",val);
}
break;
case 3:
view();
break;
case 4:
exit(0);
default:
printf("\n INVALID CHOICE \n");
}
}
}
Output
STACK OPERATION
1.PUSH 2.POP 3.VIEW 4.QUIT
Enter Choice:1
EnterStackelement:34
STACK OPERATION
1.PUSH 2.POP 3.VIEW 4.QUIT
EnterChoice:1
Enter Stack
element:45STACK
OPERATION
1.PUSH 2.POP 3.VIEW 4.QUIT
EnterChoice:3
Top--> 45 34
23 12STACK
OPERATION
1.PUSH 2.POP 3.VIEW 4.QUIT
Enter Choice : 2
Popped element
is 45STACK
OPERATION
1.PUSH 2.POP 3.VIEW 4.QUIT
Enter Choice
: 3Top--> 34
23
12
STACK OPERATION
1. PUSH 2.POP 3.VIEW
4.QUITEnter Choice:4
Ex No: 7(a) Queue Array
#include <stdio.h>
#define max 5
static int
queue[max]; int
front=-1;
int rear=-1;
void insert(int x)
{
queue[++rear]=x;
if(front==-1)
front=0;
}
int Remove()
{
int val;
val=queue[front];
if(front==rear && rear==max-
1) front=rear=-1;
else front+
+;
return(val);
}
void view()
{
int i;
if(front==-1)
printf("\n queue empty \
n"); else
{
printf("\n front-->");
for(i=front;i<=rear;i++)
printf("%4d",queue[i]);
printf("<--rear \n");
}
}
main()
{
int ch=0,val;
while(ch!=4)
{
printf("\n queue operation \
n"); printf("1.insert");
printf("2.delete");
printf("3.view");
printf("4.quit \n");
printf("enter choice:");
scanf("%d",&ch);
switch(ch)
{
case 1:
if(rear <max-1)
{
printf("\n enter element to be
inserted:"); scanf("%d",&val);
insert(val);
}
else
printf("\n queue full \
n"); break;
case 2:
if(front==-1)
printf("\n queue empty \
n"); else
{
val=delete();
printf("\n element deleted: %d \n",val);
}
break;
case 3:
view();
case 4:
exit(0);
default:
printf("\n invalid choice \n");
}
}
}
Output
QUEUE OPERATION
1.INSERT 2.DELETE 3.VIEW 4.QUIT
Enter Choice:1
Enter element to be inserted:12
QUEUE OPERATION
1.INSERT 2.DELETE 3.VIEW 4.QUIT
Enter Choice:1
Enter element to be inserted:23
QUEUE OPERATION
1.INSERT 2.DELETE 3.VIEW 4.QUIT
Enter Choice:1
Enter element to be inserted:34
QUEUE OPERATION
1.INSERT 2.DELETE 3.VIEW 4.QUIT
Enter Choice:1
Enter element to be inserted:45
QUEUE OPERATION
1.INSERT 2.DELETE 3.VIEW 4.QUIT
Enter Choice:1
Enter element to be inserted:56
QUEUE OPERATION
1.INSERT 2.DELETE 3.VIEW 4.QUIT
Enter
Choice:1
Queue Full
QUEUE OPERATION
1. INSERT 2.DELETE 3.VIEW
4.QUIT Enter Choice:3
Front--> 12 23 34 45 56 <--Rear
Ex No: 8(a) Singly Linked List
#include <stdio.h>
struct node
{
int label;
struct node*next;
};
main()
{
int ch,fou=0;
int k;
struct node*h,*temp,*head,*h1;
head=(struct node*)malloc(sizeof(struct
node)); head->label=-1;
head->next=NULL;
while(-1)
{
printf("\n SINGLY LINKED LIST OPERATIONS \n");
printf("1->add");
printf("2->delete");
printf("3->view");
printf("4->exit \n");
printf("enter your choice:");
scanf("%d",&ch);
switch(ch)
{
case 1:
printf("\n enter label after whichto add
:"); scanf("%d",&k);
h=head;
fou=0;
if(h->label==k)
fou=1;
while(h->next !=NULL)
{
if(h->label==k)
{
fou=1;
break;
}
h=h->next;
}
if(h->label==k)
fou=1;
if(fou !=1)
€printf("node not found \n");
else
{
temp=(struct
node*)malloc(sizeof(struct node));
printf("enter label for new node:");
scanf("%d",&temp->label);
temp->next=h-
>next; h-
>next=temp;
}
break;
case 2:
printf("\n enter label after whichto
add :"); scanf("%d",&k);
fou=0;
h=h1=hea
d;
while(h->next !=NULL)
{
h=h->next;
if(h->label==k)
{
fou=
1;
break
;
}
}
if(fou==0)
printf("sorry node not
found \n"); else
{
while(h1->next !
=NULL) h1=h1-
>next;
h1->next=h->next;
free(h);
printf("node delete successfully \n");
}
break;
case 3:
print("\n\n
HEAD");
h=head;
while(h->next !=NULL)
{
h=h->next;
printf("%d",h-
>label);
}
printf("NULL
"); break;
case
4:
exit(0
);
}
}
}
Output
SINGLY LINKED LIST OPERATIONS
1->Add 2->Delete 3->View 4-
>Exit Enter your choice : 1
Enter label after which new node is to be added : 23
Enter label for new node : 67
SINGLY LINKED LIST OPERATIONS
1- >Add 2->Delete 3->View 4-
>Exit Enter your choice : 3
HEAD->23->67->NULL
Ex No: 8(b) Stack Using Linked List
#include <stdio.h>
struct node
{
int label;
struct node *next;
};
main()
{
int ch=0;
int k;
struct node *h,*temp,*head;
head=(struct node*)(malloc(sizeof(struct
node))); head->next=NULL;
while(1)
{
printf("\n stack using linked list \
n"); printf("1->push");
printf("2->pop");
printf("3->view");
printf("4->exit");
scanf("%d",&ch);
switch(ch)
{
case 1:
temp=(struct node*)(malloc(sizeof(struct
node))); printf("enter label for new node:");
scanf("%d",temp->label);
h=head=temp;
break;
case 2:
h=head->next;
head->next=h->next;
printf("node %s deleted \n",h-
>label); free(h);
break;
case 3:
printf("\n head->");
h=head;
while(h->next !=NULL)
{
h=h->next;
printf("%d->",h->label);
}
printf("NULL \n");
break;
case 4:
exit(0);
}
}
}
Output
Stack using Linked List
1->Push 2->Pop 3->View 4->Exit
Enter your choice:1
Enter label for new node:23
New node added
Stack using Linked List
1->Push 2->Pop 3->View 4->Exit
Enter your choice:1
Enter label for new node:34
Stack using Linked List
1- >Push 2-> Pop 3->View 4-
>Exit Enter your choice:3
HEAD -> 34 -> 23 -> NULL
Ex No: 8(c) Queue Using Linked List
#include <stdio.h>
struct node
{
int label;
struct node *next;
};
main()
{
int ch=0;
int k;
struct node *h,*temp,*head;
head=(struct node*)malloc(sizeof(struct
node)); head->next=NULL;
while(1)
{
printf("\n queue using linked list \
n"); printf("1->insert");
printf("2->delete");
printf("3->view");
printf("4->exit");
printf("enter your choice:");
scanf("%d",&ch);
switch(ch)
{
case 1:
temp=(struct node*)(malloc(sizeof(struct
node))); printf("enter label for new node:");
scanf("%d",&temp->label);
h=head;
while(h->next!=NULL)
h=h->next;
h->next=temp;
temp->next=NULL;
break;
case 2:
h=head->next;
head->next=h->next;
printf("node deleted \n");
free(h);
break;
case 3:
printf("\n\n head->");
h=head;
while(h->next!=NULL)
{
h=h->next;
printf("%d->",h->label);
}
printf("NULL \n");
break;
case 4:
exit(0);
}
}
}
Output
Queue using Linked List
1->Insert 2->Delete 3->View 4->Exit
Enter your choice:1
Enter label for new node:12
Queue using Linked List
1->Insert 2->Delete 3->View 4->Exit
Enter your choice:1
Enter label for new node:23
Queue using Linked List
1- >Insert 2->Delete 3->View 4-
>Exit Enter your choice:3
HEAD -> 12 -> 23 -> NULL
Ex No: 9(a) Infix To Postfix Conversion
#include <stdio.h>
#define max 20
int top=-1;
char stack[max];
char pop();
void push(char item);
int prcd(char
symbol)
{
switch(symbol)
{
case'+':
case'-':
return 2;
break;
case'*':
case'/':
return 4;
break;
case'^':
case'$':
return 6;
break;
case'(':
case')':
return 1;
break;
}
}
int isoperation(char symbol)
{
switch(symbol)
{
case'+':
case'-':
case'*':
case'/':
case'^':
case'$':
case'(':
case')':
return 1;
break;
default:
return 0;
}
}
void convertip(char infix[],char postfix[])
{
int i,symbol,j=0; stack[+
+top]='#';
for(i=0;i<strlen(infix);i++)
{
symbol=infix[i];
if(isoperation(symbol)==0)
{
postfix[j]=symbol;
j++;
}
else
{
if(symbol=='(')
push(symbol);
else if(symbol==')')
{
while(stack[top] !='(')
{
postfix[j]=pop();
j++;
}
pop();//pop out(.
}
else
{
if(prcd(symbol)>prcd(stack[top]))
push(symbol);
else
{
while(prcd(symbol)<=prcd(stack[top]))
{
postfix[j]=pop();
j++;
}
push(symbol);
}
}
}
}
while(stack[top]!='#')
{
postfix[j]=pop();
j++;
}
postfix[j]='\0';
}
main()
{
char infix[20],postfix[20];
printf("enter the valid infix
string:"); gets(infix);
convertip(infix,postfix);
printf("the corresponding postfix string
is:"); puts(postfix);
}
void push(char item)
{
top++;
stack[top]=item;
}
char pop()
{
char a;
a=stack[top];
top--;
return a;
}
Output
Enter the valid infix string: (a+b*c)/(d$e)
The corresponding postfix string is:abc*+de$/
Enter the valid infix string : a*b+c*d/e
The corresponding postfix string is :ab*cd*e/+
Enter the valid infix string : a+b*c+(d*e+f)*g
The corresponding postfix string is : abc*+de*f+g*+
Ex No: 9(b) Postfix Expression Evaluation
#include <stdio.h>
struct stack
{
int top;
float a[50];
}s;
main()
{
char pf[50];
float d1,d2,d3;
int i;
s.top=-1;
printf("\n\n enter the postfix
expression:"); gets(pf);
for(i=0;pf[i]='\0';i++)
{
switch(pf[i])
{
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9': s.a[+
+s.top]=pf[i]-'0';
break;
case '+':
d1=s.a[s.top--];
d2=s.a[s.top--];
s.a[++s.top]=d1+d2;
break;
case '-':
d1=s.a[s.top--];
d2=s.a[s.top--];
s.a[++s.top]=d1-d2;
break;
case '*':
d1=s.a[s.top--];
d2=s.a[s.top--];
s.a[++s.top]=d1*d2;
break;
case '/':
d1=s.a[s.top--];
d2=s.a[s.top--];
s.a[++s.top]=d1/d2;
break;
}
}
printf("\n expression value is %5.2f",s.a[s.top]);
}
Output
Enter the postfix expression:6523+8*+3+*
Expression value is : 288.00
Ex No: 9(c) FCFS Scheduling
#include <stdio.h>
struct process
{
int pid;
int btime;
int wtime;
int ttime;
} p [10];
main()
{
int n;
int i,j,k,ttur,twat;
float awat,atur;
printf("enter no.of process:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("burst time for process p%d(in ms):",
(i+1)); scanf("%d",&p[i].btime);
p[i].pid=i+1;
}
p[0].wtime=0;
for(i=0;i<n;i++)
{
p[i+1].wtime=p[i].wtime+p[i].btime;
p[i].ttime=p[i].wtime+p[i].btime;
}
ttur=twat=0;
for(i=0;i<n;i++)
{
ttur+=p[i].ttime;
twat+=p[i].wtime;
}
awat=(float)twat /n;
atur=(float)ttur /n;
printf("\n FCFS scheduling \n\
n"); for(i=0;i<28;i++)
printf("-");
printf("\n process b-time t-time w-time \n");
for(i=0;i<28;i++)
printf("-");
for(i=0;i<n;i++)
printf("\n p%d\t%4d\t%3d\t%2d",p[i].pid,p[i].btime,p[i].ttime,p[i].wtime); printf("\
n");
for(i=0;i<28;i++)
printf("-");
printf("\n\n average waiting time :%5.2fms",awat);
printf("\n average waiting time :%5.2fms\n",atur);
printf("\n\n gantt chart\
n"); printf("-");
for(i=0;i<(p[n-1].ttime+2*n);i++)
printf("-");
printf("\n");
printf("|");
for(i=0;i<n;i++)
{
k=p[i].btime/2;
for(j=0;j<k;j++)
printf(""); printf("p
%d",p[i].pid);
for(j=k+1;j<p[i].btime;j++)
printf("");
printf("|");
}
printf("\n");
printf("-");
for(i=0;i<(p[n-1].ttime+2*n);i++)
printf("-");
printf("\n");
printf("0");
for(i=0;i<n;i++)
{
for(j=0;i<p[i].btime;j++)
printf("");
printf("2%d",p[i].ttime);
}
}
Output
Enter no.of process:4
Burst time for process P1 (in ms):10
Burst time for process P2 (in ms) :4
Burst time for process P3 (inms):11
Burst time for process P4 (inms):6
FCFS Scheduling
Process B-Time T-Time W-Time
P1 10 10 0
P2 4 14 10
P3 11 25 14
P4 6 31 25
Average waiting time:12.25ms
Average turn around time:20.00ms
GANTT Chart
| P1 | P2| P3 | P4 |
0 10 14 25 31
Ex No: 10 Tree Traversal
#include <stdio.h>
typedef struct node
{
int data;
struct node*left;
struct node*right;
}node;
int count=1;
node*insert(node*tree,int digit)
{
if(tree==NULL )
{
tree=(node*)malloc(sizeof(node));
tree->left=tree->right=NULL;
tree->data=digit;
count++;
}
else if(count%2==0)
tree->left=insert(tree->left,digit);
else
tree->right=insert(tree->right,digit);
return tree;
}
void preorder(node *t)
{
if(t!=NULL)
{
printf("%d",t->data);
preorder(t->left);
preorder(t->right);
}
}
void postorder(node*t)
{
if(t!=NULL)
{
postorder(t->left);
postorder(t->right);
printf("%d",t->data);
}
}
void inorder(node*t)
{
if(t!=NULL)
{
inorder(t->left);
printf("%d",t->data);
inorder(t->right);
}
}
main()
{
node*root=NULL;
int digit;
puts("enter integer:to quit enter
0"); scanf("%d",&digit);
while(digit !=0)
{
root=insert(root,digit);
scanf("%d",&digit);
}
printf("\n the preorder traversal of tree is:\
n"); preorder(root);
printf("\n the inorder traversal of tree is:\
n"); inorder(root);
printf("\n the postorder traversal of tree is:\
n"); postorder(root);
}
Output
Enter integer:To quit enter
0 12 4 6 9 14 17 3 19 0
The preorder traversal of tree is :
12 4 9 17 19 6 14 3
The inorder traversal of tree is :
19 17 9 4 12 6 14 3
The postorder traversal of tree is :
19 17 9 4 3 14 6 12
Ex No: 11 Binary Search Tree
#include<stdio.h>
struct node
{
int key;
struct node*left;
struct node*right;
};
struct node*newnode(int item)
{
struct node*temp=(struct node*)malloc(sizeof(struct
node)); temp->key=item;
temp->left=temp->right=NULL;
return temp;
}
void inorder(struct node*root)
{
if(root!=NULL)
{
inorder(root->left);
printf("%d",root->key);
inorder(root->right);
}
}
struct node*insert(struct node*node,int key)
{
if(node==NULL)
return newnode(key);
if(key<node->key)
node->left=insert(node->left,key);
else
node->right=insert(node->right,key);
return node;
}
struct node *minvalnode(struct node*node)
{
struct node*current=node;
while(current->left!=NULL)
current=current->left;
return current;
}
struct node*deletenode(struct node*root,int key)
{
struct node*temp;
if(root==NULL)
return root;
if(key<root->key)
root->left=deletenode(root->left,key);
else if(key>root->key)
root->right=deletenode(root->right,key);
else
{
if(root->left==NULL)
{
temp=root->right;
free(root);
return temp;
}
else if(root->right==NULL)
{
temp=root->left;
free(root);
return temp;
}
temp=minvalnode(root->right);
root->key=temp->key;
root->right=deletenode(root->right,temp->key);
}
return root;
}
main()
{
struct node*root=NULL;
root=insert(root,50);
root=insert(root,30);
root=insert(root,20);
root=insert(root,40);
root=insert(root,70);
root=insert(root,60);
root=insert(root,80);
printf("inorder traversal of the given tree \
n");
inorder(root);
printf("\n delete 20 \n");
root=deletenode(root,20);
printf("inoreder traversal of the modified tree \
n"); inorder(root);
printf("\n delete 30 \n");
root=deletenode(root,30);
printf("inoreder traversal of the modified tree \
n"); inorder(root);
printf("\n delete 50 \n");
root=deletenode(root,50);
printf("inoreder traversal of the modified tree \
n"); inorder(root);
}
Output
Inorder traversal of the given tree
20 30 40 50 60 70 80
Delete 20
Inorder traversal of the modified tree
30 40 50 60 70 80
Delete 30
Inorder traversal of the modified tree
40 50 60 70 80
Delete 50
Inorder traversal of the modified tree
40 60 70 80
Ex No: 12(a) Linear Search
#include<stdio.h>
main()
{
int a[50],i,n,val,found;
printf("enter number of element:");
scanf("%d",&n);
printf("enter array element:");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("enter element to
locate:"); scanf("%d",&val);
found=0;
for(i=0;i<n;i++)
{
if(a[i]==val)
{
printf("element found at position
%d",i); found=1;
break;
}
}
if (found==0)
printf("\n element not found");
}
Output
Enter number of
elements:7 Enter Array
Elements:
23 6 12 5 0 32 10
Enter element to locate:5
Element found at position 3
Ex No: 12(b) Binary Search
#include<stdio.h>
main()
{
int a[50],i,n,upper,lower,mid,val,found;
printf("enter array size:");
scanf("%d",&n);
for(i=0;i<n;i++)
a[i]=2*i;
printf("\n element in sorted order \
n"); for(i=0;i<n;i++)
printf("%4d",a[i]); printf("\
n element to locate:");
scanf("%d",&val);
upper=n;
lower=0;
found=-1;
while(lower<=upper)
{
mid=(upper+lower)/2;
if(a[mid]==val)
{
printf("located at position
%d,mid"); found=1;
break;
}
else
if(a[mid]>val)
upper=mid-1;
else
lower=mid+1;
}
if (found==-1)
printf("element not found");
}
Output
Enter array size:9
Elements in Sorted
Order
0 2 4 6 8 10 12 14 16
Enter elementtolocate:12
Located at position 6
Enter array size: 10
Elements in Sorted
Order
0 2 4 6 8 10 12 14 16 18
Enter element to
locate:13 Element not
found
Ex No: 13(a) Insertion Sort
#include<stdio.h>
main()
{
int i,j,k,n,temp,a[20],p=0;
printf("enter total elements
:"); scanf("%d",&n);
printf("enter array elements :");
for (i=0; i<n; i++)
scanf("%d",&a[i]);
for(i=1;i<n;i++)
{
temp=a[i];
j=i-1;
while((temp<a[j])&&(j>=0))
{
a[j+1]=a[j];
j=j-1;
}
a[j+1]=temp;
p++;
printf("\n AfterPass
%d:",p); for (k=0;k<n;k++)
printf ("%d",a[k]);
}
printf("/nsorted list:");
for (i=0;i<n;i++);
printf("%d",a[i]);
}
Output
Enter total elements : 6
Enter array elements : 34 8 64 51 32 21
After Pass1 : 8 34 64 51 32 21
After Pass2 : 8 34 64 51 32 21
After Pass3 : 8 34 51 64 32 21
After Pass4 : 8 32 34 51 64 21
After Pass5 : 8 21 32 34 51 64
Sorted List : 8 21 32 34 51 64
Ex No: 13(b) Quick Sort
#include<stdio.h>
#include<conio.h>
voidqsort(intarr[20],intfst,intlast);main()
{
intarr[30];inti,size;
printf("Entertotalno.oftheelements:");scanf("%d",&size);
printf("Entertotal%delements:\n",size);for(i=0;i<size;i++)
scanf("%d",&arr[i]);qsort(arr,0,size-
1);
printf("\nQuicksortedelements\n");for(i=0;i<size;i++) printf("%d\
t",arr[i]);getch();
}
voidqsort(intarr[20],intfst,intlast)
{
inti,j,pivot,tmp;if(fst<last)
{
pivot=fst;i=fst;
j = last;while(i<j)
{
while(arr[i]<=arr[pivot]&&i<last)i++;
while(arr[j]>arr[pivot])j--;
if(i<j)
{
tmp =
arr[i];arr[i]=arr[j];arr[j]=tmp;
}
}
tmp = arr[pivot];arr[pivot] =
arr[j];arr[j] = tmp;
qsort(arr, fst, j-1);
qsort(arr,j+1,last);
}
}
Output
Enter total no.of the elements:8
Enter total 8 elements:
1
2
7
-1
0
4
-2
3
Quick sorted elements
-2 -1 0 1 2 3 4 7
Ex No: 13(c) Merge Sort
#include <stdio.h>
void qsort(int arr[20],int fst,int
last); main()
{
int arr[30];
int i,size;
printf("enter total %d element:");
scanf("%d",&size);
printf("enter total %d element:\
n",size); for(i=0;i<size;i++)
scanf("%d",&arr[i]);
qsort(arr,0,size-1);
printf("\n quick sorted element \
n"); for(i=0;i<size;i++) printf("%d\
t",arr[i]);
}
void qsort(int arr[20],int fst,int last)
{
int i,j,pivot,tmp;
if(fst<last)
{
pivot=fst;
i=fst;
j=last;
while(i<j)
{
while(arr[i]<=arr[pivot]&&i<last)
i++;
while(arr[j]>arr[pivot])
j--;
if(i<j)
{
tmp=arr[i];
arr[i]=arr[j];
arr[j]=tmp;
}
}
tmp=arr[pivot];
arr[pivot]=arr[j];
arr[j]=tmp;
qsort(arr,fst,j-1);
qsort(arr,j+1,last);
}
}
Output
Enter total no.of elements:8
Enter array elements:24 13 26 12 27 38 15
Half sorted list : 1 13 24 26
Half sorted list : 2 15 27 38
Merge sorted list : 1 2 13 15 24 26 27 38
Ex No: 14 Open Addressing Hashing Technique
#include<stdio.h>
#include<stdlib.h>
#define MAX 10
main()
{
int a[MAX],num,key,i;
char ans;
int create(int);
void linearprobing(int[],int,int);
void display(int[]);
printf("\n collision handling by linear probing\n\
n"); for (i=0;i<MAX;i++)
a[i]=-1;
do
{
printf("\n enter number:");
scanf("%d",&num);
key=create(num);
linearprobing (a,key,
num);
printf("\n wish to continue ?(y/n):");
}while(ans=='y');
display(a);
}
int create(int num)
{
int key;
key=num%10;
return key;
}
void linearprobing(int a[MAX],int key,int num)
{
int flag,i,count=0;
void display(int
a[]); flag=0;
if (a[key]==-1)
a[key]=num;
else
{
i=0;
while(i<MAX)
{
if (a[i]!=-1)
count++;
i++;
}
if(count==MAX)
{
printf("hash table is
full"); display(a);
exit(1);
}
for(i=key+1;i<MAX;i++)
if(a[i]==-1)
{
a[i]=num;
flag=1;
break;
}
for (i=0;i<key&& flag==0;i++)
if (a[i]==-1)
{
a[i]=num;
flag=1;
break;
}
}
}
void display(int a[MAX])
{
int i;
printf("\n hash table is :");
for(i=0;i<MAX;i++)
printf("\n %d\t\t%d",i,a[i]);
}
Output
Collision handling by linear probing
Enter number:1
Wish to
continue?(y/n): Enter
number:26
Wish to
continue?(y/n): Enter
number:62
Wish to
continue?(y/n): Enter
number:84
Wish to
continue?(y/n): Enter
number:15
Wish to
continue?(y/n):
Enter number:76
Wish to
continue?(y/n): Enter
number:98
Wish to
continue?(y/n): Enter
number:26
Wish to
continue?(y/n): Enter
number:199
Wish to
continue?(y/n): Enter
number:1234
Wish to
continue?(y/n): Enter
number:5678
Hash table is full
Hash table is:
0 1234
1 1
2 62
3 93
4 84
5 15
6 26
7 76
8 98
9 199