0% found this document useful (0 votes)
9 views18 pages

Vipin DAA

The document contains multiple programming problems and their solutions written in C, focusing on algorithms such as finding duplicates in an array, calculating factorials, GCD, Fibonacci series, and various sorting techniques (Bubble, Selection, Insertion, Merge). Each problem is accompanied by code snippets and expected outputs. The document serves as a practical guide for implementing fundamental algorithms and data structures.

Uploaded by

soulgamer109
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views18 pages

Vipin DAA

The document contains multiple programming problems and their solutions written in C, focusing on algorithms such as finding duplicates in an array, calculating factorials, GCD, Fibonacci series, and various sorting techniques (Bubble, Selection, Insertion, Merge). Each problem is accompanied by code snippets and expected outputs. The document serves as a practical guide for implementing fundamental algorithms and data structures.

Uploaded by

soulgamer109
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 18

Name: Vipin Rana BCA (G1) Roll No.

: 72
Semster: 4

Problem Statement 1 : Write a program to find the duplicate element in the array.

Code :

#include <stdio.h>

void DuplicateElement(int arr[], int size) {

printf("Duplicate elements: ");

for (int i = 0; i < size - 1; i++) {

for (int j = i + 1; j < size; j++) {

if (arr[i] == arr[j]) {

printf("%d ", arr[i]);

break;

printf("\n");

int main() {

int arr[] = {1, 2, 3, 4, 3, 2, 6};

int size = sizeof(arr) / sizeof(arr[0]);

DuplicateElement(arr, size);

return 0;

Output :

1
Name: Vipin Rana BCA (G1) Roll No.: 72
Semster: 4

Problem Statement 2 : Write a program to find the largest & third largest element in the
array.

#include <stdio.h>

void firstandthirdarray(int arr[], int size) {

if (size < 3) {

printf("Array should have at least three elements.\n");

return;

int first = arr[0], second = -1, third = -1;

for (int i = 1; i < size; i++) {

if (arr[i] > first) {

third = second;

second = first;

first = arr[i];

else if (arr[i] > second && arr[i] < first) {

third = second;

second = arr[i];

else if (arr[i] > third && arr[i] < second) {

third = arr[i];

printf("Largest = %d\n", first);

printf("Second Largest = %d\n", second);

printf("Third largest = %d\n", third);

2
Name: Vipin Rana BCA (G1) Roll No.: 72
Semster: 4

int main() {

int arr[] = {12, 34, 22, 54, 45};

int size = sizeof(arr) / sizeof(arr[0]);

firstandthirdarray(arr, size);

return 0;

OUTPUT:

3
Name: Vipin Rana BCA (G1) Roll No.: 72
Semster: 4

Problem Statement 3 :Write a program to find the factorial of a number using recursion.

Code :

#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 the number: ");

scanf("%d", &num);

if (num < 0) {

printf("Factorial is not defined for negative numbers.\n");

} else {

printf("Factorial of %d is %d\n", num, factorial(num));

return 0;

Output :

4
Name: Vipin Rana BCA (G1) Roll No.: 72
Semster: 4

Problem Statement 4 :Write a program to find the GCD of two numbers using recursion.

Code :

#include <stdio.h>

int gcd(int a, int b) {

if (b == 0)

return a;

return gcd(b, a % b);

int main() {

int num1 = 56, num2 = 98;

printf("GCD of %d and %d is %d\n", num1, num2, gcd(num1, num2));

return 0;

Output :

5
Name: Vipin Rana BCA (G1) Roll No.: 72
Semster: 4

Problem Statement 5 :Write a program to implement the Fibonacci series using recursion.

Code :

#include <stdio.h>

int fibonacci(int n) {

if (n == 0) return 0;

if (n == 1) return 1;

return fibonacci(n - 1) + fibonacci(n - 2);

int main() {

int n = 10;

printf("Fibonacci series: ");

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

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

return 0;

Output :

6
Name: Vipin Rana BCA (G1) Roll No.: 72
Semster: 4

Problem Statement6 :Write a program to implement linear search in an array.

Code :

#include <stdio.h>

#include <stdlib.h>

int main() {

int item, i = 0;

int a[5] = {34, 56, 32, 56, 65};

printf("Enter the item to search: ");

scanf("%d", &item);

while (i < 5) {

if (a[i] == item) {

printf("Item found at index %d\n", i);

exit(0);

i++;

printf("Item not found\n");

return 0;

Output :

7
Name: Vipin Rana BCA (G1) Roll No.: 72
Semster: 4

Problem Statement 7 :Write a program to implement binary search in an array.

Code :

#include <stdio.h>

int binarySearch(int a[], int i, int j, int x) {

if (i > j)

return -1;

int mid = (i + j) / 2;

if (a[mid] == x)

return mid;

else if (a[mid] > x)

return binarySearch(a, i, mid - 1, x);

else

return binarySearch(a, mid + 1, j, x);

int main() {

int arr[] = {1, 2, 3, 3, 15, 5, 6};

int size = sizeof(arr) / sizeof(arr[0]);

int x, result;

printf("Enter the number to search: ");

scanf("%d", &x);

result = binarySearch(arr, 0, size - 1, x);

if (result != -1)

printf("Element found at index %d\n", result);

else

printf("Element not found\n");

return 0;

8
Name: Vipin Rana BCA (G1) Roll No.: 72
Semster: 4

Output :

9
Name: Vipin Rana BCA (G1) Roll No.: 72
Semster: 4

Problem Statement 8 :Write a program using a recursive function to implement the Tower of
Hanoi.

Code :

#include <stdio.h>

void Toh(int n, char s, char d, char t)

if (n == 0)

return;

Toh(n - 1, s, t, d);

printf("\nDisk %d moves from %c to %c", n, s, d);

Toh(n - 1, t, d, s);

int main() {

int n = 3;

Toh(n, 'S', 'D', 'T');

return 0;

Output :

10
Name: Vipin Rana BCA (G1) Roll No.: 72
Semster: 4

Problem Statement 9 : WAP to implement Bubble Sort Technique in an array using recursion.
Compute the time complexity.

Code:

#include <stdio.h>

void bubbleSort(int arr[], int n)

for (int j = 0; j < n - 1; j++)

for (int i = 0; i < n - j - 1; i++)

if (arr[i] > arr[i + 1])

int temp = arr[i];

arr[i] = arr[i + 1];

arr[i + 1] = temp;

void printArray(int arr[], int n)

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

printf("%d ", arr[i]);

printf("\n");

11
Name: Vipin Rana BCA (G1) Roll No.: 72
Semster: 4

int main() {

int arr[] = {64, 34, 25, 12, 22, 11, 90};

int n = sizeof(arr) / sizeof(arr[0]);

printf("Original array: ");

printArray(arr, n);

bubbleSort(arr, n);

printf("Sorted array: ");

printArray(arr, n);

return 0;

Output :

12
Name: Vipin Rana BCA (G1) Roll No.: 72
Semster: 4

Problem Statement 10 : WAP to implement Selection Sort Technique in an array using


recursion. Compute the time complexity.

Code :

#include <stdio.h>

void selectionSort(int arr[], int n) {

for (int i = 0; i < n - 1; i++) {

int minIndex = i;

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

if (arr[j] < arr[minIndex]) {

minIndex = j;

int temp = arr[minIndex];

arr[minIndex] = arr[i];

arr[i] = temp;

void printArray(int arr[], int n) {

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

printf("%d ", arr[i]);

printf("\n");

int main() {

int arr[] = {64, 25, 12, 22, 11};

int n = sizeof(arr) / sizeof(arr[0]);

13
Name: Vipin Rana BCA (G1) Roll No.: 72
Semster: 4

printf("Original array: ");

printArray(arr, n);

selectionSort(arr, n);

printf("Sorted array: ");

printArray(arr, n);

return 0;

Output :

14
Name: Vipin Rana BCA (G1) Roll No.: 72
Semster: 4

Problem Statement 11 : WAP to implement Insertion Sort Technique in an array using


recursion. Compute the time complexity.

#include <stdio.h>

void insertionSort(int arr[], int n) {

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

int key = arr[i];

int j = i - 1;

while (j >= 0 && arr[j] > key) {

arr[j + 1] = arr[j];

j--;

arr[j + 1] = key;

void printArray(int arr[], int n) {

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

printf("%d ", arr[i]);

printf("\n");

int main() {

int arr[] = {64, 25, 12, 22, 11};

int n = sizeof(arr) / sizeof(arr[0]);

printf("Original array: ");

printArray(arr, n);

15
Name: Vipin Rana BCA (G1) Roll No.: 72
Semster: 4

insertionSort(arr, n);

printf("Sorted array: ");

printArray(arr, n);

return 0;

output :

16
Name: Vipin Rana BCA (G1) Roll No.: 72
Semster: 4

Problem Statement 12 : WAP to implement Merge Sort Algorithm. Compute its time
complexity.

Code :

#include <stdio.h>

void merge(int arr[], int left, int mid, int right) {

int i = left, j = mid + 1, k = 0;

int temp[right - left + 1];

while (i <= mid && j <= right) {

temp[k++] = (arr[i] < arr[j]) ? arr[i++] : arr[j++];

while (i <= mid) temp[k++] = arr[i++];

while (j <= right) temp[k++] = arr[j++];

for (i = left, k = 0; i <= right; i++, k++) {

arr[i] = temp[k];

void mergeSort(int arr[], int left, int right) {

if (left < right) {

int mid = (left + right) / 2;

mergeSort(arr, left, mid);

mergeSort(arr, mid + 1, right);

merge(arr, left, mid, right);

17
Name: Vipin Rana BCA (G1) Roll No.: 72
Semster: 4

void printArray(int arr[], int n) {

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

printf("%d ", arr[i]);

printf("\n");

int main() {

int arr[] = {38, 27, 43, 3, 9, 82, 10};

int n = sizeof(arr) / sizeof(arr[0]);

printf("Original array: ");

printArray(arr, n);

mergeSort(arr, 0, n - 1);

printf("Sorted array: ");

printArray(arr, n);

return 0;

Output :

18

You might also like