DS LAB Part A

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 9

DATA STRUCTURE LAB PROGRAMS

PART - A

1. Program to find GCD using recursive function.

#include<stdio.h>
#include<math.h>

int GCD(int i, int j);


int main()
{
int a,b;
printf("Enter the two integers: ");
scanf("%d%d", &a, &b);
printf("GCD of %d and %d is %d", a, b, GCD(a, b));
return 0;
}
/* Recursive Function*/
int GCD(int i, int j)
{
if(j>i)
return GCD(j, i);
if(j==0)
return i;
else
return GCD(j, i % j);
}

Vijayalaxmi S B 1
2. Program to generate binomial coefficient using using recursive function.

#include <stdio.h>

int Ceff(int n,int k);


int main()
{
unsigned int n,k,result;
printf("\n enter n = ");
scanf("%d",&n);
printf("\n enter k = ");
scanf("%d",&k);
result=Ceff(n,k);
printf("\n %d\n",result);
return 0;
}

int Ceff(int n,int k)


{
if(k==0 || k==n)
return 1;
return Ceff(n-1,k-1)+Ceff(n-1,k);
}

Vijayalaxmi S B 2
3. Program to generate n Fibonacci numbers using recursive function.

#include <stdio.h>

int fibonacci(int n)
{
if(n == 0)
return 0;
else if(n == 1)
return 1;
else
return (fibonacci(n-1) + fibonacci(n-2));
}

int main()
{
int n;

printf("Enter the number of terms\n");


scanf("%d", &n);

printf("Fibonacci Series: ");

for (int i = 0; i < n; i++)


{
printf("%d ", fibonacci(i));
}

return 0;
}

Vijayalaxmi S B 3
4. Program to implement Towers of Hanoi using recursion.

#include <stdio.h>
void hanoi(int n, char from, char to, char via)
{
if(n == 1)
{
printf("Move disk 1 from %c to %c\n", from, to);
}
Else
{
hanoi(n-1, from, via, to);
printf("Move disk %d from %c to %c\n", n, from, to);
hanoi(n-1, via, to, from);
}
}
int main()
{
int n = 3;
char from = 'A';
char to = 'B';
char via = 'C';
//calling hanoi() method
hanoi(n, from, via, to);
}

Vijayalaxmi S B 4
5. Program to implement dynamic array, find smallest and largest element of the
array.

#include <stdio.h>
#include <stdlib.h>

int main()
{
int *arr;
int n, i, max, min;

printf("Enter the number of elements in the array: ");


scanf("%d", &n);

arr = (int *)malloc(n * sizeof(int));

if (arr == NULL)
{
printf("Memory allocation failed.");
return 1;
}

printf("Enter the elements of the array: ");


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

max = arr[0];
min = arr[0];

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


if (arr[i] > max) {
max = arr[i];
}
if (arr[i] < min) {
min = arr[i];
}
}

printf("Maximum element: %d\n", max);


printf("Minimum element: %d\n", min);

free(arr);

return 0;
}

Vijayalaxmi S B 5
6. Program to read the names of cities and arrange them alphabetically.

#include<stdio.h>
#include<string.h>
int main()
{
int i,j,n;
char str[100][100],s[100];
printf("Enter number of names :");
scanf("%d",&n);
printf("Enter city names:");
for(i=0;i<n;i++)
{
scanf("%s",str[i]);
}
for(i=0;i<n;i++){
for(j=i+1;j<n;j++){
if(strcmp(str[i],str[j])>0){
strcpy(s,str[i]);
strcpy(str[i],str[j]);
strcpy(str[j],s);
}
}
}
printf("The sorted order of city names are:");
for(i=0;i<n;i++)
{
printf("%s\n",str[i]);
}
}

Vijayalaxmi S B 6
7. Program to sort the given list using selection sort technique.

#include <stdio.h>
int main()
{
int arr[10]={6,2,12,9,5,45,52,9,21,1};
int n=10;
int i, j, loc, swap;
for (i = 0; i < (n - 1); i++)
{
loc = i;
for (j = i + 1; j < n; j++)
{
if (arr[loc] > arr[j])
loc = j;
}
if (loc != i)
{
swap = arr[i];
arr[i] = arr[loc];
arr[loc] = swap;
}
}
for (i = 0; i < n; i++)
printf("%d\t", arr[i]);
return 0;
}

Vijayalaxmi S B 7
8. Program to sort the given list using bubble sort technique.

#include <stdio.h>
int main()
{
int n, j, I, swap;
printf(“Enter number of elements\n”);
scanf(“%d”, &n);
int arr[n];
printf(“Enter %d integers\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])
{
swap = arr[j];
arr[j] = arr[j+1];
arr[j+1] = swap;
}
}
}
printf(“Sorted list in ascending order:\n”);
for (I = 0; I < n; i++)
printf(“%d\n”, arr[i]);
return 0;
}

Vijayalaxmi S B 8
9. Program to sort the given list using insertion sort technique.

#include <stdio.h>
void insertionSort(int arr[], int n)
{
int i, key, j;
for (i = 1; i < n; i++)
{
key = arr[i];
j = i - 1;
while (j >= 0 && arr[j] > key)
{
arr[j + 1] = arr[j];
j = j - 1;
}
arr[j + 1] = key;
}
}
int main()
{
int arr[] = { 12, 11, 13, 5, 6 };
int n = sizeof(arr) / sizeof(arr[0]);
insertionSort(arr, n);
for (int i = 0; i < n; i++)
printf("%d ", arr[i]);
printf("\n");
return 0;
}

Vijayalaxmi S B 9

You might also like