C & DS Lab Final Ece
C & DS Lab Final Ece
DEPARTMENT
OF
ELECTRONICS AND COMMUNICATION ENGINEERING
CS3362
C PROGRAMMING & DATASTRUCTURE
LAB MANUAL
REGISTER NO:
NAME :
CS3362 -C PROGRAMMING & DATASTRUCTURE LAB MANUAL
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:
EXPERIMENTS
PAGE
S.NO DATE PARTICULARS MARK SIGN
NO
COMPLETED/NOT COMPLETED
STAFF INCHARGE
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:
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.
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.
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:
RESULT:
Thus the above program to swap the given numbers has been executed successfully and the
output was verified.
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.
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.
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;
}
}
Enter Operator: *
OBS
REC
VIVA
TOTAL
RESULT:
Thus the above program to perform arithmetic operations been executed successfully and the
output was verified.
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.
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:
VIVA
TOTAL
RESULT:
Thus the above program to generate the Fibonacci series has been executed successfully and
the output was verified.
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.
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.
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:
VIVA
TOTAL
RESULT:
Thus the above program to print the largest number in an array has been executed
successfully and the output was verified.
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:
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.
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.
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);
printf("\nAfter swapping:");
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.
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.
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:
OBS
REC
VIVA
TOTAL
RESULT:
Thus the above program comparison of string using pointer has been executed successfully and the
output was verified
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”);
OUTPUT:
OBS
REC
VIVA
TOTAL
RESULT:
Thus the above program comparison of string using pointer has been executed successfully and the
output was verified
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;
}
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.
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;
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.
Aim:
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;
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.
Aim:
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;
if (fptr == NULL)
return;
scanf("%d", &id);
scanf("%s", name);
scanf("%f", &salary);
fclose(fptr);
OUTPUT:
Enter the id
sonoo
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.
AIM:
Booking
Availability checking
Cancellation
Prepare Chart
ALGORITHM
STEP 1: Start
STEP 6: if a->seat>70
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)
{
}
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 ;
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.
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();
}
}
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
TOTAL
RESULT :
Thus various operations were successfully executed on list using array implementation.
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--]);
}
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)
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
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.
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];
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);
}
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
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.
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;
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;
OUTPUT
OBS
REC
VIVA
TOTAL
RESULT
Thus the program to implement Singly Linked List has been executed and verified successfully.
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;
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);
}
}
OUTPUT
OBS
REC
VIVA
TOTAL
RESULT
Thus push and pop operations of a stack was demonstrated using linked list.
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;
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);
}
OUTPUT
OBS
REC
VIVA
TOTAL
RESULT
Thus insert and delete operations of a queue was demonstrated using linked list.
AIM
To write a ‘C’ Program to represent a polynomial as a linked list and to write functions for
polynomial addition.
ALGORITHM
PROGRAM
#include<stdio.h>
#include<conio.h>
#include<malloc.h>
struct link
{
int coeff;
int pow;
struct link *next;
};
do
{
printf("\n Enter Co-efficient : ");
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) :
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.
AIM
To write a C program to convert the infix expression to postfix expression using stack
concept.
ALGORITHM
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++)
OUTPUT
OBS
REC
VIVA
TOTAL
RESULT
Thus C program to convert the infix expression to postfix expression using stack
concept was verified successfully.
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.
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()
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
----------------------------
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.
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)
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.
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;
}
}
OUTPUT
REC
VIVA
TOTAL
RESULT
Thus program to implement binary search tree traversal and its inorder, preorder, and
postorder traversals.
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;
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.
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;
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
REC
VIVA
TOTAL
RESULT
Thus an element is located quickly using binary search method.
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;
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.
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.
AIM
ALGORITHM
1. Start
7. Stop
PROGRAM :
#include <stdio.h>
#include <conio.h>
int size;
main()
int i, arr[30];
scanf("%d", &size);
scanf("%d", &arr[i]);
part(arr, 0, size-1);
getch();
int i, mid;
j = min;
m = mid + 1;
else {
arr[k] = tmp[k];
OUTPUT :
VIVA
TOTAL
RESULT :
Thus array elements were sorted using merge sort's divide and conquer method.
AIM :
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
4. User must choose one option from four choices given in the menu
6. Stop
PROGRAM :
/* Open hashing */
#include <stdio.h>
#include <stdlib.h>
#define MAX 10
main()
int create(int);
void display(int[]);
a[i] = -1;
do
4213 - KCET DEPARTMENT OF ECE - III - SEMESTER Page 94
CS3362 -C PROGRAMMING & DATASTRUCTURE LAB MANUAL
{
scanf("%d", &num);
key = create(num);
printf("\nwish to continue?(y/n):");
ans = getch();
display(a); }
int key;
return key; }
flag = 0;
if(a[key] == -1)
a[key] = num;
else {
i=0;
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); }
if(a[i] ==
-1)
break; }
if(a[i] == -1)
break; }}}
int i;
printf("
printf("
\n %d
\t%d",i,a[i]);
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):
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
Extra Programs
Aim:
Algorithm:
1. Start
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);
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
Aim:
Algorithm:
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:
OBS
REC
VIVA
TOTAL
RESULT :
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");
mx = arr1[0];
mn = arr1[0];
if(arr1[i]<mn)
Output:
RESULT :
OBS
REC
VIVA
TOTAL
Aim:
Algorithm:
#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;
}
Output:
RESULT :
OBS
REC
VIVA
TOTAL
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
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:
OBS
REC
VIVA
TOTAL
RESULT :
Algorithm:
Program:
#include <stdio.h>
void main()
int i,j,t[3][3];
//int t[cols][rows];
clrscr();
printf("\n");
getch();
Output:
147
258
369
OBS
REC
RESULT :
VIVA
Thus hashing has been performed successfully.
TOTAL