1.
Write a program to implement linear search algorithm
#include<stdio.h>
int main(){
int arr[100],n,target,i,found=0;
printf("Enter the number of elements in the array : ");
scanf("%d",&n);
printf("Enter %d elements : \n",n);
for(i=0;i<n;i++) {
scanf("%d",&arr[i]);
printf("Enter the elements to search : ");
scanf("%d",&target);
for(i=0;i<n;i++) {
if(arr[i]==target){
printf("Element found at position %d\n",i+1);
found=1;
break;
if(found==0){
printf("Element not found in the array\n");
return 0;
OUTPUT:
Enter the number of elements in the array : 3
Enter 3 elements :
123
Enter the elements to search : 2
Element found at position 2
[Link] a program to implement binary search algorithm
#include<stdio.h>
int main(){
int arr[100],n,target,low,high,mid,found=0;
printf("Enter the number of elements in the array:");
scanf("%d",&n);
printf("Enter %d elements in sorted order.\n",n);
for(int i=0;i<n;i++){
scanf("%d",&arr[i]);
printf("Enter the element to search:");
scanf("%d",&target);
low=0;
high=n-1;
while(low<=high){
mid=(low+high)/2;
if(arr[mid]==target){
printf("Element found at position %d\n",mid+1);
found=1;
break;
else if(arr[mid]<target){
low=mid+1;
else {
high=mid-1;
}
if(found==0){
printf("Element not found in the array.\n");
return 0;
OUTPUT:
Enter the number of elements in the array:5
Enter 5 elements in sorted order.
10 20 30 40 50
Enter the element to search:30
Element found at position 3
[Link] a program to sort a given set of numbers using
selection sort algorithm
#include<stdio.h>
int main()
int i,j,min,a[100],n;
printf("Enter the no. of elements:");
scanf("%d",&n);
printf("Enter the elements:");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
for(i=0;i<n-1;i++)
{
min=a[i];
for(j=i+1;j<n;j++)
if(a[j]<min)
min=a[j];
a[j]=a[i];
a[i]=min;
printf("The elemenets are:");
for(i=0;i<n;i++)
printf("%d",a[i]);
OUTPUT:
Enter the no. of elements:5
Enter the elements:64 25 12 22 11
The elements are:11 12 22 25 64
[Link] a program to sort a given set of numbers using
bubble sort algorithm
#include<stdio.h>
int main(){
int arr[100],n,i,j,temp;
printf("Enter the number of elements:");
scanf("%d",&n);
printf("Enter %d elements:\n",n);
for(i=0;i<n;i++){
scanf("%d",&arr[i]);
for(i=0;i<n-1;i++){
for(j=0;j<n-i-1;j++){
if(arr[j]>arr[j+1]){
temp=arr[j];
arr[j]=arr[j+1];
arr[j+1]=temp;
printf("Sorted array in ascending order:\n");
for(i=0;i<n;i++){
printf("%d",arr[i]);
return 0;
OUTPUT:
Enter the number of elements:5
Enter 5 elements:
64 34 25 12 22
Sorted array in ascending order:
12 2 25 34 64
[Link] a program to sort a given set of numbers using
insertion sort algorithm
#include<stdio.h>
int main(){
int arr[100],n,i,j,key;
printf("Enter the number of elements:");
scanf("%d",&n);
printf("Enter %d elements:\n",n);
for(i=0;i<n;i++){
scanf("%d",&arr[i]);
for(i=1;i<n;i++){
key=arr[i];
j=i-1;
while(j>=0 &&arr[j]>key){
arr[j+1]=arr[j];
j--;
arr[j+1]=key;
printf("Sorted array in ascending order:\n");
for(i=0;i<n;i++){
printf("%d",arr[i]);
return 0;
OUTPUT:
Enter the number of elements:6
Enter 6 elements:
12 11 13 5 6 7
Sorted array in ascending order:
5 6 7 11 12 13
[Link] a program to sort a given set of numbers using
Merge sort algorithm
#include<stdio.h>
void mergesort(int a[],int low, int high);
void merge(int a[],int low,int mid,int high);
int main()
int a[50],i,n;
printf("Enter the number of elements: ");
scanf("%d",&n);
printf("Enter the array elements: ");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
mergesort(a,0,n-1);
printf("\n Sorted Array is : ");
for(i=0;i<n;i++)
printf("%d",a[i]);
printf("\n");
return 0;
void mergesort(int a[],int low,int high)
{
int mid;
if(low<high)
mid=(low+high)/2;
mergesort(a,low,mid);
mergesort(a,mid+1,high);
merge(a,low,mid,high);
void merge(int a[],int low,int mid,int high)
int c[50];
int i,j,k;
i=low;
j=mid+1;
k=low;
while((i<=mid)&&(j<=high))
if(a[i]<=a[j])
c[k]=a[i];
i++;
k++;
else{
c[k]=a[j];
j++;
k++;
}
while(i<=mid)
c[k]=a[i];
k++;
i++;
while(j<=high)
c[k]=a[j];
k++;
j++;
for(i=low;i<=k-1;i++)
a[i]=c[i];
OUTPUT:
Enter the number of elements: 5
Enter the array elements: 1 2 3 4 5
Sorted Array is : 12345
[Link] a program to implement matrix multiplication
#include <stdio.h>
int main()
int a[10][10], b[10][10], result[10][10];
int r1, c1, r2, c2, i, j, k;
printf("Enter rows and columns of first matrix: ");
scanf("%d %d", &r1, &c1);
printf("Enter rows and columns of second matrix: ");
scanf("%d %d", &r2, &c2);
// Check if multiplication is possible
if (c1 != r2) {
printf("Matrix multiplication not possible.\n");
return 0;
// Optional: size check
if (r1 > 10 || c1 > 10 || r2 > 10 || c2 > 10) {
printf("Matrix size too large (max 10x10).\n");
return 0;
// Input first matrix
printf("Enter the elements of first matrix:\n");
for (i = 0; i < r1; i++) {
for (j = 0; j < c1; j++) {
scanf("%d", &a[i][j]);
// Input second matrix
printf("Enter the elements of second matrix:\n");
for (i = 0; i < r2; i++) {
for (j = 0; j < c2; j++) {
scanf("%d", &b[i][j]);
// Initialize result matrix to 0
for (i = 0; i < r1; i++) {
for (j = 0; j < c2; j++) {
result[i][j] = 0;
// Matrix multiplication
for (i = 0; i < r1; i++) {
for (j = 0; j < c2; j++) {
for (k = 0; k < c1; k++) {
result[i][j] += a[i][k] * b[k][j];
// Display result
printf("Resultant matrix after multiplication:\n");
for (i = 0; i < r1; i++) {
for (j = 0; j < c2; j++) {
printf("%d ", result[i][j]); // space added for proper formatting
printf("\n");
return 0;
OUTPUT:
Enter rows and columns of first matrix: 2 2
Enter rows and columns of second matrix: 2 2
Enter the elements of first matrix:
12
34
Enter the elements of second matrix:
5 67
87
Resultant matrix after multiplication:
21 81
47 229
[Link] a program to find the factorial of a given
number using recursive function
#include<stdio.h>
int factorial(int n) {
if(n==0 || n==1)
return 1;
else
return n*factorial(n-1);
int main(){
int num;
printf("Enter a positive integer : ");
scanf("%d",&num);
if(num<0)
printf("Factorial is not defined for negative numbers.\n");
else
printf("Factorial of %d = %d\n",num,factorial(num));
return 0;
OUTPUT:
Enter a positive integer: 5
Factorial of 5 = 120
[Link] a program to find the minimum spanning tree
of a given graph using prim’s algorithm
#include<stdio.h>
#define INF 9999
#define MAX 100
int main(){
int cost[MAX][MAX];
int visited[MAX];
int n;
int i,j,ne=1;
int min,a,b;
int total_cost=0;
printf("Enter the number of vertices: ");
scanf("%d",&n);
printf("Enter the cost adjancency matrix(enter 0 if no edge):\n");
for(i=1;i<=n;i++) {
for(j=1;j<=n;j++){
scanf("%d",&cost[i][j]);
if(cost[i][j]==0)
cost[i][j]=INF;
visited[1]=1;
for(i=2;i<=n;i++)
visited[i]=0;
printf("\nEdges in the Minimum Spanning Tree: \n");
while(ne<n){
min=INF;
for(i=1;i<=n;i++) {
if(visited[i]){
for(j=1;j<=n;j++){
if(!visited[j]&&cost[i][j]<min){
min=cost[i][j];
a=i;
b=j;
printf("%d edge(%d,%d)=%d\n",ne++,a,b,min);
total_cost += min;
visited[b]=1;
cost[a][b]=cost[b][a]=INF;
printf("\nTotal cost of Minimum Spanning Tree = %d\n",total_cost);
return 0;
OUTPUT:
Enter the number of vertices: 4
Enter the cost adjacency matrix (enter 0 if no edge):
0206
2038
0300
6800
Edges in the Minimum Spanning Tree:
1 edge (1,2) =2
2 edge (2,3)=3
3 edge (1,4) =6
Total cost of Minimum Spanning Tree = 11
[Link] a program to find the minimum spanning tree
of a given graph using prim’s algorithm
#include<stdio.h>
#define MAX 100
#define INF 9999
struct Edge{
int u,v,w;
}edge[MAX];
int find(int parent[],int i) {
while(parent[i])
i=parent[i];
return i;
}
int unionSet(int parent[],int i,int j) {
if(i!=j) {
parent[j]=i;
return 1;
return 0;
int main() {
int n,e;
int i,j,a,b,u,v;
int total_cost=0;
int parent[MAX]={0};
struct Edge temp;
printf("Enter the number of vertices: ");
scanf("%d",&n);
printf("Enter the number of edges : ");
scanf("%d",&e);
printf("Enter the edges and their weights (u v w):\n");
for(i=0;i<e;i++) {
scanf("%d %d %d",&edge[i].u,&edge[i].v,&edge[i].w);
for(i=0;i<e-1;i++){
for(j=0;j<e-i-1;j++) {
if(edge[j].w>edge[j+1].w){
temp = edge[j];
edge[j]=edge[j+1];
edge[j+1]=temp;
}
printf("\nEdges in the Minimum Spanning Tree:\n");
int edgeCount = 0;
for(i=0;i<e;i++) {
a=find(parent,edge[i].u);
b=find(parent,edge[i].v);
if(unionSet(parent,a,b)){
printf("Edge(%d %d)=%d\n",edge[i].u,edge[i].v,edge[i].w);
total_cost += edge[i].w;
edgeCount++;
if(edgeCount==n-1)
break;
printf("\nTotal cost of Minimum Spanning Tree = %d\n",total_cost);
return 0;
OUTPUT:
Enter the number of vertices: 3
Enter the number of edges: 3
Enter the edges and their weights (u v w):
123
456
789
Edges in the Minimum Spanning Tree:
Edge (1 2) = 3
Edge (4 5) = 6
Total cost of Minimum Spanning Tree = 9
[Link] a program to find the binomial coefficient
C(n, k),[Where n and k are integers and n>k] using
brute force based algorithm and dynamic programming
based algorithm .
#include <stdio.h>
int binomialCoeff(int n, int k) {
if (k==0|| k==n)
return 1;
else
return binomialCoeff(n - 1, k - 1) + binomialCoeff(n - 1, k);
int main(){
int n,k;
printf("Enter values for n and k (n > k): ");
scanf("%d %d",&n,&k);
printf("Binomial Coefficient C(%d ,%d)=%d\n",n,k,binomialCoeff(n,k));
return 0;
}
OUTPUT:
Enter values for n and k (n > k): 5 2
Binomial Coefficient C(5 ,2)=10
[Link] a program to implement BFS traversal
algorithm
#include<stdio.h>
#define MAX 100
int queue[MAX],front=-1,rear=-1;
int visited[MAX];
int adj[MAX][MAX];
int n;
void enqueue(int vertex){
if(rear==MAX-1)
printf("Queue Overflow\n");
else{
if (front==-1)
front=0;
queue[++rear]=vertex;
int dequeue(){
if(front==-1||front>rear)
return -1;
else
return queue[front++];
void BFS(int startVertex){
int i, currentVertex;
enqueue(startVertex);
visited[startVertex]=1;
printf("BFS Traversal starting from vertex %d:",startVertex);
while(front<=rear){
currentVertex=dequeue();
printf("%d",currentVertex);
for(i=0;i<n;i++){
if(adj[currentVertex][i]==1 && !visited[i]){
enqueue(i);
visited[i]=1;
printf("\n");
int main(){
int i,j,startVertex;
printf("Enter the number of vertices:");
scanf("%d",&n);
printf("Enter the adjacency matrix of the graph:\n");
for(i=0;i<n;i++){
for(j=0;j<n;j++){
scanf("%d",&adj[i][j]);
for(i=0;i<n;i++)
visited[i]=0;
printf("Enter the starting vertex (0 to %d):",n-1);
scanf("%d",&startVertex);
BFS(startVertex);
return 0;
OUTPUT:
Enter the number of vertices:4
Enter the adjacency matrix of the graph:
0110
1001
1001
0110
Enter the starting vertex (0 to 3):0
BFS Traversal starting from vertex 0:0123
[Link] a program to implement DFS traversal algorithm
#include <stdio.h>
#define MAX 100
int adj[MAX][MAX];
int visited[MAX];
int n;
void DFS(int vertex) {
int i;
visited[vertex] = 1;
printf("%d ", vertex);
for (i = 0; i < n; i++) {
if (adj[vertex][i] == 1 && !visited[i]) {
DFS(i);
int main() {
int i, j, startVertex;
printf("Enter the number of vertices: ");
scanf("%d", &n);
printf("Enter the adjacency matrix of the graph:\n");
for (i = 0; i < n; i++) {
for (j = 0; j < n; j++) {
scanf("%d", &adj[i][j]);
for (i = 0; i < n; i++) {
visited[i] = 0;
}
printf("Enter the starting vertex (0 to %d): ", n - 1);
scanf("%d", &startVertex);
printf("DFS Traversal starting from vertex %d: ", startVertex);
DFS(startVertex);
printf("\n");
return 0;
OUTPUT:
Enter the number of vertices: 4
Enter the adjacency matrix of the graph:
0110
1001
1000
0100
Enter the starting vertex (0 to 3): 0
DFS Traversal starting from vertex 0: 0 1 3 2