Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
5 views111 pages

C & DS Lab Final Ece

The document is a lab manual for the C Programming and Data Structures course (CS3362) at the College of Engineering and Technology. It outlines the course objectives, a list of experiments, and expected outcomes for students, along with detailed examples of C programs for various tasks such as arithmetic operations, data structures, and algorithms. Each experiment includes an aim, algorithm, program code, output, and result verification.

Uploaded by

Keerthana
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views111 pages

C & DS Lab Final Ece

The document is a lab manual for the C Programming and Data Structures course (CS3362) at the College of Engineering and Technology. It outlines the course objectives, a list of experiments, and expected outcomes for students, along with detailed examples of C programs for various tasks such as arithmetic operations, data structures, and algorithms. Each experiment includes an aim, algorithm, program code, output, and result verification.

Uploaded by

Keerthana
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 111

COLLEGE OF ENGINEERING AND TECHNOLOGY

DEPARTMENT
OF
ELECTRONICS AND COMMUNICATION ENGINEERING

CS3362
C PROGRAMMING & DATASTRUCTURE
LAB MANUAL

REGISTER NO:

NAME :
CS3362 -C PROGRAMMING & DATASTRUCTURE LAB MANUAL

CS3362 C PROGRAMMING AND DATA STRUCTURES LABORATORY LTPC


0 0 3 1.5
COURSE OBJECTIVES:
 To develop applications in C
 To implement linear and non-linear data structures
 To understand the different operations of search trees
 To get familiarized to sorting and searching algorithms

LIST OF EXPERIMENTS
1. Practice of C programming using statements, expressions, decision making and iterative
Statements.
2. Practice of C programming using Functions and Arrays
3. Implement C programs using Pointers and Structures
4. Implement C programs using Files
5. Development of real time C applications
6. Array implementation of List ADT
7. Array implementation of Stack and Queue ADTs
8. Linked list implementation of List, Stack and Queue ADTs
9. Applications of List, Stack and Queue ADTs
10. Implementation of Binary Trees and operations of Binary Trees
11. Implementation of Binary Search Trees
12. Implementation of searching techniques
13. Implementation of Sorting algorithms: Insertion Sort, Quick Sort, Merge Sort
14. Implementation of Hashing – any two collision techniques
TOTAL: 45 PERIODS
COURSE OUTCOMES:
At the end of the course, the students will be able to:

CO1: Use different constructs of C and develop applications


CO2: Write functions to implement linear and non-linear data structure operations
CO3: Suggest and use the appropriate linear / non-linear data structure operations for a givenProblem
CO4: Apply appropriate hash functions that result in a collision free scenario for data
storageandRetrieval
CO5: Implement Sorting and searching algorithms for a given application.

4213 - KCET DEPARTMENT OF ECE - III - SEMESTER Page 2


CS3362 -C PROGRAMMING & DATASTRUCTURE LAB MANUAL

EXPERIMENTS

PAGE
S.NO DATE PARTICULARS MARK SIGN
NO

4213 - KCET DEPARTMENT OF ECE - III - SEMESTER Page 3


CS3362 -C PROGRAMMING & DATASTRUCTURE LAB MANUAL

COMPLETED/NOT COMPLETED

STAFF INCHARGE

4213 - KCET DEPARTMENT OF ECE - III - SEMESTER Page 4


CS3362 -C PROGRAMMING & DATASTRUCTURE LAB MANUAL
EX.NO: 1A /*Finding Sum and Average */ DATE:

AIM: To write a C program to find the sum and average of the given number.

ALGORITHM:

STEP 1: Start
STEP 2: Initialize the set of variables
STEP 3: Compute Sum
STEP 4: Compute Average
STEP 5: Print Result
STEP 6: Stop

PROGRAM:

#include<stdio.h>
void main()
{
int num1,num2,sum,avg;
printf (“Enter two numbers”);
scanf (“%d”,&num1);
scanf (“%d”,&num2);
sum = num1+num2;
avg = sum/2;
printf (“Sum of two numbers %d”, sum);
printf (“Average of two numbers %d” , avg);
}

OUTPUT:

Enter two numbers 10 20

Sum of two numbers 30 OBS


Average of two numbers 15
REC

VIVA

TOTAL

RESULT:

Thus the above program to find the sum and average of the given number has been executed
successfully and the output was verified.

4213 - KCET DEPARTMENT OF ECE - III - SEMESTER Page 5


CS3362 -C PROGRAMMING & DATASTRUCTURE LAB MANUAL
EX.NO: 1B /*Swapping 2 Numbers without using Third Variable */ DATE:

AIM: To write a C program to swap the given numbers

ALGORITHM:

STEP 1: Start
STEP 2: Initialize the set of variables
STEP 3: Get the input Values
STEP 4: Swap the values
STEP 5: Print Result
STEP 6: Stop

PROGRAM:

#include<stdio.h>
#include<conio.h>
void main()
{
int num1,num2;
clrscr();
printf ("Enter two numbers");
scanf ("%d",&num1);
scanf ("%d",&num2);
num1 = num1 - num2;
num2 = num1 + num2;
num1 = num2 - num1;
printf ("Values after swapping :\n");
printf ("\n Value of a Num1: %d" ,num1);
printf ("\n Value of a Num2: %d" ,num2);
getch();
}
OUTPUT:
OBS
Enter two numbers
3 REC
4
VIVA
Values after swapping :
Value of a num1: 4 TOTAL
Value of a num2: 3

RESULT:

Thus the above program to swap the given numbers has been executed successfully and the
output was verified.

4213 - KCET DEPARTMENT OF ECE - III - SEMESTER Page 6


CS3362 -C PROGRAMMING & DATASTRUCTURE LAB MANUAL
EX.NO: 1C /*Swapping 2 Numbers with using Third Variable */ DATE:

AIM: To write a C program to swap the given numbers

ALGORITHM:

STEP 1: Start
STEP 2: Initialize the set of variables
STEP 3: Get the input Values
STEP 4: Swap the values
STEP 5: Print Result
STEP 6: Stop

PROGRAM:

#include<stdio.h>
#include<conio.h>
void main()
{
int num1,num2,temp;
clrscr();
printf ("Enter two numbers");
scanf ("%d",&num1);
scanf ("%d",&num2);
temp = num1;
num1 = num2;
num2 = temp;
printf ("Values after swapping :\n");
printf ("\n Value of a Num1: %d" ,num1);
printf ("\n Value of a Num2: %d" ,num2);
getch();
}
OUTPUT:

Enter two numbers OBS


3
REC
4
Values after swapping : VIVA
Value of a num1: 4
TOTAL
Value of a num2: 3

RESULT:
Thus the above program to swap the given numbers has been executed successfully and the
output was verified.

4213 - KCET DEPARTMENT OF ECE - III - SEMESTER Page 7


CS3362 -C PROGRAMMING & DATASTRUCTURE LAB MANUAL
EX.NO: 1D /*Check Odd or Even */ DATE:

AIM: To write a C program to check Odd or Even for the given numbers

ALGORITHM:

STEP 1: Start
STEP 2: Initialize the set of variables
STEP 3: Get the input Values
STEP 4: Check the given number is odd or even
STEP 5: Print Result
STEP 6: Stop

PROGRAM:

#include <stdio.h>
void main()
{
int num;
printf ("Enter number:");
scanf("%d",&num);
if (num%2==0)
{
printf ("Number is even");
}
else
{
printf("Number is odd");
}
}

OUTPUT:

Enter number: 2
OBS
Number is even REC

VIVA

TOTAL

RESULT:

Thus the above program to find the given number is odd or even has been executed
successfully and the output was verified.

4213 - KCET DEPARTMENT OF ECE - III - SEMESTER Page 8


CS3362 -C PROGRAMMING & DATASTRUCTURE LAB MANUAL
EX.NO: 1E /*Finding Largest Number */ DATE:

AIM: To write a C program to find the largest of given numbers

ALGORITHM:

STEP 1: Start
STEP 2: Initialize the set of variables
STEP 3: Get the input Values
STEP 4: Check the greatest number
STEP 5: Print Result
STEP 6: Stop

PROGRAM:

#include <stdio.h>
void main()
{
int num1,num2,num3;
printf ("Enter three numbers");
scanf ("%d%d%d",&num1,&num2,&num3);
if (num1 >= num2 && num1 >= num3)
{
printf ("Largest number: %d",num1);
}
else if (num2 >= num1 && num2 >= num3)
{
printf ("Largest number: %d",num2);
}
else
{
printf ("Largest number: %d",num3);
}
}
OUTPUT:
Enter three numbers 10 30 20 OBS
Largest number: 30 REC

VIVA

TOTAL
RESULT:

Thus the above program to find the largest of given number has been executed successfully
and the output was verified.

4213 - KCET DEPARTMENT OF ECE - III - SEMESTER Page 9


CS3362 -C PROGRAMMING & DATASTRUCTURE LAB MANUAL
EX.NO: 1F /*Arithmetic Operations */ DATE:

AIM: To write a C program to perform arithmetic operations.

ALGORITHM:

STEP 1: Start
STEP 2: Initialize the set of variables
STEP 3: Get the Choice
STEP 4: Go to relevant choice and perform
STEP 5: Print Result
STEP 6: Stop

PROGRAM:

#include <stdio.h>
void main()
{
int num1, num2; char op;
printf ("Enter two numbers: ");
scanf ("%d%d",&num1,&num2);
printf ("Enter operator : ");
scanf ("%c",&op);
Switch (op)
{
case '+': printf ("\n Sum of two numbers %d", num1+num2);
break;
case '-': printf ("\n Subtraction of two numbers %d",num1-num2);
break;
case '*': printf ("\n Multiplication of two numbers %d",num1*num2);
break;
case '/': printf ("\n Division of two numbers %d", num1/num2);
break;
default: printf ("\n Invalid operator");
break;
}
}

4213 - KCET DEPARTMENT OF ECE - III - SEMESTER Page 10


CS3362 -C PROGRAMMING & DATASTRUCTURE LAB MANUAL
OUTPUT:

Enter two numbers: 56

Enter Operator: *

Multiplication of two numbers 504

OBS

REC

VIVA

TOTAL

RESULT:

Thus the above program to perform arithmetic operations been executed successfully and the
output was verified.

4213 - KCET DEPARTMENT OF ECE - III - SEMESTER Page 11


CS3362 -C PROGRAMMING & DATASTRUCTURE LAB MANUAL
EX.NO: 1G /*Printing Table */ DATE:

AIM: To write a C program to print the given table.

ALGORITHM:

STEP 1: Start
STEP 2: Initialize the set of variables
STEP 3: Get the table number
STEP 4: Perform the operation
STEP 5: Print Result
STEP 6: Stop

PROGRAM:

#include <stdio.h>
void main()
{
int num, i;
printf ("Enter a number: ");
scanf ("%d",&num);
printf ("Table of %d\n",num);
for (i=1;i<=10;i++)
{
printf ("%d\n",num*i);
}
}
OUTPUT:
Enter a number: 2
Table of 2
2
4
6
8
10 OBS
12
14 REC
16
18 VIVA
20
TOTAL
RESULT:

Thus the above program to print the given table has been executed successfully and the output
was verified.

4213 - KCET DEPARTMENT OF ECE - III - SEMESTER Page 12


CS3362 -C PROGRAMMING & DATASTRUCTURE LAB MANUAL
EX.NO: 1H /*Fibonacci Series */ DATE:

AIM: To write a C program to generate the Fibonacci Series.

ALGORITHM:

STEP 1: Start
STEP 2: Initialize the set of variables
STEP 3: Get the range value
STEP 4: Perform the operation
STEP 5: Print Result
STEP 6: Stop

PROGRAM:

#include<stdio.h>
int main()
{
int n1=0,n2=1,n3,i,number;
printf("Enter the number of elements:");
scanf("%d",&number);
printf("\n%d %d",n1,n2);
for(i=2;i<number;++i)
{
n3=n1+n2;
printf(" %d",n3);
n1=n2;
n2=n3;
}
return 0;
}

OUTPUT:

Enter the number of elements:15 OBS


0 1 1 2 3 5 8 13 21 34 55 89 144 233 377
REC

VIVA

TOTAL
RESULT:

Thus the above program to generate the Fibonacci series has been executed successfully and
the output was verified.

4213 - KCET DEPARTMENT OF ECE - III - SEMESTER Page 13


CS3362 -C PROGRAMMING & DATASTRUCTURE LAB MANUAL

EX.NO: 1 I /*Armstrong Number */ DATE:

AIM: To write a C program to find the given number is Armstrong or not.

ALGORITHM:

STEP 1: Start
STEP 2: Initialize the set of variables
STEP 3: Get the input value
370 is an Armstrong number because:
370 = 33+ 73+ 03
= 27 + 343 + 0
= 370
STEP 4: Perform the operation
STEP 5: Print Result
STEP 6: Stop
PROGRAM:

#include <stdio.h>
void main()
{
int num, sum = 0, rem, temp;
printf ("Enter a number: ");
scanf ("%d",&num);
temp = num;
while (num>0)
{
rem = num%10;
sum = sum+(rem*rem*rem);
num = num/10;
}
if (temp==sum)
printf ("Given number is armstrong\n "); OBS
else
REC
printf ("Given number is not armstrong\n ");
} VIVA
OUTPUT:
TOTAL
Enter a number: 370
Given number is Armstrong

RESULT:

Thus the above program to find the given number is Armstrong or not has been executed
successfully and the output was verified.

4213 - KCET DEPARTMENT OF ECE - III - SEMESTER Page 14


CS3362 -C PROGRAMMING & DATASTRUCTURE LAB MANUAL

EX.NO: 2 A) /*Printing value using Array */ DATE:

AIM: To write a C program to print 5 values using arrays.

ALGORITHM:

STEP 1: Start
STEP 2: Initialize the array
STEP 3: Get the input value
STEP 4: Perform the operation
STEP 5: Print Result
STEP 6: Stop

PROGRAM:

#include <stdio.h>
int main()
{
int arr[5], i;
printf ("Enter 5 numbers:\n ");
for (i=0;i<5;i++)
scanf ("%d",&arr[i]);
printf ("\n Array values are \n");
for (i=0;i<5;i++)
printf ("%d \n",arr[i]);
}

OUTPUT:

Enter 5 numbers: 10 30 70 20 40
Array values are 10 30 70 20 40 OBS

REC

VIVA

TOTAL

RESULT:

Thus the above program to print 5 values using arrays has been executed successfully and the
output was verified.

4213 - KCET DEPARTMENT OF ECE - III - SEMESTER Page 15


CS3362 -C PROGRAMMING & DATASTRUCTURE LAB MANUAL

EX.NO: 2B /*Finding Largest Value in an Array */ DATE:

AIM: To write a C program to find the largest value in an array.

ALGORITHM:

STEP 1: Start
STEP 2: Initialize the array
STEP 3: Get the input value
STEP 4: Perform the operation
STEP 5: Print Result
STEP 6: Stop

PROGRAM:

#include <stdio.h>
int main()
{
int arr[5],i,max;
printf ("Enter 5 numbers:\n ");
for (i=0;i<5;i++)
scanf ("%d",&arr[i]);
max = arr[0];
for (i = 1;i < 5; i++)
{
if (max < arr[i])
max = arr[i];
}
printf ("Largest element = %d" ,max);
}

OUTPUT:

Enter 5 numbers: 12 6 85 78 92 100 17


OBS
Largest element = 100
REC

VIVA

TOTAL

RESULT:

Thus the above program to print the largest number in an array has been executed
successfully and the output was verified.

4213 - KCET DEPARTMENT OF ECE - III - SEMESTER Page 16


CS3362 -C PROGRAMMING & DATASTRUCTURE LAB MANUAL

EX.NO: 2C /*Sum of two Matrixes */ DATE:

AIM: To write a C program to find the sum of two matrix

ALGORITHM:

STEP 1: Start
STEP 2: Initialize the array
STEP 3: Get the input value
STEP 4: Perform the operation
STEP 5: Print Result
STEP 6: Stop

PROGRAM:

#include<stdio.h>
int main()
{
int i, j, m1[10][10], m2[10][10], sum[10][10];
printf ("Enter the elements of first matrix\n");
for ( i = 0 ;i < 3 ; i++ )
{
printf ("\n Enter values for row %d \n",i+1);
for ( j = 0 ; j<3 ; j++ )
{
scanf ("%d",&m1[i][j]);
}
}
printf ("\n Enter the elements of second matrix\n");
for ( i = 0 ;i < 3; i++ )
{
printf ("\n Enter values for row %d \n",i+1);
for ( j = 0 ; j< 3 ; j++ )
{
scanf ("%d",&m2[i][j]);
}
}
printf ("Sum of two matrices \n");
for ( i = 0 ;i < 3 ; i++ )
{
for ( j = 0 ; j<3 ; j++ )
{
sum[i][j] = m1[i][j]+m2[i][j];
printf ("%d \t", sum[i][j]);
}
4213 - KCET DEPARTMENT OF ECE - III - SEMESTER Page 17
CS3362 -C PROGRAMMING & DATASTRUCTURE LAB MANUAL
printf ("\n");
}
}

OUTPUT:

Enter the elements of first matrix


Enter values for row 1
1 2 3
Enter values for row 2
2 3 4
Enter values for row 3
2 3 1

Enter the elements of second matrix


Enter values for row 1
5 4 3
Enter values for row 2
2 3 4
Enter values for row 3
4 5 6

Sum of two matrices


6 6 6
4 6 8
6 8 7

OBS

REC

VIVA

TOTAL

RESULT:

Thus the above program to find the sum of two matrix has been executed successfully and the
output was verified.

4213 - KCET DEPARTMENT OF ECE - III - SEMESTER Page 18


CS3362 -C PROGRAMMING & DATASTRUCTURE LAB MANUAL

EX.NO: 2 D /*Functions using call by value */ DATE:

AIM: To write a C program to swap the values using function.

ALGORITHM:

STEP 1: Start
STEP 2: Initialize the variables
STEP 3: Get the input value
STEP 4: Perform the operation
STEP 5: Print Result
STEP 6: Stop

PROGRAM:

#include <stdio.h>
swap (int, int);
main()
{
int a, b;
printf("\n Enter value of a & b: ");
scanf("%d %d", &a, &b);
printf("\n Before Swapping:\n");
printf("\na = %d\n\nb = %d\n", a, b);
swap(a, b);
printf("\n After Swapping:\n");
printf("\na = %d\n\nb = %d", a, b);
getch();
}
swap (int a, int b)
{
int temp;
temp = a;
a = b;
b = temp; OBS
}
OUTPUT: REC
Enter value of a & b: 2 3 VIVA
Before swapping: 2 3
After swapping: 2 3 TOTAL

RESULT:

Thus the above program to swap the values using function has been executed successfully and
the output was verified.

4213 - KCET DEPARTMENT OF ECE - III - SEMESTER Page 19


CS3362 -C PROGRAMMING & DATASTRUCTURE LAB MANUAL
EX.NO: 2 E /*Functions using call by refernce */ DATE:

AIM: To write a C program to swap the values using function.

ALGORITHM:

STEP 1: Start
STEP 2: Initialize the variables
STEP 3: Get the input value
STEP 4: Perform the operation
STEP 5: Print Result
STEP 6: Stop

PROGRAM:

#include<stdio.h>
#include<conio.h>
void swapnum ( int *var1, int *var2 )
{
int tempnum ;
tempnum = *var1 ;
*var1 = *var2 ;
*var2 = tempnum ;
}
int main( )
{
int num1 = 35, num2 = 45 ;
clrscr();
printf("Before swapping:");
printf("\nnum1 value is %d", num1);
printf("\nnum2 value is %d", num2);

/*calling swap function*/


swapnum( &num1, &num2 );

printf("\nAfter swapping:");

4213 - KCET DEPARTMENT OF ECE - III - SEMESTER Page 20


CS3362 -C PROGRAMMING & DATASTRUCTURE LAB MANUAL
printf("\nnum1 value is %d", num1);
printf("\nnum2 value is %d", num2);
getch();
return 0;
}

OUTPUT:
Before swapping:
num1 value is 35
num2 value is 45
After swapping:
num1 value is 45
num2 value is 35

OBS

REC

VIVA

TOTAL

RESULT:

Thus the above program to swap the values using function has been executed successfully and
the output was verified.

4213 - KCET DEPARTMENT OF ECE - III - SEMESTER Page 21


CS3362 -C PROGRAMMING & DATASTRUCTURE LAB MANUAL

EX.NO: 2F /*Factorial using Recursion */ DATE:

AIM: To write a C program to find the factorial using recursion.

ALGORITHM:

STEP 1: Start
STEP 2: Initialize the variables
STEP 3: Get the input value
STEP 4: Perform the operation
STEP 5: Print Result
STEP 6: Stop

PROGRAM:

#include <stdio.h>
long fact(int);
main()
{
int n;
long f;
printf("\nEnter number to find factorial: ");
scanf("%d", &n);
f = fact(n);
printf("\nFactorial: %ld", f);
getch();
}
long fact(int n)
{
int m;
if (n == 1)
return n;
else
return n * fact(n-1);
OBS
}
REC
OUTPUT:
Enter number to find factorial: 5 VIVA
Factorial: 120 TOTAL

RESULT:

Thus the above program to print the factorial using recursion has been executed successfully
and the output was verified.

4213 - KCET DEPARTMENT OF ECE - III - SEMESTER Page 22


CS3362 -C PROGRAMMING & DATASTRUCTURE LAB MANUAL

EX.NO: 3A /*Program using Pointer */ DATE:

AIM: To write a C program using Pointers.

ALGORITHM:

STEP 1: Start
STEP 2: Initialize the variables
STEP 3: Get the input value
STEP 4: Perform the operation
STEP 5: Print Result
STEP 6: Stop

PROGRAM:

#include<stdio.h>
#include<conio.h>
int main(){
int number=50;
int *p;//pointer to int
int **p2;//pointer to pointer
p=&number;//stores the address of number variable
p2=&p;
printf("Address of number variable is %x \n",&number);
printf("Address of p variable is %x \n",p);
printf("Value of *p variable is %d \n",*p);
printf("Address of p2 variable is %x \n",p2);
printf("Value of **p2 variable is %d \n",*p);
getch();
return 0;
}

OUTPUT:

Address of number variable is fff4


Address of p variable is fff4
Value of *p variable is 50
Address of p2 variable is fff2
Value of **p variable is 50

4213 - KCET DEPARTMENT OF ECE - III - SEMESTER Page 23


CS3362 -C PROGRAMMING & DATASTRUCTURE LAB MANUAL

OBS

REC

VIVA

TOTAL

RESULT:

Thus the above program comparison of string using pointer has been executed successfully and the
output was verified

4213 - KCET DEPARTMENT OF ECE - III - SEMESTER Page 24


CS3362 -C PROGRAMMING & DATASTRUCTURE LAB MANUAL
EX.NO: 3B /* Compare Strings Using Pointers */ DATE:
Aim:
To implement comparison two Strings using pointer

ALGORITHM:

STEP 1: Start
STEP 2: Initialize the variables
STEP 3: Get the input value
STEP 4: Perform the operation
STEP 5: Print Result
STEP 6: Stop

PROGRAM:

#include<stdio.h>
#include<conio.h>
int compare_string(char *a, char *b)
{
while(*a==*b)
{
if ( *a == '\0' || *b == '\0' )
break;

a++;
b++;
}
if( *a == '\0' && *b == '\0' )
return 0;
else
return -1;
}
int main()
{
char first[100], second[100], result;
printf("Enter First String: ");
gets(first);
printf("\nEnter Second String: ");
gets(second);
result = compare_string(first, second);
if (result == 0 )
printf("\nStrings are Equal.\n");
else
printf("\nStrings are NOT Equal.\n”);

4213 - KCET DEPARTMENT OF ECE - III - SEMESTER Page 25


CS3362 -C PROGRAMMING & DATASTRUCTURE LAB MANUAL
getch();
return 0;
}

OUTPUT:

Enter First String: computer

Enter Second String: Network

Strings are NOT Equal

OBS

REC

VIVA

TOTAL

RESULT:

Thus the above program comparison of string using pointer has been executed successfully and the
output was verified

4213 - KCET DEPARTMENT OF ECE - III - SEMESTER Page 26


CS3362 -C PROGRAMMING & DATASTRUCTURE LAB MANUAL
EX.NO: 3C /* Finding Smallest number using Pointer */ DATE:

Aim:
To Write a C program to find smallest number using pointer

ALGORITHM:

STEP 1: Start
STEP 2: Initialize the variables
STEP 3: Get the input value
STEP 4: Perform the operation
STEP 5: Print Result
STEP 6: Stop

PROGRAM :

#include<stdio.h>
#include<conio.h>
int main()
{
int a[5],*s,i,small;
s=&a[0];
printf("Enter 5-Elements :\n\n ");
for(i=0;i<5;i++,s++)
scanf("%d",s);
s=&a[0];
small=*s;
for(i=0;i<5;i++,s++)
if(*s<small)
small=*s;
printf("\nSmallest Element : %d",small);
getch();
return 0;
}

4213 - KCET DEPARTMENT OF ECE - III - SEMESTER Page 27


CS3362 -C PROGRAMMING & DATASTRUCTURE LAB MANUAL

OUTPUT:

Enter 5-Elements :

55

65

75

150

44

Smallest Element : 44

OBS

REC

VIVA

TOTAL

RESULT:

Thus the above program finding smallest number using pointer has been executed successfully and
the output was verified.

4213 - KCET DEPARTMENT OF ECE - III - SEMESTER Page 28


CS3362 -C PROGRAMMING & DATASTRUCTURE LAB MANUAL
EX.NO: 3D /* Empolyee details using structure */ DATE:

Aim:
To Write a C program for getting Empolyee details using structure

ALGORITHM:

STEP 1: Start
STEP 2: Initialize the variables
STEP 3: Get the input value
STEP 4: Perform the operation
STEP 5: Print Result
STEP 6: Stop

PROGRAM:

#include<stdio.h>
#include <string.h>
struct employee
{ int id;
char name[50];
float salary;
}e1,e2; //declaring e1 and e2 variables for structure
int main( )
{
//store first employee information
e1.id=101;
strcpy(e1.name, "Sonoo Jaiswal");//copying string into char array
e1.salary=56000;

//store second employee information


e2.id=102;
strcpy(e2.name, "James Bond");
e2.salary=126000;

//printing first employee information


printf( "employee 1 id : %d\n", e1.id);
printf( "employee 1 name : %s\n", e1.name);
printf( "employee 1 salary : %f\n", e1.salary);

4213 - KCET DEPARTMENT OF ECE - III - SEMESTER Page 29


CS3362 -C PROGRAMMING & DATASTRUCTURE LAB MANUAL

//printing second employee information


printf( "employee 2 id : %d\n", e2.id);
printf( "employee 2 name : %s\n", e2.name);
printf( "employee 2 salary : %f\n", e2.salary);
return 0;
}

OUTPUT

employee 1 id : 101
employee 1 name : Sonoo Jaiswal
employee 1 salary : 56000.000000
employee 2 id : 102
employee 2 name : James Bond
employee 2 salary : 126000.000000

OBS

REC

VIVA

TOTAL

RESULT:

Thus the above program for getting employee details using structure has been executed successfully
and the output was verified.

4213 - KCET DEPARTMENT OF ECE - III - SEMESTER Page 30


CS3362 -C PROGRAMMING & DATASTRUCTURE LAB MANUAL
EX.NO: 3E /* Employee details using Nested structure */ DATE:

Aim:

To Write a C program for getting Employee details using nested structure

ALGORITHM:

STEP 1: Start
STEP 2: Initialize the variables
STEP 3: Get the input value
STEP 4: Perform the operation
STEP 5: Print Result
STEP 6: Stop

PROGRAM:

#include <stdio.h>
#include <string.h>
struct Employee
{
int id;
char name[20];
struct Date
{
int dd;
int mm;
int yyyy;
}doj;
}e1;
int main( )
{
//storing employee information
e1.id=101;
strcpy(e1.name, "Sonoo Jaiswal");//copying string into char array
e1.doj.dd=10;
e1.doj.mm=11;
e1.doj.yyyy=2014;

4213 - KCET DEPARTMENT OF ECE - III - SEMESTER Page 31


CS3362 -C PROGRAMMING & DATASTRUCTURE LAB MANUAL
//printing first employee information
printf( "employee id : %d\n", e1.id);
printf( "employee name : %s\n", e1.name);
printf( "employee date of joining (dd/mm/yyyy) : %d/%d/%d\n", e1.doj.dd,e1.doj.mm,e1.doj.yyy
y);
return 0;
}

OUTPUT:
employee id : 101
employee name : Sonoo Jaiswal
employee date of joining (dd/mm/yyyy) : 10/11/2014

OBS

REC

VIVA

TOTAL

RESULT:

Thus the above program for getting employee details using Nested structure has been executed
successfully and the output was verified.

4213 - KCET DEPARTMENT OF ECE - III - SEMESTER Page 32


CS3362 -C PROGRAMMING & DATASTRUCTURE LAB MANUAL
EX.NO: 4 /* To Store Employees details using File Handling */ DATE:

Aim:

To Write a C program to store Employee details using File Handling .

ALGORITHM:

STEP 1: Start
STEP 2: Initialize the variables
STEP 3: Get the input value
STEP 4: Perform the operation
STEP 5: Print Result
STEP 6: Stop

PROGRAM:

#include <stdio.h>

void main()

FILE *fptr;

int id;

char name[30];

float salary;

fptr = fopen("emp.txt", "w+");/* open for writing */

if (fptr == NULL)

printf("File does not exists \n");

return;

printf("Enter the id\n");

scanf("%d", &id);

fprintf(fptr, "Id= %d\n", id);

printf("Enter the name \n");

scanf("%s", name);

4213 - KCET DEPARTMENT OF ECE - III - SEMESTER Page 33


CS3362 -C PROGRAMMING & DATASTRUCTURE LAB MANUAL
fprintf(fptr, "Name= %s\n", name);

printf("Enter the salary\n");

scanf("%f", &salary);

fprintf(fptr, "Salary= %.2f\n", salary);

fclose(fptr);

OUTPUT:

Enter the id

Enter the name

sonoo

Enter the salary

120000

emp.txt

Id= 1

Name= sonoo

Salary= 120000

OBS

REC

VIVA

TOTAL

RESULT:

Thus the above program for storing employee details using File Handling has been executed
successfully and the output was verified.

4213 - KCET DEPARTMENT OF ECE - III - SEMESTER Page 34


CS3362 -C PROGRAMMING & DATASTRUCTURE LAB MANUAL
EX.NO: 5 /*RAILWAY RESERVATION*/ DATE:

AIM:

To create a railway reservation system with following modules,

Booking

Availability checking

Cancellation

Prepare Chart

ALGORITHM

STEP 1: Start

STEP 2: Read the choice,seat,name,age,char,sex

STEP 3: Use to function add(),printset(),delete()

STEP 4: Ticket to ad function

STEP 5: Read input1,input2

STEP 6: if a->seat>70

STEP 7: Print Train full

STEP 8: elseif a->seat >50

STEP 9: Print waiting available

STEP 10: if input2==1 return

STEP 11: Print the name, age, sex, seat;

STEP 12: endif

STEP 13: Ticket to print seat

STEP 14: if temp->seat->50

STEP 15: Print waiting seat

STEP 16: else print confirm seat

STEP 17: endif

STEP 18: Delete function

STEP 19: Read input,tep a->next


4213 - KCET DEPARTMENT OF ECE - III - SEMESTER Page 35
CS3362 -C PROGRAMMING & DATASTRUCTURE LAB MANUAL
STEP 20: Enter the seat number

STEP 21: while a1=NULL && a->seat !=input

STEP 22: Print cannot delete

STEP 23: end while

STEP 24: Stop

PROGRAM:

#include<stdio.h>
#include<stdlib.h>
typedef struct node
{
int seat;
char name[15];
int age;
char sex;
struct node* next;
}list,*ptr;
void delete(ptr a);
void add(ptr a);
void printseat(ptr temp);
void print(ptr a);
void delete(ptr a);
int main()
{
int i,input;
ptr a;
a=(ptr)malloc(1*sizeof(list)); // dummy node
a->next=NULL;
a->seat=0;
for(i=0;i<100;i++)
{
printf(“\n 1 Book a ticket \n2 Deleting a TICKET \n3 Print the chart \n4 Exit the loop \n");
scanf("%d",&input);
if(input==1)
{
add(a);
}
else if(input==2)
{
delete(a);
}
else if(input==3)
{
print(a);
}
else if(input==4)
{

4213 - KCET DEPARTMENT OF ECE - III - SEMESTER Page 36


CS3362 -C PROGRAMMING & DATASTRUCTURE LAB MANUAL
break;
}
else
{
printf("not able to recognise the command \n");
}

}
printf("thank you \n");
return 0;
}
void add(ptr a)
{
int input,input2;
ptr temp=NULL;
while(a->next!=NULL)
{
a=a->next;
}
if(a->seat>70)
{
printf("train full \n");
return ;
}
else if(a->seat>50)
{
printf("%d waiting available \n",(a->seat)-50);
printf("enter 1 to exit 0 to continue ");
scanf("%d",&input2);
if(input2==1)
{
return ;
}
}
temp=(ptr)malloc(1*sizeof(list));
temp->next=NULL;
printf("enter name ");
scanf("%s",temp->name);
printf("enter age ");
scanf("%d",&temp->age);
printf("enter sex ");
scanf("%s",&temp->sex);
temp->seat=(a->seat)+1;
printf("Enter 1 to print the ticket \n 0 for not to print the ticket \n");
scanf("%d",&input);
if(input==1)
{
printseat(temp);
}
a->next=temp;
return ;

4213 - KCET DEPARTMENT OF ECE - III - SEMESTER Page 37


CS3362 -C PROGRAMMING & DATASTRUCTURE LAB MANUAL
}
void printseat(ptr temp)
{
if(temp->seat>50)
{
printf("WL=%d",(temp->seat)-50);
}
Else
{
printf("confirm seat=%d",temp->seat);
}
printf(" name=%s ",temp->name);
printf("sex=%c ",temp->sex);
printf("age=%d \n",temp->age);
return ;
}
void print(ptr a)
{
a=a->next;
while(a!=NULL)
{
printf("seat number=%d,name=%s ,age=%d,sex=%c \n",a->seat,a->name,a->age,a->sex);
a=a->next;
}
return ;
}
void delete(ptr a)
{
int input;
ptr temp;
a=a->next;
printf("enter the seat number ");
scanf("%d",&input);
while(a!=NULL&&(a->seat)!=input)
{
a=a->next;
}
if(a==NULL)
{
printf("can not delete \n");
return ;
}
temp=a;
a=a->next;
free(temp);
while(a!=NULL)
{
a->seat=a->seat-1;
a=a->next;
}
printf("seat deleted \n");

4213 - KCET DEPARTMENT OF ECE - III - SEMESTER Page 38


CS3362 -C PROGRAMMING & DATASTRUCTURE LAB MANUAL
return ;
}

OUTPUT:

1 Book a ticket
2 Deleting a TICKET
3 Print the chart
4 Exit the loop
Enter name:Ram
Enter age:20
Enter sex:male
Enter 1 to print the ticket
0 for not to print the ticket
0
1 Book a ticket
2 Deleting a TICKET
3 Print the chart
4 Exit the loop
1
Enter name:Balaji
Enter age:50
Enter sex:male
Enter 1 to print the ticket
0 for not to print the ticket

1 Book a ticket
2 Deleting a TICKET
3 Print the chart
4 Exit the loop
3
Seat number=1,name=Ram,age=20,sex=male OBS
Seat number=2,name=Balaji,age=50,sex=male
REC

VIVA

TOTAL

RESULT:
Thus above program for Railway Reservation has been executed Successfully.

4213 - KCET DEPARTMENT OF ECE - III - SEMESTER Page 39


CS3362 -C PROGRAMMING & DATASTRUCTURE LAB MANUAL
EX.NO: 6 /*Implementation of List using Array */ DATE:

AIM :
To perform various operations on List ADT using array implementation.

ALGORITHM :
1. Start
2. Create a list of n elements
3. Display list operations as a menu
4. Accept user choice
5. If choice = 1 then
▪ Get position of element to be deleted
▪ Move elements one position upwards thereon. Decrement length of the list Else if
choice = 2
▪ Get position of element to be inserted. Increment length of the list
▪ Move elements one position downwards thereon Store the new element in
corresponding position
Else if choice = 3
▪ Traverse the list and inspect each element Report position if it exists.
6. Stop

PROGRAM :
#include <stdio.h>
#include <conio.h>
void create();
void insert();
void search();
void deletion();
void display();
int i, e, n, pos;
static int b[50];
main()
{
int ch;
char g = 'y';
create();
do
{
printf("\n List Operations");
printf("\n 1.Deletion\n 2.Insert\n 3.Search\n 4.Exit\n");
printf("Enter your choice: ");
scanf("%d", &ch);
switch(ch)
4213 - KCET DEPARTMENT OF ECE - III - SEMESTER Page 40
CS3362 -C PROGRAMMING & DATASTRUCTURE LAB MANUAL
{
case 1:
deletion();
break;
case 2:
insert();
break;
case 3:
search();
break;
case 4: exit(0);
break;
default:printf("\n Enter the correct choice:");
}
printf("Do you want to continue: ");
fflush(stdin);
scanf("\n %c",&g);
}
while(g=='y' || g=='Y');
getch();
}
void create()
{
printf("\n Enter the number of elements:");
scanf("%d",&n);
printf("\n Enter list elements: ");
for(i=0; i<n; i++)
scanf("%d", &b[i]);
}
void deletion()
{
printf("\n enter the position you want to delete: "); scanf("%d", &pos);
if(pos >= n)
printf("\n Invalid location");
else
{
for(i=pos+1; i<n; i++)
b[i-1] = b[i];
n--;
printf("List elements after deletion");
display();
}
}

4213 - KCET DEPARTMENT OF ECE - III - SEMESTER Page 41


CS3362 -C PROGRAMMING & DATASTRUCTURE LAB MANUAL
void search()
{
int flag = 0;
printf("\n Enter the element to be searched: ");
scanf("%d", &e);
for(i=0; i<n; i++)
{
if(b[i] == e)
{
flag = 1;
printf("Element is in the %d position", i);
break;
}
}
if(flag == 0)
printf("Value %d is not in the list", e);
}
void insert()
{
printf("\n Enter the position you need to insert: ");
scanf("%d", &pos);
if(pos >= n)
printf("\n Invalid location");
else
{
++n;
for(i=n; i>pos; i--)
b[i] = b[i-1];
printf("\n Enter the element to insert: ");
scanf("%d", &e);
b[pos] = e;
}
printf("\n List after insertion:");
display();
}
void display()
{
for(i=0; i<n; i++)
printf("\n %d", b[i]);
}

4213 - KCET DEPARTMENT OF ECE - III - SEMESTER Page 42


CS3362 -C PROGRAMMING & DATASTRUCTURE LAB MANUAL
OUTPUT
Enter the number of elements: 5
Enter list elements: 12 23 34 45 56

List Operations
1.Deletion 2.Insert 3.Search 4.Exit
Enter your choice:2
Enter the position you need to insert: 1
Enter the element to insert: 99
List after insertion:
12
99
23
34
45
56
Do you want to continue: y

List Operations 1.Deletion 2.Insert 3.Search Exit


Enter your choice:1
Enter the position you want to delete: 3
Elements after deletion
12
99
23
45 OBS
56
REC
Do you want to continue: n
VIVA

TOTAL

RESULT :
Thus various operations were successfully executed on list using array implementation.

4213 - KCET DEPARTMENT OF ECE - III - SEMESTER Page 43


CS3362 -C PROGRAMMING & DATASTRUCTURE LAB MANUAL

EX.NO: 7 a) /*Implementation of Stack using Array */ DATE:

AIM
To implement stack operations using array.

ALGORITHM
1. Start
2. Define a array stack of size max = 5
3. Initialize top = -1
4. Display a menu listing stack operations
5. Accept choice
6. If choice = 1 then
If top < max -1
Increment top
Store element at current position of top
Else
Print Stack overflow
Else If choice = 2 then
If top < 0 then
Print Stack underflow
Else
Display current top element Decrement top
Else If choice = 3 then
Display stack elements starting from top
7. Stop

PROGRAM
/* Stack Operation using Arrays */
#include <stdio.h>
#include <conio.h>
#define max 5
static int stack[max];
int top = -1;

void push(int x)
{
stack[++top] = x;
}
int pop()
{
return (stack[top--]);
}

4213 - KCET DEPARTMENT OF ECE - III - SEMESTER Page 44


CS3362 -C PROGRAMMING & DATASTRUCTURE LAB MANUAL
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;
clrscr();
while(ch != 4)
{
printf("\n STACK OPERATION \n");
printf("1.PUSH ");
printf("2.POP ");
printf("3.VIEW ");
printf("4.QUIT \n");
printf("Enter Choice : ");
scanf("%d", &ch);
switch(ch)
{
case 1:
if(top < max-1)
{
printf("\nEnter Stack element : ");
scanf("%d", &val);
push(val);
}
else
printf("\n Stack Overflow \n");
break;
case 2:
if(top < 0)

4213 - KCET DEPARTMENT OF ECE - III - SEMESTER Page 45


CS3362 -C PROGRAMMING & DATASTRUCTURE LAB MANUAL
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);
break;
default:
printf("\n Invalid Choice \n");
}
}
}

OUTPUT
STACK OPERATION
1.PUSH 2.POP 3.VIEW 4.QUIT
Enter Choice : 1
Enter Stack element : 12

STACK OPERATION
1.PUSH 2.POP 3.VIEW 4.QUIT
Enter Choice : 1
Enter Stack element : 23

STACK OPERATION
1.PUSH 2.POP 3.VIEW 4.QUIT
Enter Choice : 1
Enter Stack element : 12

STACK OPERATION
1.PUSH 2.POP 3.VIEW 4.QUIT
Enter Choice : 1
Enter Stack element : 23

STACK OPERATION
1.PUSH 2.POP 3.VIEW 4.QUIT
Enter Choice : 1

4213 - KCET DEPARTMENT OF ECE - III - SEMESTER Page 46


CS3362 -C PROGRAMMING & DATASTRUCTURE LAB MANUAL
Enter Stack element : 34

STACK OPERATION
1.PUSH 2.POP 3.VIEW 4.QUIT
Enter Choice : 1
Enter Stack element : 45

STACK OPERATION
1.PUSH 2.POP 3.VIEW 4.QUIT
Enter Choice : 3
Top--> 45 34 23 12

STACK OPERATION
1.PUSH 2.POP 3.VIEW 4.QUIT
Enter Choice : 2
Popped element is 45

STACK OPERATION
1.PUSH 2.POP 3.VIEW 4.QUIT
Enter Choice : 3
Top--> 34 23 12

STACK OPERATION
1. PUSH 2.POP 3.VIEW 4.QUIT
Enter Choice : 4

OBS

REC

VIVA

TOTAL

RESULT :
Thus push and pop operations of a stack was demonstrated using arrays.

4213 - KCET DEPARTMENT OF ECE - III - SEMESTER Page 47


CS3362 -C PROGRAMMING & DATASTRUCTURE LAB MANUAL
EX.NO: 7 b) /*Implementation of Queue using Array */ DATE:

AIM
To implement queue operations using array.

ALGORITHM
1. Start
2. Define a array queue of size max = 5
3. Initialize front = rear = –1
4. Display a menu listing queue operations
5. Accept choice
6. If choice = 1 then
If rear < max -1
Increment rear
Store element at current position of rear
Else
Print Queue Full
Else If choice = 2 then
If front = –1 then
Print Queue empty
Else
Display current front element Increment front
Else If choice = 3 then
Display queue elements starting from front to rear.
7. Stop

PROGRAM
/* Queue Operation using Arrays */
#include <stdio.h>
#include <conio.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];

4213 - KCET DEPARTMENT OF ECE - III - SEMESTER Page 48


CS3362 -C PROGRAMMING & DATASTRUCTURE LAB MANUAL
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;
clrscr();
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);
}

4213 - KCET DEPARTMENT OF ECE - III - SEMESTER Page 49


CS3362 -C PROGRAMMING & DATASTRUCTURE LAB MANUAL
else
printf("\n Queue Full \n");
break;
case 2:
if(front == -1)
printf("\n Queue Empty \n");
else
{
val = remove();
printf("\n Element deleted : %d \n", val);
}
break;
case 3:
view();
break;
case 4: exit(0);
break;
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

4213 - KCET DEPARTMENT OF ECE - III - SEMESTER Page 50


CS3362 -C PROGRAMMING & DATASTRUCTURE LAB MANUAL
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

OBS

REC

VIVA

TOTAL

RESULT
Thus insert and delete operations of a queue was demonstrated using arrays.

4213 - KCET DEPARTMENT OF ECE - III - SEMESTER Page 51


CS3362 -C PROGRAMMING & DATASTRUCTURE LAB MANUAL

EX.NO: 8 a) /*Implementation of Singly Linked List */ DATE:

AIM
To write a C program to implement Singly Linked List.

ALGORITHM
1. Start the program
2. Create a structure for the Node with a Data part and a Address part containing the
address of the next element.
3. Initialize the variables and functions.
4. Using the Switch case, call the insert or delete or display functions.
5. The insertion operation can be done either in the first or middle or Last. 6. The
new node to be inserted is first created at runtime using the malloc function. 7. The
new node is inserted into the list by changing the head pointers
8. To insert the node in the middle of the list,
a) The node after which the new node has to insert is obtained.
b) The list is traversed until the noe3 is 3ndoun5343e.
c) The new node is inserted by changing the links.
9. To insert the node at the last,
a) The list is traversed until the node is encountered.
b) The new node is inserted by changing the links.
10. The deletion operation is done at three places.
11. The node at the front is deleted from the list by changing the head
pointers. 12. To delete an element from the middle of the list,
a) Traverse the list until the node which has to be deleted is encountered.
b) Remove the node by changing the links.
13. To delete an element at the last,
a) Traverse the list until the last node is encountered.
b) Remove the node by changing the links.
14. In display operation all the elements are displayed by traversing the list using a
while loop until the last element.
15. Stop the execution.

PROGRAM
#include<stdio.h>
#include<conio.h>
#define null 0
typedef struct n
{
int data;
struct n*next;
}
node;

4213 - KCET DEPARTMENT OF ECE - III - SEMESTER Page 52


CS3362 -C PROGRAMMING & DATASTRUCTURE LAB MANUAL
node*head=null,*newn,*temp,*t;
void insert();
void delete();
void view();

void main()
{
int choice;
clrscr();
printf("\n\t\t SINGLY LINKED LIST");
while (1)
{
printf("\n 1.Insert\t 2.Delete\t 3.View");
printf("\n Enter the choice:");
scanf("%d", &choice);
switch(choice)
{
case 1:insert();
break;
case 2:delete();
break;
case 3:view();
break;
case 4:exit(0);
break;
default: printf("\n Enter the correct option");
}
}
}
void insert()
{
int option,no;
newn=(node*)malloc(sizeof(node));
printf("Enter the data to be inserted : ");
scanf("%d",&newn->data);
newn->next=null;
printf("\n 1.Insert first\t 2.Insert middle\t 3.Insert last");
printf("\n Enter the option:");
scanf("%d", &option);
switch (option)
{
case 1:if(head==null)
head=newn;

4213 - KCET DEPARTMENT OF ECE - III - SEMESTER Page 53


CS3362 -C PROGRAMMING & DATASTRUCTURE LAB MANUAL
else
{
newn-> next=head;
head=newn;
}
break;
case 2:if(head-> next==null)
printf("single entry list cannot perform insertion in the middle");
else
{
printf("\n entry the data which the node has to insert");
scanf("%d",&no);
temp=head;
while(temp->data!=no)
{
temp=temp-> next;
}
newn->next=temp->next;
temp->next = newn;
}
break;
case 3:temp=head;
if(head == null)
head=newn;
else
{
temp=head;
while(temp->next!=null)
{
temp=temp->next;
}
temp->next = newn;
}
break;
}
}
void delete()
{
int option,no;
if(head==null)
{
printf("empty list cannot perform deletion");
}

4213 - KCET DEPARTMENT OF ECE - III - SEMESTER Page 54


CS3362 -C PROGRAMMING & DATASTRUCTURE LAB MANUAL
printf("1.Deletion at first\t 2.Deletion in middle\t 3.Deletion at
last\n"); printf(“Enter the Option : “);
scanf("%d",&option);
switch (option)
{
case 1: temp=head;
head =head->next;
printf("The data deleted is %d",temp ->data);
free(temp);
break;
case 2:if(head->next == null)
printf("single entry list cannot perform deletion at middle");
else
{
printf("Enter the data which has to be deleted : ");
scanf("%d",&no);
temp=head;
while(temp->data!=no)
{
t=temp;
temp=temp->next;
}
printf("the data deleted is %d",temp->data);
t->next=temp->next;
free(temp);
}
break;
case 3:temp=head;
if(head->next == null)
{
printf("the data deleted is%d",head->data);
head=null;
}
else
{
while(temp->next!=null)
{
t=temp;
temp=temp->next;
printf("the data deleted is%d",temp->data);
free (temp);
}
}

4213 - KCET DEPARTMENT OF ECE - III - SEMESTER Page 55


CS3362 -C PROGRAMMING & DATASTRUCTURE LAB MANUAL
break;
}
}
void view()
{
temp=head;
printf("Head-->");
while(temp!=null)
{
printf("%d->\t",temp->data);
temp=temp->next;
}
printf("NULL");
}

OUTPUT

SINGLY LINKED LIST


1.Insert 2.Delete 3.View
Enter the choice: 1
Enter the data to be inserted : 11
1.Insert first 2.Insert middle 3.Insert last
Enter the option : 1

1.Insert 2.Delete 3.View


Enter the choice: 1
Enter the data to be inserted : 22
1.Insert first 2.Insert middle 3.Insert last
Enter the option : 3

1.Insert 2.Delete 3.View


Enter the choice: 1
Enter the data to be inserted : 33
1.Insert first 2.Insert middle 3.Insert last
Enter the option : 2
Enter the data after which the node has to insert : 11

1.Insert 2.Delete 3.View


Enter the choice:3
Head-->11 -> 33 -> 22 -> Null

1.Insert 2.Delete 3.View


Enter the choice:2

4213 - KCET DEPARTMENT OF ECE - III - SEMESTER Page 56


CS3362 -C PROGRAMMING & DATASTRUCTURE LAB MANUAL
1.Deletion at first 2.Deletion in middle 3.Deletion at last
Enter the Option : 2
Enter the data after which has to be deleted 22
The data deleted is 22

OBS

REC

VIVA

TOTAL

RESULT
Thus the program to implement Singly Linked List has been executed and verified successfully.

4213 - KCET DEPARTMENT OF ECE - III - SEMESTER Page 57


CS3362 -C PROGRAMMING & DATASTRUCTURE LAB MANUAL

EX.NO: 8 b) /*Implementation of Stack using Linked List */ DATE:

AIM
To implement stack operations using linked list.

ALGORITHM
1. Start
2. Define a singly linked list node for stack
3. Create Head node
4. Display a menu listing stack operations
5. Accept choice
6. If choice = 1 then
Create a new node with data Make new node point to first node
Make head node point to new node Else If choice = 2 then
Make temp node point to first node
Make head node point to next of temp node Release memory
Else If choice = 3 then
Display stack elements starting from head node till null
7. Stop

PROGRAM
/* Stack using Single Linked List */
#include <stdio.h>
#include <conio.h>
#include <process.h>
#include <alloc.h>

struct node
{
int label;
struct node *next;
};

main()
{
int ch = 0; int k;
struct node *h, *temp, *head;

/* Head node construction */


head = (struct node*) malloc(sizeof(struct node));
head->next = NULL;

4213 - KCET DEPARTMENT OF ECE - III - SEMESTER Page 58


CS3362 -C PROGRAMMING & DATASTRUCTURE LAB MANUAL
while(1)
{
printf("\n Stack using Linked List \n");
printf("1->Push ");
printf("2->Pop");
printf("3->View");
printf("4->Exit \n");
printf("Enter your choice : ");
scanf("%d", &ch);

switch(ch)
{
case 1:
/* Create a new node */
temp=(struct node *)(malloc(sizeof(struct node)));
printf("Enter label for new node : ");
scanf("%d", &temp->label);
h = head;
temp->next = h->next;
h->next = temp;
break;

case 2:
/* Delink the first node */
h = head->next;
head->next = h->next;
printf("Node %s deleted\n", h->label);
free(h);
break;

case 3:
printf("\n HEAD -> ");
h = head;
/* Loop till last node */
while(h->next != NULL)
{
h = h->next;
printf("%d -> ",h->label);
}
printf("NULL \n");
break;
case 4: exit(0);
}
}

4213 - KCET DEPARTMENT OF ECE - III - SEMESTER Page 59


CS3362 -C PROGRAMMING & DATASTRUCTURE LAB MANUAL
}

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
.

OBS

REC

VIVA

TOTAL

RESULT

Thus push and pop operations of a stack was demonstrated using linked list.

4213 - KCET DEPARTMENT OF ECE - III - SEMESTER Page 60


CS3362 -C PROGRAMMING & DATASTRUCTURE LAB MANUAL
EX.NO: 8 C) /*Implementation of Queue using Linked List */ DATE:

AIM
To implement queue operations using linked list.

ALGORITHM
1. Start
2. Define a singly linked list node for stack
3. Create Head node
4. Display a menu listing stack operations
5. Accept choice
6. If choice = 1 then
Create a new node with data Make new node point to first node
Make head node point to new node Else If choice = 2 then
Make temp node point to first node
Make head node point to next of temp node Release memory
Else If choice = 3 then
Display stack elements starting from head node till null
7. Stop

PROGRAM
/* Queue using Single Linked List */
#include <stdio.h>
#include <conio.h>
#include <process.h>
#include <alloc.h>

struct node
{
int label;
struct node *next;
};

main()
{
int ch=0, k;
struct node *h, *temp, *head;

/* Head node construction */


head = (struct node*) malloc(sizeof(struct node));
head->next = NULL;

while(1)
{
printf("\n Queue using Linked List \n");
4213 - KCET DEPARTMENT OF ECE - III - SEMESTER Page 61
CS3362 -C PROGRAMMING & DATASTRUCTURE LAB MANUAL
printf("1->Insert ");
printf("2->Delete ");
printf("3->View ");
printf("4->Exit \n");
printf("Enter your choice : ");
scanf("%d", &ch);
switch(ch)
{
case 1:
/* Create a new node */
temp=(struct node *)(malloc(sizeof(struct node)));
printf("Enter label for new node : ");
scanf("%d", &temp->label);
/* Reorganize the links */
h = head;
while (h->next != NULL)
h = h->next;
h->next = temp;
temp->next = NULL;
break;

case 2:
/* Delink the first node */
h = head->next;
head->next = h->next;
printf("Node deleted \n");
free(h);
break;

case 3:
printf("\n\nHEAD -> ");
h=head;
while (h->next!=NULL)
{
h = h->next;
printf("%d -> ",h->label);
}
printf("NULL \n");
break;

case 4:
exit(0);
}

4213 - KCET DEPARTMENT OF ECE - III - SEMESTER Page 62


CS3362 -C PROGRAMMING & DATASTRUCTURE LAB MANUAL
}
}

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

OBS

REC

VIVA

TOTAL

RESULT
Thus insert and delete operations of a queue was demonstrated using linked list.

4213 - KCET DEPARTMENT OF ECE - III - SEMESTER Page 63


CS3362 -C PROGRAMMING & DATASTRUCTURE LAB MANUAL

EX.NO: 9 a) /* POLYNOMIAL ADDITION USING LINKED LIST */ DATE:

AIM

To write a ‘C’ Program to represent a polynomial as a linked list and to write functions for
polynomial addition.

ALGORITHM

1. Start the program.


2. Create a Structure for the link with a coefficient part, power part and address part which points
to the Next link.
3. Initialize the variables and functions.
4. Read the first node of polynomial (poly1) by calling create function to store the coefficient and
power values.
5. Read the second node of polynomial (poly2) by calling create function to store the coefficient
and power values.
6. Display both the first and second polynomials using show function.
7. Add both polynomials and store the result in another polynomial (poly3) and display the result.
8. Stop the execution.

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

struct link
{
int coeff;
int pow;
struct link *next;
};

struct link *poly1 = NULL,*poly2 = NULL,*poly = NULL;

void create(struct link *node)


{
char ch;

do
{
printf("\n Enter Co-efficient : ");

4213 - KCET DEPARTMENT OF ECE - III - SEMESTER Page 64


CS3362 -C PROGRAMMING & DATASTRUCTURE LAB MANUAL
scanf("%d",&node -> coeff);
printf("\n Enter Power : ");
scanf("%d",&node -> pow);
node -> next = (struct link*)malloc(sizeof(struct link));
node = node -> next;
node -> next = NULL;
printf("\n Continue (y/n) : ");
ch = getch();
}
while(ch = = 'y' || ch = = 'Y');
}

void show(struct link *node)


{
while(node -> next! = NULL)
{
printf("%dx^%d ", node -> coeff,node -> pow);
node = node -> next;
if(node -> next! = NULL)
printf("+");
}
}

void polyadd(struct link *poly1,struct link *poly2,struct link *poly)


{
while(poly1 -> next && poly2 -> next)
{
if(poly1 -> pow > poly2 -> pow)
{
poly -> pow = poly1 -> pow;
poly -> coeff = poly1 -> coeff;
poly1 = poly1 -> next;
}
else if(poly1 -> pow < poly2 -> pow)
{

poly -> pow = poly2 -> pow;


poly -> coeff = poly2 -> coeff;
poly2 = poly2 -> next;
}
else
{
Poly -> pow = poly1 -> pow;

4213 - KCET DEPARTMENT OF ECE - III - SEMESTER Page 65


CS3362 -C PROGRAMMING & DATASTRUCTURE LAB MANUAL
poly -> coeff = poly1 -> coeff + poly2 -> coeff;
poly1 = poly1 -> next;
poly2 = poly2 -> next;
}
poly -> next = (struct link *)malloc(sizeof(struct link));
poly = poly -> next;
poly -> next = NULL;
}
while(poly1 -> next || poly2 -> next)
{
if(poly1 -> next)
{
poly -> pow = poly1 -> pow;
poly -> coeff = poly1 -> coeff;
poly1 = poly1 -> next;
}
if(poly2 -> next)
{
poly -> pow = poly2 -> pow;
poly -> coeff = poly2 -> coeff;
poly2 = poly2 -> next;
}
poly -> next = (struct link *)malloc(sizeof(struct link));
poly = poly -> next;
poly -> next = NULL;
}
}
main()
{
char ch;
clrscr();
do
{
poly1 = (struct link *)malloc(sizeof(struct link));
poly2 = (struct link *)malloc(sizeof(struct link));
poly = (struct link *)malloc(sizeof(struct link));
printf("\n \n Enter 1st Polynomial ");
create(poly1);
printf("\n \n Enter 2nd Polynomial ");
create(poly2);
printf("\n \n The 1st Polynomial : ");
show(poly1);
printf("\n \n The 2nd Polynomial : ");

4213 - KCET DEPARTMENT OF ECE - III - SEMESTER Page 66


CS3362 -C PROGRAMMING & DATASTRUCTURE LAB MANUAL
show(poly2);
polyadd(poly1,poly2,poly);
printf("\n \n Added polynomial : ");
show(poly);
printf("\n \n Want to add two more numbers (Y/N) : ");
ch = getch();
}
while(ch = = 'y' || ch = = 'Y');
}

OUTPUT
Enter 1st Polynomial
Enter Co-efficient : 2
Enter Power : 3
Continue (y/n) :
Enter Co-efficient :3
Enter Power : 2
Continue (y/n) :
Enter Co-efficient : 4
Enter Power : 1
Continue (y/n) :

Enter 2nd Polynomial


Enter Co-efficient : 2
Enter Power : 3
Continue (y/n) :
Enter Co-efficient : 2
Enter Power : 1
Continue (y/n) :
Enter Co-efficient : 4
Enter Power : 0
Continue (y/n) :

The 1st Polynomial : 2x^3 +3x^2 +4x^1

The 2nd Polynomial : 2x^3 +2x^1 +4x^0

Added polynomial : 4x^3 +3x^2 +6x^1 +4x^0

Want to add two more numbers (Y/N) :

4213 - KCET DEPARTMENT OF ECE - III - SEMESTER Page 67


CS3362 -C PROGRAMMING & DATASTRUCTURE LAB MANUAL

OBS

REC

VIVA

TOTAL

RESULT
Thus the C Program to represent a polynomial as a linked list and to write functions for
polynomial addition has been executed and verified successfully.

4213 - KCET DEPARTMENT OF ECE - III - SEMESTER Page 68


CS3362 -C PROGRAMMING & DATASTRUCTURE LAB MANUAL

EX.NO: 9 b) /* CONVERSION OF INFIX TO POSTFIX EXPRESSION */ DATE:

AIM
To write a C program to convert the infix expression to postfix expression using stack
concept.

ALGORITHM

1. Start the program.


2. Get the Infix expression.
3. Read each character from infix expression.
4. If the character is variable, then push into the post array.
5. If the character is symbol, then push into the stack array.
6. If the character is closing parenthesis, then pop the symbols from the stack array and
push into the post array.
7. Repeat the steps 3, 4, 5, 6 for all the characters.
8. Then print the post array which gives the postfix expression.
9. Stop the program.

PROGRAM

#include<stdio.h>
#include<conio.h>
char inf[40],post[40];
int top=0,st[20];
void postfix();
void push();
char pop();
void main()
{
clrscr();
printf("\n\t\tCONVERSION OF INFIX TO POSTFIX EXPRESSION
\n\n"); printf("\n\tEnter the infix expression : ");
scanf("%s",inf);
postfix();

getch();
}
void postfix()
{
int i,j=0;
for(i=0;inf[i]!='\0';i++)

4213 - KCET DEPARTMENT OF ECE - III - SEMESTER Page 69


CS3362 -C PROGRAMMING & DATASTRUCTURE LAB MANUAL
{
switch(inf[i])
{
case '+':while(st[top]>=1)
post[i++]=pop();
push(1);
break;
case '-':while(st[top]>=1)
post[j++]=pop();
push(2);
break;
case '*':while(st[top]>=2)
post[j++]=pop();
push(3);
break;
case '/':while(st[top]>=2)
post[j++]=pop();
push(4);
break;
case '^':while(st[top]>=3)
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\tPostfix exp is =>\n\n\t %s",post);
}

void push(int ele)


{
top++;
st[top]=ele;
}
char pop()

4213 - KCET DEPARTMENT OF ECE - III - SEMESTER Page 70


CS3362 -C PROGRAMMING & DATASTRUCTURE LAB MANUAL
{
int el;
char e;
el=st[top];
top--;
switch(el)
{
case 1:e='+'; break;
case 2:e='-'; break;
case 3:e='*'; break;
case 4:e='/'; break;
case 5:e='^'; break;
}
return(e);
}

OUTPUT

CONVERSION OF INFIX TO POSTFIX EXPRESSION

Enter the infix expression : (a + b * c) / d

Postfix exp is => a b c * + d /

OBS

REC

VIVA

TOTAL

RESULT
Thus C program to convert the infix expression to postfix expression using stack
concept was verified successfully.

4213 - KCET DEPARTMENT OF ECE - III - SEMESTER Page 71


CS3362 -C PROGRAMMING & DATASTRUCTURE LAB MANUAL

EX.NO: 9 c) /* APPLICATION OF QUEUE - FCFS SCHEDULING */ DATE:

AIM
To schedule snapshot of processes queued according to FCFS scheduling.

Process Scheduling
▪ CPU scheduling is used in multiprogrammed operating systems.
▪ By switching CPU among processes, efficiency of the system can be improved. ▪
Some scheduling ALGORITHMs are FCFS, SJF, Priority, Round-Robin, etc. ▪ Gantt
chart provides a way of visualizing CPU scheduling and enables to understand better.

First Come First Serve (FCFS)


▪ Process that comes first is processed first
▪ FCFS scheduling is non-preemptive
▪ Not efficient as it RESULTs in long average waiting time.
▪ Can RESULT in starvation, if processes at beginning of the queue have long bursts.

ALGORITHM
1. Define an array of structure process with members pid, btime, wtime & ttime. 2.
Get length of the ready queue, i.e., number of process (say n)
3. Obtain btime for each process.
4. The wtime for first process is 0.
5. Compute wtime and ttime for each process as:
a. wtimei+1 = wtimei + btimei
b. ttimei = wtimei + btimei
6. Compute average waiting time awat and average turnaround time atur 7.
Display the btime, ttime and wtime for each process.
8. Display GANTT chart for the above scheduling
9. Display awat time and atur
10. Stop

PROGRAM
#include <stdio.h>
struct process
{
int pid;
int btime;
int wtime;
int ttime;
} p[10];

main()

4213 - KCET DEPARTMENT OF ECE - III - SEMESTER Page 72


CS3362 -C PROGRAMMING & DATASTRUCTURE LAB MANUAL
{
int i,j,k,n,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("\nProcess 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\nAverage waiting time : %5.2fms", awat);
printf("\nAverage turn around time : %5.2fms\n", atur);

printf("\n\nGANTT Chart\n"); printf("-");


for(i=0; i<(p[n-1].ttime + 2*n); i++)
printf("-");
printf("\n");

4213 - KCET DEPARTMENT OF ECE - III - SEMESTER Page 73


CS3362 -C PROGRAMMING & DATASTRUCTURE LAB MANUAL
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; j<p[i].btime; j++)
printf(" ");
printf("%2d",p[i].ttime);
}
}

OUTPUT
$ gcc fcfs.c
$ ./a.out
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 (in ms) : 11
Burst time for process P4 (in ms) : 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

4213 - KCET DEPARTMENT OF ECE - III - SEMESTER Page 74


CS3362 -C PROGRAMMING & DATASTRUCTURE LAB MANUAL

GANTT Chart
----------------------------------------
|P1 |P2 | P3 |P4 |
----------------------------------------
0 10 14 25 31

OBS

REC

VIVA

TOTAL

RESULT
Thus waiting time & turnaround time for processes based on FCFS scheduling was
computed and the average waiting time was determined.

4213 - KCET DEPARTMENT OF ECE - III - SEMESTER Page 75


CS3362 -C PROGRAMMING & DATASTRUCTURE LAB MANUAL

EX.NO: 10) /* IMPLEMENTATION OF BINARY TREE */ DATE:

AIM
To implement different types of traversal for the given binary tree.

ALGORITHM
1. Create a structure with key and 2 pointer variable left and right
2. Read the node to be inserted.
If (root==NULL)
root=node
else if (root->key < node->key)
root->right=NULL
else
Root->left=node
3. For Inorder Traversal
Traverse Left subtree
Visit root
Traverse Right subtree
4. For Preorder Traversal
Visit root
Traverse Left subtree
Traverse Right subtree
5. For Postorder Traversal
Traverse Left subtree
Traverse Right subtree
Visit root
6. Stop.

PROGRAM

#include <stdio.h>
#include <stdlib.h>
typedef struct node
{
int data;
struct node *left;
struct node *right;
}node;
int count=1;
node *insert(node *tree,int digit)

4213 - KCET DEPARTMENT OF ECE - III - SEMESTER Page 76


CS3362 -C PROGRAMMING & DATASTRUCTURE LAB MANUAL
{
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);
}

4213 - KCET DEPARTMENT OF ECE - III - SEMESTER Page 77


CS3362 -C PROGRAMMING & DATASTRUCTURE LAB MANUAL
}

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("\nThe preorder traversal of tree is:\n");
preorder(root);
printf("\nThe inorder traversal of tree is:\n");
inorder(root);
printf("\nThe postorder traversal of tree is:\n");
postorder(root);
getch();
}

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

OBS

REC

VIVA

TOTAL

RESULT
Thus three types of tree traversal were performed on the given binary tree.

4213 - KCET DEPARTMENT OF ECE - III - SEMESTER Page 78


CS3362 -C PROGRAMMING & DATASTRUCTURE LAB MANUAL
EX.NO: 11) /* IMPLEMENTATION OF BINARY SEARCH TREE */ DATE:

AIM
To write a C program to implement binary search tree traversal and its inorder, preorder, and
postorder traversals.

ALGORITHM
1. Start the program.
2. Create the tree structure with a data part and two address parts to hold the Address its right and
left child.
3. Initialize the variables and functions.
4. Create a root node.
5. Examine each node and place in its right position as left or right child.
a. if the element is less than the parent node, then place it as left child.
b. if the element is greater than the parent node, then place it as right child.
6. In the inorder traversal, each node in the tree is visited in the corresponding order left, root,
right recursively.
7. In the preorder traversal, each node in the tree is visited in the corresponding order root, left,
right recursively.
8. In the postorder traversal, each node in the tree is visited in the corresponding order left, right,
root recursively.
9. Stop the execution.

PROGRAM
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#define null 0
typedef struct btree
{
int data;
struct btree*left,*right;
}*NODEPTR,node;
NODEPTR maketree(int);
void setleft(NODEPTR,int);
void setright(NODEPTR,int);
void intra(NODEPTR);
void pretra(NODEPTR);
void posttra(NODEPTR);
void main()
{
NODEPTR tree,p,q;

4213 - KCET DEPARTMENT OF ECE - III - SEMESTER Page 79


CS3362 -C PROGRAMMING & DATASTRUCTURE LAB MANUAL
int no;
clrscr();
printf("\n\t\tBINARY SEARCH TREE TRAVERSAL");
printf("\nEnter the root value : ");
scanf("%d",&no);
tree=maketree(no);
printf("\nEnter the other nodes. Enter 0 to exit\n");
while(1)
{
scanf("%d",&no);
if(no==0)
break;
else
{
p=q=tree;
while(no!=p->data&&q!=null)
{
p=q;
if(no<p->data)
q=p->left;
else
q=p->right;
}
if(no==p->data)
printf("%d is duplicate \n",no);
if(no<p->data)
setleft(p,no);
else
setright(p,no);
}
}
printf("\nInorder traversal \n");
intra(tree);
printf("\nPreorder traversal \n");
pretra(tree);
printf("\nPostorder traversal \n" );
posttra(tree);
getch();

NODEPTR maketree(int no)


{

4213 - KCET DEPARTMENT OF ECE - III - SEMESTER Page 80


CS3362 -C PROGRAMMING & DATASTRUCTURE LAB MANUAL
NODEPTR p;
p=(node*)malloc(sizeof(node));
p->data=no;
p->left=null;
p->right=null;
return(p);
}

void setleft(NODEPTR p,int no)


{
if(p==null)
printf("Invalid insertion \n");
else if(p->left!=null)
printf("Invalid insertion \n");
else
p->left=maketree(no);
}

void setright(NODEPTR p,int no)


{
if(p==null)
printf("Invalid insertion \n");
else if(p->right!=null)
printf("Invalid insertion \n");
else
p->right=maketree(no);
}
void intra(NODEPTR p)
{
if(p!=null)
{
intra(p->left);
printf(" %d --> ",p->data);
intra (p->right);
}
}
void pretra(NODEPTR p)
{
if(p!=null)
{
printf(" %d --> ",p->data);
pretra(p->left);
pretra (p->right);

4213 - KCET DEPARTMENT OF ECE - III - SEMESTER Page 81


CS3362 -C PROGRAMMING & DATASTRUCTURE LAB MANUAL
}
}
void posttra(NODEPTR p)
{
if(p!=null)
{
posttra(p->left);
posttra (p->right);
printf(" %d --> ",p->data);

}
}

OUTPUT

BINARY SEARCH TREE TRAVERSAL


Enter the root value : 15
Enter the other nodes. Enter 0 to exit
12
10
45
13
5
59
48
0
Inorder traversal
5 --> 10 --> 12 --> 13 --> 15 --> 45 --> 48 --> 59 -->
Preorder traversal
15 --> 12 --> 10 --> 5 --> 13 --> 45 --> 59 --> 48 -->
Postorder traversal
5 --> 10 --> 13 --> 12 --> 48 --> 59 --> 45 --> 15 -->
OBS

REC

VIVA

TOTAL

RESULT
Thus program to implement binary search tree traversal and its inorder, preorder, and
postorder traversals.

4213 - KCET DEPARTMENT OF ECE - III - SEMESTER Page 82


CS3362 -C PROGRAMMING & DATASTRUCTURE LAB MANUAL

EX.NO: 12 A) /* LINEAR SEARCH */ DATE:

AIM
To perform linear search of an element on the given array.

ALGORITHM
1. Start
2. Read number of array elements n
3. Read array elements Ai, i = 0,1,2,…n–1
4. Read search value
5. Assign 0 to found
6. Check each array element against search
If Ai = search then found = 1
Print "Element found"
Print position i
Stop
7. If found = 0 then print "Element not found"
8. Stop

PROGRAM
/* Linear search on a sorted array */
#include <stdio.h>
#include <conio.h>
main()
{
int a[50],i, n, val, found;
clrscr();
printf("Enter number of elements : ");
scanf("%d", &n);
printf("Enter Array Elements : \n");
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;

4213 - KCET DEPARTMENT OF ECE - III - SEMESTER Page 83


CS3362 -C PROGRAMMING & DATASTRUCTURE LAB MANUAL
}
}
if (found == 0)
printf("\n Element not found");
getch();
}

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

OBS

REC

VIVA

TOTAL

RESULT
Thus an array was linearly searched for an element's existence.

4213 - KCET DEPARTMENT OF ECE - III - SEMESTER Page 84


CS3362 -C PROGRAMMING & DATASTRUCTURE LAB MANUAL

EX.NO: 12 b) /* BINARY SEARCH */ DATE:

AIM
To locate an element in a sorted array using Binary search method

ALGORITHM
1. Start
2. Read number of array elements, say n
3. Create an array arr consisting n sorted elements
4. Get element, say key to be located
5. Assign 0 to lower and n to upper
6. While (lower < upper)
Determine middle element mid = (upper+lower)/2
If key = arr[mid] then Print mid Stop
Else if key > arr[mid] then lower = mid + 1
Else upper = mid – 1
7. Print "Element not found"
8. Stop

PROGRAM
/* Binary Search on a sorted array */
#include <stdio.h>
#include <conio.h>
main()
{
int a[50],i, n, upper, lower, mid, val, found;
clrscr();
printf("Enter array size : ");
scanf("%d", &n);
for(i=0; i<n; i++)
a[i] = 2 * i;
printf("\n Elements in Sorted Order \n");
for(i=0; i<n; i++)
printf("%4d", a[i]);
printf("\n Enter element to locate : ");
scanf("%d", &val);
upper = n; lower = 0;
found = -1;

while (lower <= upper)


{

4213 - KCET DEPARTMENT OF ECE - III - SEMESTER Page 85


CS3362 -C PROGRAMMING & DATASTRUCTURE LAB MANUAL
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");
getch();
}

OUTPUT
Enter array size : 9
Elements in Sorted Order
0 2 4 6 8 10 12 14 16
Enter element to locate : 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
OBS

REC

VIVA

TOTAL

RESULT
Thus an element is located quickly using binary search method.

4213 - KCET DEPARTMENT OF ECE - III - SEMESTER Page 86


CS3362 -C PROGRAMMING & DATASTRUCTURE LAB MANUAL
EX.NO:13A) /* INSERTION SORT */ DATE:

AIM
To sort an array of N numbers using Insertion sort.

ALGORITHM
1. Start
2. Read number of array elements n
3. Read array elements Ai
4. Sort the elements using insertion sort
In pass p, move the element in position p left until its correct place is found among the
first p + 1 elements.
Element at position p is saved in temp, and all larger elements (prior to position p) are
moved one spot to the right. Then temp is placed in the correct spot.
5. Stop

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

4213 - KCET DEPARTMENT OF ECE - III - SEMESTER Page 87


CS3362 -C PROGRAMMING & DATASTRUCTURE LAB MANUAL
p++;
printf("\n After Pass %d: ", p);
for(k=0; k<n; k++)
printf("%d", a[k]);
}
printf("\n Sorted 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 Pass 1: 8 34 64 51 32 21
After Pass 2: 8 34 64 51 32 21
After Pass 3: 8 34 51 64 32 21
After Pass 4: 8 32 34 51 64 21
After Pass 5: 8 21 32 34 51 64
Sorted List : 8 21 32 34 51 64

OBS

REC

VIVA

TOTAL

RESULT
Thus array elements were sorted using insertion sort.

4213 - KCET DEPARTMENT OF ECE - III - SEMESTER Page 88


CS3362 -C PROGRAMMING & DATASTRUCTURE LAB MANUAL
EX.NO: 13 b) /* QUICK SORT */ DATE:

AIM
To sort an array of N numbers using Quick sort.

ALGORITHM
1. Start
2. Read number of array elements n
3. Read array elements Ai
4. Select an pivot element x from Ai
5. Divide the array into 3 sequences: elements < x, x, elements > x
6. Recursively quick sort both sets (Ai < x and Ai > x)
7. Stop

PROGRAM
#include <stdio.h>
#include <conio.h>
void qsort(int arr[20], int fst, int last);
main()
{
int arr[30], i, size;
printf("Enter total no. of the elements : ");
scanf("%d", &size);
printf("Enter total %d elements : \n", size);
for(i=0; i<size; i++)
scanf("%d", &arr[i]);
qsort(arr,0,size-1);
printf("\n Quick sorted elements \n");
for(i=0; i<size; i++)
printf("%d\t", arr[i]);
getch();
}
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)
4213 - KCET DEPARTMENT OF ECE - III - SEMESTER Page 89
CS3362 -C PROGRAMMING & DATASTRUCTURE LAB MANUAL
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
OBS
-2 -1 0 1 2 3 4 7
REC

VIVA

TOTAL

RESULT
Thus an array was sorted using quick sort’s divide and conquers method.

4213 - KCET DEPARTMENT OF ECE - III - SEMESTER Page 90


CS3362 -C PROGRAMMING & DATASTRUCTURE LAB MANUAL
EX.NO: 13 C) /* MERGE SORT */ DATE:

AIM

To sort an array of N numbers using Merge sort.

ALGORITHM

1. Start

2. Read number of array elements n

3. Read array elements Ai

4. Divide the array into sub-arrays with a set of elements

5. Recursively sort the sub-arrays

6. Merge the sorted sub-arrays onto a single sorted array.

7. Stop

PROGRAM :

#include <stdio.h>

#include <conio.h>

void merge(int [],int ,int ,int );

void part(int [],int ,int );

int size;

main()

int i, arr[30];

printf("Enter total no. of elements : ");

scanf("%d", &size);

printf("Enter array elements : ");

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

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

part(arr, 0, size-1);

printf("\n Merge sorted list : ");

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


4213 - KCET DEPARTMENT OF ECE - III - SEMESTER Page 91
CS3362 -C PROGRAMMING & DATASTRUCTURE LAB MANUAL
printf("%d ",arr[i]);

getch();

void part(int arr[], int min, int max) {

int i, mid;

if(min < max) {

mid = (min + max) / 2;

part(arr, min, mid);

part(arr, mid+1, max);

merge(arr, min, mid, max); }

if (max -min == (size/2) -1)

printf("\n Half sorted list : ");

for(i=min; i<=max; i++)

printf("%d ", arr[i]); }}

void merge(int arr[],int min,int mid,int max) {int tmp[30], i, j, k, m;

j = min;

m = mid + 1;

for(i=min; j<=mid && m<=max; i++) {

if(arr[j] <= arr[m]) {

tmp[i] = arr[j]; j++; }

else {

tmp[i] = arr[m]; m++; }}

if(j > mid) {

for(k=m; k<=max; k++) {

tmp[i] = arr[k]; i++; }}

4213 - KCET DEPARTMENT OF ECE - III - SEMESTER Page 92


CS3362 -C PROGRAMMING & DATASTRUCTURE LAB MANUAL
else

for(k=j; k<=mid; k++)

tmp[i] = arr[k]; i++;

for(k=min; k<=max; k++)

arr[k] = tmp[k];

OUTPUT :

Enter total no. of elements : 8

Enter array elements : 24 13 26 1 2 27 38 15

Half sorted list : 1 13 24 26

Half sorted list : 2 15 27 38


OBS
Merge sorted list : 1 2 13 15 24 26 27 3
REC

VIVA

TOTAL

RESULT :

Thus array elements were sorted using merge sort's divide and conquer method.

4213 - KCET DEPARTMENT OF ECE - III - SEMESTER Page 93


CS3362 -C PROGRAMMING & DATASTRUCTURE LAB MANUAL
EX.NO: 14 /* IMPLEMENTATION OF HASHING*/ DATE:

AIM :

To implement hash table using a C program.

ALGORITHM :

1. Create a structure, data (hash table item) with key and value as data.

2. Now create an array of structure, data of some certain size (10, in this case). But, the

size of array must be immediately updated to a prime number just greater than initial

array capacity (i.e 10, in this case).

3. A menu is displayed on the screen.

4. User must choose one option from four choices given in the menu

5. Perform all the operations

6. Stop

PROGRAM :

/* Open hashing */

#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("\nCollision handling by linear probing\n\n");

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

a[i] = -1;

do
4213 - KCET DEPARTMENT OF ECE - III - SEMESTER Page 94
CS3362 -C PROGRAMMING & DATASTRUCTURE LAB MANUAL
{

printf("\n Enter number:");

scanf("%d", &num);

key = create(num);

linearprobing(a, key, num);

printf("\nwish to continue?(y/n):");

ans = getch();

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) {
4213 - KCET DEPARTMENT OF ECE - III - SEMESTER Page 95
CS3362 -C PROGRAMMING & DATASTRUCTURE LAB MANUAL
printf("hash table is full");

display(a);

getch();

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%d",i,a[i]);

4213 - KCET DEPARTMENT OF ECE - III - SEMESTER Page 96


CS3362 -C PROGRAMMING & DATASTRUCTURE LAB MANUAL
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:93

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

11

2 62
4213 - KCET DEPARTMENT OF ECE - III - SEMESTER Page 97
CS3362 -C PROGRAMMING & DATASTRUCTURE LAB MANUAL
3 93

4 84

5 15

6 26

7 76

8 98

9 199

RESULT :

OBS

REC

VIVA

TOTAL

4213 - KCET DEPARTMENT OF ECE - III - SEMESTER Page 98


CS3362 -C PROGRAMMING & DATASTRUCTURE LAB MANUAL

Extra Programs

1. Program to find roots of Quadratic equation.

Aim:

Algorithm:

1. Start

2. Input the coefficient variable, x, y and z.

3. D <- sqrt (y * y - 4 * x * z).

4. R1 <- (-y + D) / ( 2 * x).

5. R2 <- (-y - D) / (2 * x).

6. Print the roots R1 and R2.

7. Stop.

Program:

#include<stdio.h>
#include<math.h> // it is used for math calculation
#include<conio.h>
void main()
{
float x, y, z, det, root1, root2, real, img;
printf("\n Enter the value of coefficient x, y and z: \n ");
scanf("%f %f %f", &x, &y, &z);
// define the quadratic formula of the nature of the root
det = y * y - 4 * x * z;
// defines the conditions for real and different roots of the quadratic equation
if (det > 0)
{
root1 = (-y + sqrt(det)) / (2 * x);
root2 = (-y + sqrt(det)) / (2 * x);

4213 - KCET DEPARTMENT OF ECE - III - SEMESTER Page 99


CS3362 -C PROGRAMMING & DATASTRUCTURE LAB MANUAL

printf("\n Value of root1 = %.2f and value of root2 = %.2f", root1, root2);
}
// elseif condition defines both roots ( real and equal root) are equal in the quadratic equation
else if (det == 0)
{
root1 = root2 = -y / (2 * x); // both roots are equal;
printf("\n Value of root1 = %.2f and Value of root2 = %.2f", root1, root2);
}
// if det < 0, means both roots are real and imaginary in the quadratic equation.
else {
real = -y / (2 * x);
img = sqrt(-det) / (2 * x);
printf("\n value of root1 = %.2f + %.2fi and value of root2 = %.2f - %.2fi ", real, img, real, img);
}
getch();
}
Output:

RESULT :

OBS

REC

VIVA

TOTAL

4213 - KCET DEPARTMENT OF ECE - III - SEMESTER Page 100


CS3362 -C PROGRAMMING & DATASTRUCTURE LAB MANUAL

2. Program to find the sum of the digits of a given number

Aim:

Algorithm:

o Step 1: Get number by user

o Step 2: Get the modulus/remainder of the number

o Step 3: sum the remainder of the number

o Step 4: Divide the number by 10

o Step 5: Repeat the step 2 while number is greater than 0.

Program:

#include<stdio.h>
void main() {
int num,sum; /* given numbers */
clrscr();
printf("Enter integer numbers: ");
scanf ("%d", &num);
sum = 0;
do {
sum += num % 10; /* add LS digit to digit sum */
num /= 10; /* remove LS digit from num */
} while (num > 0);
printf ("The Sum is = %d \n",sum);
getch();
}

Output:

4213 - KCET DEPARTMENT OF ECE - III - SEMESTER Page 101


CS3362 -C PROGRAMMING & DATASTRUCTURE LAB MANUAL

OBS

REC

VIVA

TOTAL

RESULT :

4213 - KCET DEPARTMENT OF ECE - III - SEMESTER Page 102


CS3362 -C PROGRAMMING & DATASTRUCTURE LAB MANUAL
3. Program to find the min and max number in an array

Aim:

Algorithm:

1. Read the entered array size and store that value into the variable n.
2) Read the entered elements using scanf and store the entered array elements into the array using
for loop for(i=0;i<n;i++).
3) Initialise min, max values with the 1st element of the array.
4) Compare min, max values with a[i],
If min value is greater than a[i] then initialise min=a[i] and if max value is less than a[i] then initialise
max=a[i]. Repeat this step for each element of the string using for loop which is having the structure
for(i=1;i<n;i++).

Program:

#include <stdio.h>

void main()
{
int arr1[100];
int i, mx, mn, n;
clrscr();
printf("\n\nFind maximum and minimum element in an array :\n");
printf("--------------------------------------------------\n");

printf("Input the number of elements to be stored in the array :");


scanf("%d",&n);

printf("Input %d elements in the array :\n",n);


for(i=0;i<n;i++)
{
printf("element - %d : ",i);
scanf("%d",&arr1[i]);
}

mx = arr1[0];
mn = arr1[0];

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


{
if(arr1[i]>mx)
{
mx = arr1[i];
}

if(arr1[i]<mn)

4213 - KCET DEPARTMENT OF ECE - III - SEMESTER Page 103


CS3362 -C PROGRAMMING & DATASTRUCTURE LAB MANUAL
{
mn = arr1[i];
}
}
printf("Maximum element is : %d\n", mx);
printf("Minimum element is : %d\n\n", mn);
getch();
}

Output:

Find maximum and minimum element in an array :


--------------------------------------------------
Input the number of elements to be stored in the array :3
Input 3 elements in the array :
element - 0 : 45
element - 1 : 25
element - 2 : 21
Maximum element is : 45
Minimum element is : 21

RESULT :

OBS

REC

VIVA

TOTAL

4213 - KCET DEPARTMENT OF ECE - III - SEMESTER Page 104


CS3362 -C PROGRAMMING & DATASTRUCTURE LAB MANUAL
4.Program to find whether given number is a prime number.

Aim:

Algorithm:

STEP 1: Take num as input.


STEP 2: Initialize a variable temp to 0.
STEP 3: Iterate a “for” loop from 2 to num/2.
STEP 4: If num is divisible by loop iterator, then increment temp.
STEP 5: If the temp is equal to 0,
Return “Num IS PRIME”.
Else,
Return “Num IS NOT PRIME”.

#include<stdio.h>
int main(){
int n,i,m=0,flag=0;
clrscr();
printf("Enter the number to check prime:");
scanf("%d",&n);
m=n/2;
for(i=2;i<=m;i++)
{
if(n%i==0)
{
printf("Number is not prime");
flag=1;
break;
}
}
if(flag==0)
printf("Number is prime");
getch();
return 0;
}

4213 - KCET DEPARTMENT OF ECE - III - SEMESTER Page 105


CS3362 -C PROGRAMMING & DATASTRUCTURE LAB MANUAL

Output:

Enter the number to check prime:56


Number is not prime

Enter the number to check prime:23


Number is prime

RESULT :

OBS

REC

VIVA

TOTAL

4213 - KCET DEPARTMENT OF ECE - III - SEMESTER Page 106


CS3362 -C PROGRAMMING & DATASTRUCTURE LAB MANUAL

5.Program to print all even numbers upto 20


Aim:

Algorithm:
Step 1: Start Program.
Step 2: Read the number from user and store it in a.
Step 3: Iterate for or while loop according to a user input a number.
Step 4: Inside loop, use if with n % 2 == 0 condition to print even number.
Step 5: Stop Program.

Program:
#include <stdio.h>
#include<conio.h>
int main() {
int i;
clrscr();
printf("Even numbers between 1 to 50 (inclusive):\n");
for (i = 1; i <= 20; i++)
{
if(i%2 == 0)
{
printf("%d ", i);
}
}
Getch();
return 0;
}
Output:
Even numbers between 1 to 20 (inclusive):
2 4 6 8 10 12 14 16 18 20

OBS

REC
RESULT :
VIVA

TOTAL

6. program to print all leap year from 2000 to 2022

4213 - KCET DEPARTMENT OF ECE - III - SEMESTER Page 107


CS3362 -C PROGRAMMING & DATASTRUCTURE LAB MANUAL

Aim:

Algorithm:
START
Step 1 → Take integer variable year
Step 2 → Assign value to the variable
Step 3 → Check if year is divisible by 4 but not 100, DISPLAY "leap year"
Step 4 → Check if year is divisible by 400, DISPLAY "leap year"
Step 5 → Otherwise, DISPLAY "not leap year"
STOP

Program:
#include<stdio.h>
#include<conio.h>
void main() {
int startYear, endyear, i;
printf ("Enter a year to start searching the leap years: ");
scanf ("%d", &startYear);
printf ("Enter a year to end the search of leap years: ");
scanf ("%d", &endyear);
printf ("Leap Years between %d to %d are: \n", &startYear, &endyear);
for (i= startYear; i<= endyear; i++)
{
if((i%4==0) && (i%100!=0) || (i%400==0))
{
printf("%d \n", i);
}
}
getch();
}

Output:

4213 - KCET DEPARTMENT OF ECE - III - SEMESTER Page 108


CS3362 -C PROGRAMMING & DATASTRUCTURE LAB MANUAL

OBS

REC

VIVA

TOTAL

RESULT :

7. Program to print the transpose of a matrix.

4213 - KCET DEPARTMENT OF ECE - III - SEMESTER Page 109


CS3362 -C PROGRAMMING & DATASTRUCTURE LAB MANUAL
Aim:

Algorithm:

1. Declare and initialize a two-dimensional array a.


2. Calculate the number of rows and columns present in the matrix and store it variables rows
and cols respectively.
3. Declare another array t with reversed dimensions i.e t[cols][rows]. Array t will be used to
store the elements of the transposed matrix.
4. Loop through the array a and convert its rows into columns of matrix t using
t[ i ][ j ] = a[ j ][ i ];
5. Finally, display the elements of matrix t.

Program:

#include <stdio.h>

void main()

//int rows, cols;

int i,j,t[3][3];

int a[][3] = {{1, 2, 3},{4, 5, 6},{7, 8, 9}};

//Calculates number of rows and columns present in given matrix

int rows = (sizeof(a)/sizeof(a[0]));

int cols = (sizeof(a)/sizeof(a[0][0]))/rows;

//Declare array t with reverse dimensions

//int t[cols][rows];

//Calculates transpose of given matrix

clrscr();

for(i = 0; i < cols; i++){

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

//Converts the row of original matrix into column of transposed matrix

4213 - KCET DEPARTMENT OF ECE - III - SEMESTER Page 110


CS3362 -C PROGRAMMING & DATASTRUCTURE LAB MANUAL
t[i][j] = a[j][i];

printf("Transpose of given matrix: \n");

for(i = 0; i < cols; i++){

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

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

printf("\n");

getch();

Output:

Transpose of given matrix:

147

258

369
OBS

REC
RESULT :
VIVA
Thus hashing has been performed successfully.
TOTAL

4213 - KCET DEPARTMENT OF ECE - III - SEMESTER Page 111

You might also like