DSUC File - 2 - 2
DSUC File - 2 - 2
void main()
{
int ch;
float a,b,res;
clrscr();
printf( Enter two numbers: );
scanf( %f%f ,&a,&b);
printf( nMenun1.Additionn2.Subtractionn3.Multiplicationn4.Division );
printf( nEnter your choice: );
scanf( %d ,&ch);
switch(ch)
{
case 1: res=a+b;
break;
case 2: res=a-b;
break;
case 3: res=a*b;
break;
case 4: res=a/b;
break;
default: printf( Wrong choice!!nPress any key… );
getch();
exit(0);
}printf( nResult=%f ,res);
getch();
}
PROGRAM :2
WAP for swapping of two numbers using Functions with call by
value and call by reference strategies.
#include <stdio.h>
#include<conio.h>
void swap(int *n1, int *n2);
int main()
{
int num1 = 5, num2 = 10;
clrscr();
interchange(num1,num2);
// swaping using call by value
printf("\n Call by value result");
printf("\n Number 1 : %d",num1);
printf("\n Number 2 : %d",num2);
swap( &num1, &num2); // address of num1 and num2 is passed to
the swap function
printf("\n Call by reference result");
printf("\n Number1 = %d", num1);
printf("\n Number2 = %d", num2);
getch();
return 0;
}
void swap(int * n1, int * n2)
{
int temp; // pointer n1 and n2 points to the address of num1 and
num2 respectively
temp = *n1;
*n1 = *n2;
*n2 = temp;
}
#include<stdio.h>
#include<conio.h>
int Fibonacci(int);
main()
{
int n, i = 0, c;
scanf("%d",&n);
printf("Fibonacci series\n");
int Fibonacci(int n)
{
if ( n == 0 )
return 0;
else if ( n == 1 )
return 1;
else
return ( Fibonacci(n-1) + Fibonacci(n-2) );
}
PROGRAM : 4
Factorial of a number using Recursion
#include<stdio.h>
#include<conio.h>
long factorial(int);
int main()
{
int n;
long f;
if (n < 0)
printf("Negative integers are not allowed.\n");
else
{
f = factorial(n);
printf("%d! = %ld\n", n, f);
}
getch();
return 0;
}
long factorial(int n)
{
if (n == 0)
return 1;
else
return(n * factorial(n-1));
}
PROGRAM :5
Program to read and display the information of Employee
using Structure.
#include <conio.h>
int main()
{
int arr[10]; //declare integer array
int *pa; //declare an integer pointer
int i;
clrscr();
pa=&arr[0]; //assign base address of array
printf("Enter array elements:\n");
for(i=0;i < 5; i++){
printf("Enter element %02d: ",i+1);
scanf("%d",pa+i); //reading through pointer
}
printf("\nEntered array elements are:");
printf("\nAddress\t\tValue\n");
for(i=0;i<5;i++){
printf("%d\t\t%d \n",(pa+i),*(pa+i));
} getch();
Return 0;
}
PROGRAM : 7
WAP to insert an element at specified position in a
linear array
ALGORITHM:
1. Start
2. Set J = N
3. Set N = N+1
4. Repeat steps 5 and 6 while J >= K
5. Set LA[J+1] = LA[J]
6. Set J = J-1
7. Set LA[K] = ITEM
8. Stop
PROGRAM:
#include <stdio.h>
#include<conio.h>
int main()
{
int array[100], position, c, n, value;
return 0;
}
PROGRAM: 8
WAP to delete an element at specified position in a
linear array.
ALGORITHM:
1. Start
2. Set J = K
3. Repeat steps 4 and 5 while J < N
4. Set LA[J] = LA[J + 1]
5. Set J = J+1
6. Set N = N-1
7. Stop
PROGRAM:
#include <stdio.h>
#include<conio.h>
int main()
{ int array[100], position, c, n;
PROGRAM :9
WAP to find largest of three numbers using
Structure.
#include <stdio.h>
#include<conio.h>
struct Num
{
int num1, num2, num3;
};
void main()
{
struct Num NL;
clrscr();
printf("Enter the values of num1, num2 and num3\n");
scanf("%d %d %d", &NL.num1, &NL.num2, &NL.num3);
printf("num1 = %d\tnum2 = %d\tnum3 = %d\n", NL.num1, NL.num2,
NL.num3);
if (NL.num1 > NL.num2)
{
if (NL.num1 > NL.num3)
{
printf("num1 is the greatest among three \n");
}
else
{
printf("num3 is the greatest among three \n");
}
}
ALGORITHM:
Linear Search ( Array A, Value x)
Step 1: Set i to 1
Step 2: if i > n then go to step 7
Step 3: if A[i] = x then go to step 6
Step 4: Set i to i + 1
Step 5: Go to Step 2
Step 6: Print Element x Found at index i and go to step
8
Step 7: Print element not found
Step 8: Exit
PROGRAM:
#include <stdio.h>
#include<conio.h>
int main()
{
int array[100], search, c, n;
ALGORITHM:
Binary_search(A, target):
1. lo = 1, hi = size(A)
2. while lo <= hi repeat 3 to 6
3. mid = lo + (hi-lo)/2
4. if A[mid] == target:
return mid
5. else if A[mid] < target:
lo = mid+1
6. else:
hi = mid-1
7. End
PROGRAM:
#include <stdio.h>
#include<conio.h>
int main()
{ int c, first, last, middle, n, search, array[100];
printf("Enter number of elements\n");
scanf("%d",&n);
printf("Enter %d integers\n", n);
for (c = 0; c < n; c++)
scanf("%d",&array[c]);
printf("Enter value to find\n");
scanf("%d", &search);
first = 0;
last = n - 1;
middle = (first+last)/2;
case 4:
printf("Enter a string: ");
scanf("%s", string1);
printf("String1 = %s\n");
printf("After copying string1 to string 2\n");
copy_string(string2, string1);
printf("String2 = %s", string2);
break;
case 5:
exit(0);
}
}
getch();
return 0;
}
PROGRAM: 13
WAP to sort a linear array in ascending order using
Bubble Sort.
ALGORITHM:
BubbleSort(list,n)
1. set i=0
2. repeat step 3 to 5 until i<n
3. set j=0
4. repeat step 5 until ( j <n-i-1 )
5. IF list[j]>list[j+1] then
Set temp = list[j]
Set list[j] = list[j+1]
Set list[j+1] = temp
6. Exit
PROGRAM:
#include <stdio.h>
int main()
{
int array[100], n, c, d, swap;
ALGORITHM:
Insertion(A,n)
1. Set A[0]
2. Repeat step 3 to 5 for k<n
3. Set temp = A[k] and ptr = k -1
4. Repeat while temp < A[ptr]
Set A[ptr +1] = A[ptr]
Set ptr =ptr -1
5. Set A[ptr +1] = temp
6. Exit.
PROGRAM:
#include <stdio.h>
int main()
{
int n, array[1000], c, d, t;
printf("Enter number of elements\n");
scanf("%d", &n);
printf("Enter %d integers\n", n);
return 0;
}
PROGRAM : 15
WAP to sort a linear array in ascending order using
Selection Sort.
ALGORITHM:
SelectionSort(A[],n)
1. Repeat step 2,3 until k < n
2. Call MIN(A,k,n,loc)
3. Set temp = A[k]
A[k]= A[loc]
A[loc] = temp
4. Exit
MIN(A,k,n,loc)
1. Set MIN=A[k] and loc =k
2. Repeat for j =k+1 until j<n
3. If MIN >A[j]
Set MIN = A[j]
Loc = j
4. Return
PROGRAM:
#include <stdio.h>
int main()
{
int array[100], n, c, d, position, swap;
ALGORITHM:
MergeSort(arr[], l, r)
If r > l
1. Find the middle point to divide the array into two
halves:
middle m = (l+r)/2
2. Call mergeSort for first half:
Call mergeSort(arr, l, m)
3. Call mergeSort for second half:
Call mergeSort(arr, m+1, r)
4. Merge the two halves sorted in step 2 and 3:
Call merge(arr, l, m, r)
PROGRAM:
#include <stdio.h>
#include <conio.h>
#define max 10
int a[11] = { 10, 14, 19, 26, 27, 31, 33, 35, 42, 44, 0 };
int b[10];
void merging(int low, int mid, int high) {
int l1, l2, i;
for(l1 = low, l2 = mid + 1, i = low; l1 <= mid && l2 <= high; i++) {
if(a[l1] <= a[l2])
b[i] = a[l1++];
else
b[i] = a[l2++];
}
while(l1 <= mid)
b[i++] = a[l1++];
while(l2 <= high)
b[i++] = a[l2++];
for(i = low; i <= high; i++)
a[i] = b[i];
}
void sort(int low, int high) {
int mid;
if(low < high) {
mid = (low + high) / 2;
sort(low, mid);
sort(mid+1, high);
ALGORITHM:
PUSH()
Push operation involves a series of steps −
Step 1 − Checks if the stack is full.
Step 2 − If the stack is full, produces an error and exit.
Step 3 − If the stack is not full, increments top to point next empty
space.
Step 4 − Adds data element to the stack location, where top is pointing.
Step 5 − Returns success.
POP():
A Pop operation may involve the following steps −
Step 1 − Checks if the stack is empty.
Step 2 − If the stack is empty, produces an error and exit.
Step 3 − If the stack is not empty, accesses the data element at
which top is pointing.
Step 4 − Decreases the value of top by 1.
Step 5 − Returns success.
PROGRAM:
#include<conio.h>
#include<stdio.h>
#define max 50
void push();
void pop();
void display();
int menu();
int stack[max], top=0;
void main()
{
int ch;
clrscr();
do{
ch=menu();
switch(ch)
{
case 1: push();
break;
case 2: pop();
break;
case 3: display();
break;
case 4: exit();
default: printf("\nEnter a valid choice!!");
}
}while(1);
}
int menu()
{
int ch;
printf("\nStack");
printf("\n1.Push\n2.Pop\n3.Display\n4.Exit");
printf("\nEnter your Choice:");
scanf("%d",&ch);
return ch;
}
void push()
{
if(top==max)
printf("\nOverflow");
else
{
int element;
printf("\nEnter Element:");
scanf("%d",&element);
printf("\nElement(%d) has been pushed at %d", element, top);
stack[top++]=element;
}
}
void pop()
{
if(top==-1)
printf("\nUnderflow");
else
{
top--;
printf("\nELement has been popped out!");
}
}
void display()
{
if(top==0)
printf("\nStack is Empty!!");
else
{
int i;
for(i=0;i<max;i++)
printf("%d",stack[i]);
}
}
PROGRAM : 18
WAP to Convert Infix Expression to postfix form using
Stack.
ALGORITM:
void main()
{
char str[]="KIIT COLLEGE OF ENGINEERING";
int len = strlen(str);
int i;
clrscr();
for(i=0;i<len;i++)
push(str[i]);
for(i=0;i<len;i++)
pop();
getch();
}
PROGRAM : 20
WAP to perform different operations with Queue such as
Insert, Delete, Display of elements using array.
ALGORITHM:
Insert():
Queues maintain two data pointers, front and rear. Therefore, its
operations are comparatively difficult to implement than that of
stacks.
The following steps should be taken to enqueue (insert) data into a
queue −
Step 1 − Check if the queue is full.
Step 2 − If the queue is full, produce overflow error and exit.
Step 3 − If the queue is not full, increment rear pointer to point the
next empty space.
Step 4 − Add data element to the queue location, where the rear is
pointing.
Step 5 − return success.
Delete():
Accessing data from the queue is a process of two tasks − access the
data where front is pointing and remove the data after access. The
following steps are taken to perform dequeue operation −
Step 1 − Check if the queue is empty.
Step 2 − If the queue is empty, produce underflow error and exit.
Step 3 − If the queue is not empty, access the data where front is
pointing.
Step 4 − Increment front pointer to point to the next available data
element.
Step 5 − Return success.
PROGRAM:
#include <stdio.h>
#include<conio.h>
#define MAX 50
int queue_array[MAX];
int rear = - 1;
int front = - 1;
main()
{int choice;
clrscr();
while (1)
{ printf("1.Insert element to queue \n");
printf("2.Delete element from queue \n");
printf("3.Display all elements of queue \n");
printf("4.Quit \n");
printf("Enter your choice : ");
scanf("%d", &choice);
switch (choice)
{ case 1:
insert();
break;
case 2:
delete();
break;
case 3:
display();
break;
case 4:
exit(1);
default:
printf("Wrong choice \n");
} /*End of switch*/
} /*End of while*/
getch(); } /*End of main()*/
insert()
{ int add_item;
if (rear == MAX - 1)
printf("Queue Overflow \n");
else
{ if (front == - 1) /*If queue is initially empty */
front = 0;
printf("Inset the element in queue : ");
scanf("%d", &add_item);
rear = rear + 1;
queue_array[rear] = add_item;
} } /*End of insert()*/
delete()
{ if (front == - 1 || front > rear)
{ printf("Queue Underflow \n");
return ;}
else
{ printf("Element deleted from queue is : %d\n",
queue_array[front]);
front = front + 1;
} } /*End of delete() */
display()
{ int i;
if (front == - 1)
printf("Queue is empty \n");
else
{printf("Queue is : \n");
for (i = front; i <= rear; i++)
printf("%d ", queue_array[i]);
printf("\n");
} }
PROGRAM :21
Program to perform different operations with Queue such
as Insert, Delete, Display of elements using Circular
Queue
#include <stdio.h>
#define size 5
void insertq(int[], int);
void deleteq(int[]);
void display(int[]);
int front = - 1;
int rear = - 1;
int main()
{ int n, ch;
int queue[size];
do
{ printf("\n\n Circular Queue:\n1. Insert \n2. Delete\n3.
Display\n0. Exit");
printf("\nEnter Choice 0-3? : ");
scanf("%d", &ch);
switch (ch)
{ case 1:
printf("\nEnter number: ");
scanf("%d", &n);
insertq(queue, n);
break;
case 2:
deleteq(queue);
break;
case 3:
display(queue);
break;
}
}while (ch != 0);
}
void insertq(int queue[], int item)
{ if ((front == 0 && rear == size - 1) || (front == rear + 1))
{ printf("queue is full");
return; }
else if (rear == - 1)
{ rear++;
front++; }
else if (rear == size - 1 && front > 0)
{ rear = 0; }
else
{ rear++; }
queue[rear] = item;
}
void display(int queue[])
{ int i;
printf("\n");
if (front > rear)
{ for (i = front; i < size; i++)
{ printf("%d ", queue[i]); }
for (i = 0; i <= rear; i++)
printf("%d ", queue[i]); }
else
{ for (i = front; i <= rear; i++)
printf("%d ", queue[i]);
} }
void deleteq(int queue[])
{ if (front == - 1)
{
printf("Queue is empty ");
}
else if (front == rear)
{
printf("\n %d deleted", queue[front]);
front = - 1;
rear = - 1;
}
else
{
printf("\n %d deleted", queue[front]);
front++;
}
}
PROGRAM :22
Program to implement Linear Link List with operations:
Insertion (Beginning, Between, End) Deletion (Beginning,
Between, End),Traverse
#include <stdio.h>
#include <stdlib.h>
struct node {
int data;
struct node *next;
};
int main () {
int input, data;
for (;;) {
printf("1. Insert an element at beginning of linked list.\n");
printf("2. Insert an element at end of linked list.\n");
printf("3. Traverse linked list.\n");
printf("4. Delete element from beginning.\n");
printf("5. Delete element from end.\n");
printf("6. Exit\n");
scanf("%d", &input);
if (input == 1) {
printf("Enter value of element\n");
scanf("%d", &data);
insert_at_begin(data);
}
else if (input == 2) {
printf("Enter value of element\n");
scanf("%d", &data);
insert_at_end(data);
}
else if (input == 3)
traverse();
else if (input == 4)
delete_from_begin();
else if (input == 5)
delete_from_end();
else if (input == 6)
break;
else
printf("Please enter valid input.\n");
}
return 0;
}
void insert_at_begin(int x) {
struct node *t;
if (start == NULL) {
start = t;
start->data = x;
start->next = NULL;
return;
}
t->data = x;
t->next = start;
start = t;
}
void insert_at_end(int x) {
struct node *t, *temp;
if (start == NULL) {
start = t;
start->data = x;
start->next = NULL;
return;
}
temp = start;
temp->next = t;
t->data = x;
t->next = NULL;
}
void traverse() {
struct node *t;
t = start;
if (t == NULL) {
printf("Linked list is empty.\n");
return;
}
void delete_from_begin() {
struct node *t;
int n;
if (start == NULL) {
printf("Linked list is already empty.\n");
return;
}
n = start->data;
t = start->next;
free(start);
start = t;
count--;
void delete_from_end() {
struct node *t, *u;
int n;
if (start == NULL) {
printf("Linked list is already empty.\n");
return;
}
count--;
if (start->next == NULL) {
n = start->data;
free(start);
start = NULL;
printf("%d deleted from end successfully.\n", n);
return;
}
t = start;
n = t->data;
u->next = NULL;
free(t);