0% found this document useful (0 votes)
2 views

All practical Programs

The document contains multiple C programs demonstrating basic algorithms such as Linear Search, Binary Search, Bubble Sort, Selection Sort, and Stack operations. Each program includes user input for data, processing logic, and output results. The programs utilize standard input/output functions and basic control structures to perform their respective tasks.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

All practical Programs

The document contains multiple C programs demonstrating basic algorithms such as Linear Search, Binary Search, Bubble Sort, Selection Sort, and Stack operations. Each program includes user input for data, processing logic, and output results. The programs utilize standard input/output functions and basic control structures to perform their respective tasks.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

Write a program for Linear Search

#include<stdio.h>
#include<conio.h>
int main()
{
int a[20],i,x,n;
clrscr();
printf("How many elements?");
scanf("%d",&n);

printf("Enter array elements:n");


for(i=0;i<n;++i)
scanf("%d",&a[i]);

printf("nEnter element to search:");


scanf("%d",&x);

for(i=0;i<n;++i)
if(a[i]==x)
break;

if(i<n)
printf("Element found at index %d",i);
else
printf("Element not found");
getch();
return 0;
}

/* output
*************************************************************************************
Write a program for Binary Search

/Program Name: BinarySearch.c


#include<stdio.h>
#include<conio.h>
int main()
{
int c, first, last, middle, n, search, array[100];
clrscr();
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;
while( first <= last )
{
if ( array[middle] < search )
first = middle + 1;
else if ( array[middle] == search )
{
printf("%d found at location %d.\n", search, middle+1);
break;
}
else
last = middle - 1;
middle = (first + last)/2;
}
if ( first > last )
printf("Not found! %d is not present in the list.\n", search);
getch();
return 0;
}
/* Output
Enter number of elements
6
Enter 6 integers
10
30
20
15
56
100
Enter value to find
33
Not found! 33 is not present in the list.
*/
*************************************************************************************
Write a program for Bubble Sort
#include<stdio.h>
#include<conio.h>
int main()
{
int array[100], n, i, j, swap;
clrscr();
printf("Enter number of elementsn");
scanf("%d", &n);
printf("Enter %d Numbers:n", n);
for(i = 0; i < n; i++)
scanf("%d", &array[i]);
for(i = 0 ; i < n - 1; i++)
{
for(j = 0 ; j < n-i-1; j++)
{
if(array[j] > array[j+1])
{
swap=array[j];
array[j]=array[j+1];
array[j+1]=swap;
}
}
}
printf("Sorted Array:n");
for(i = 0; i < n; i++)
printf("%dn", array[i]);
getch();
return 0;
}

*************************************************************************************
Write a program for Selection sort
#include <stdio.h>
#include<conio.h>

int main() {
int arr[10]={6,12,0,18,11,99,55,45,34,2};
int n=10;
int i, j, position, swap;
clrscr();
for (i = 0; i < (n - 1); i++) {
position = i;
for (j = i + 1; j < n; j++) {
if (arr[position] > arr[j])
position = j;
}
if (position != i) {
swap = arr[i];
arr[i] = arr[position];
arr[position] = swap;
}
}
for (i = 0; i < n; i++)
printf("%d\t", arr[i]);
getch();
return 0;
}
/* Output
0 2 6 11 12 18 34 45 55 99

*************************************************************************************
Write a program for implement stack operation
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#define Size 4
int Top=-1, inp_array[Size];
void Push();
void Pop();
void show();

int main()
{
int choice;

while(1)
{
printf("\nOperations performed by Stack");
printf("\n1.Push the element\n2.Pop the element\n3.Show\n4.End");
printf("\n\nEnter the choice:");
scanf("%d",&choice);

switch(choice)
{
case 1: Push();
break;
case 2: Pop();
break;
case 3: show();
break;
case 4: exit(0);

default: printf("\nInvalid choice!!");


}
}
}

void Push()
{
int x;

if(Top==Size-1)
{
printf("\nOverflow!!");
}
else
{
printf("\nEnter element to be inserted to the stack:");
scanf("%d",&x);
Top=Top+1;
inp_array[Top]=x;
}
}

void Pop()
{
if(Top==-1)
{
printf("\nUnderflow!!");
}
else
{
printf("\nPopped element: %d",inp_array[Top]);
Top=Top-1;
}
}

void show()
{

if(Top==-1)
{
printf("\nUnderflow!!");
}
else
{
printf("\nElements present in the stack: \n");
for(int i=Top;i>=0;--i)
printf("%d\n",inp_array[i]);
}
}

/* Output
Operations performed by Stack
1.Push the element
2.Pop the element
3.Show
4.End

Enter the choice:1

Enter element to be inserted to the stack:10

Operations performed by Stack


1.Push the element
2.Pop the element
3.Show
4.End

Enter the choice:3

Elements present in the stack:


10

Operations performed by Stack


1.Push the element
2.Pop the element
3.Show
4.End

Enter the choice:2


Popped element: 10

Operations performed by Stack


1.Push the element
2.Pop the element
3.Show
4.End

Enter the choice:3


Underflow!!
*/

You might also like