DS LAB Part A
DS LAB Part A
DS LAB Part A
PART - A
#include<stdio.h>
#include<math.h>
Vijayalaxmi S B 1
2. Program to generate binomial coefficient using using recursive function.
#include <stdio.h>
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;
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;
if (arr == NULL)
{
printf("Memory allocation failed.");
return 1;
}
max = arr[0];
min = arr[0];
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