0% found this document useful (0 votes)
3 views13 pages

lab work dsa

The document contains several programming labs focused on fundamental algorithms, including finding the Greatest Common Divisor (GCD), generating the Fibonacci series, calculating factorials using recursion, and implementing sorting algorithms like Insertion Sort and Selection Sort. Each lab provides a brief explanation of the concept, followed by source code examples in C. The document serves as a practical guide for understanding and implementing these algorithms.

Uploaded by

DEVRAJ SILWAL
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)
3 views13 pages

lab work dsa

The document contains several programming labs focused on fundamental algorithms, including finding the Greatest Common Divisor (GCD), generating the Fibonacci series, calculating factorials using recursion, and implementing sorting algorithms like Insertion Sort and Selection Sort. Each lab provides a brief explanation of the concept, followed by source code examples in C. The document serves as a practical guide for understanding and implementing these algorithms.

Uploaded by

DEVRAJ SILWAL
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/ 13

LAB: 1

Write a program to find out the Greatest Common divisor of two numbers

Greatest Common Divisor:


The greatest common divisor (GCD) of two or more numbers is the greatest common factor number that
divides them, exactly. It is also called the highest common factor (HCF). For example, the greatest common
factor of 15 and 10 is 5, since both the numbers can be divided by 5.

15/5 = 3

10/5 = 2

If a and b are two numbers then the greatest common divisor of both the numbers is denoted by gcd(a, b). To
find the gcd of numbers, we need to list all the factors of the numbers and find the largest common factor.

Suppose, 4, 8 and 16 are three numbers. Then the factors of 4, 8 and 16 are:

4 → 1,2,4

8 → 1,2,4,8

16 → 1,2,4,8,16

Therefore, we can conclude that 4 is the highest common factor among all three numbers.
SOURCE CODE:
#include<stdio.h>

int main()
{
int a,b;
printf("\t\t************\n");
printf("\t\tGCD\n");
printf("\t\t************\n");
printf("Enter the value of both the numbers(a,b):\n");
scanf("%d%d",&a,&b);
printf("The GCD Of (%d,%d) is:\n%d",a,b,gcd(a,b));
return 0;
}
int gcd(int a, int b)
{
if(b>a)
{
printf("B is greater than a\n");
}
else if(b==0)
{
return a;
}
else
{
return gcd(b, a%b);
}
}

OUTPUT
LAB 2

Write a program to generate the Fibonacci Series

Fibonacci Series:

The Fibonacci series is a sequence of numbers in which each term is the sum of the two preceding ones,
starting with 0 and 1. This sequence follows the pattern: 0, 1, 1, 2, 3, 5, 8, 13, 21, and so on.The Fibonacci
sequence appears in various natural phenomena, such as the arrangement of leaves in plants, the branching
of trees, and the spirals of shells.

Mathematically, it is defined by the recurrence relation

F(n) = F(n-1) + F(n-2) for n ≥ 2, with F(0) = 0 and F(1) = 1.

The series is also closely linked to the golden ratio, as the ratio of successive Fibonacci numbers
approximates 1.618, a number that frequently appears in art, architecture, and nature.
Source Code

#include <stdio.h>
int main() {

int i, n;

int t1 = 0, t2 = 1;
int nextTerm = t1 + t2;

printf("Enter the number of terms: ");


scanf("%d", &n);

printf("Fibonacci Series: %d, %d, ", t1, t2);

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


printf("%d, ", nextTerm);
t1 = t2;
t2 = nextTerm;
nextTerm = t1 + t2;
}

return 0;
}

OUTPUT
LAB 3

Write a program to find the factorial of an input number using recursive function.

Factorial of a Number:

Factorial of a non-negative integer is the multiplication of all positive integers smaller than or equal to n.
For example factorial of 6 is 6*5*4*3*2*1 which is 720.

A factorial is represented by a number and a ” ! ” mark at the end. It is widely used in permutations and
combinations to calculate the total possible outcomes. A French mathematician Christian Kramp firstly
used the exclamation.

Formula

n! = n(n-1)(n-2)…………………….(3)(2)(1)
n! = n × (n - 1)!
Source Code

#include<stdio.h>

int facto(int);
void main()
{

printf("\t\t******************\n");
printf("\t\t Factorial of a number:\n");
printf("\t\t******************\n");
int num, res;
printf("Enter a number to calculate its factorial:\n");
scanf("%d",&num);
res = facto(num); printf("%d! = %d" ,num ,res);

}
int facto(int num)
{
int f=1;
if(num <= 0)
{
return(1);
}
else
{
f = num * facto(num-1);
return(f);
}
}
OUTPUT
LAB 5

Write a program to implement Insertion Sort

Insertion Sort:

Insertion sort is a simple sorting algorithm that works by iteratively inserting each element of
an unsorted list into its correct position in a sorted portion of the list. It is like sorting playing
cards in your hands. You split the cards into two groups: the sorted cards and the unsorted
cards. Then, you pick a card from the unsorted group and put it in the right place in the sorted
group.
 We start with second element of the array as first element in the array is assumed to be
sorted.
 Compare second element with the first element and check if the second element is smaller
then swap them.
 Move to the third element and compare it with the first two elements and put at its correct
position
 Repeat until the entire array is sorted.
Source Code

#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 = j - 1;
}
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[] = { 12, 11, 13, 5, 6 };
int n = sizeof(arr) / sizeof(arr[0]);

insertionSort(arr, n);
printArray(arr, n);

return 0;
}

OUTPUT
LAB 5

Write a program to implement Selection Sort

Selection Sort:

Selection Sort is a comparison-based sorting algorithm. It sorts an array by repeatedly selecting


the smallest (or largest) element from the unsorted portion and swapping it with the first unsorted
element. This process continues until the entire array is sorted.
1. First we find the smallest element and swap it with the first element. This way we get the smallest
element at its correct position.
2. Then we find the smallest among remaining elements (or second smallest) and swap it with the second
element.
3. We keep doing this until we get all elements moved to correct position.
Source Code

#include <stdio.h>

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


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

int min_idx = i;

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


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

min_idx = j;
}
}

int temp = arr[i];


arr[i] = arr[min_idx];
arr[min_idx] = 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]);

printf("Original array: ");


printArray(arr, n);

selectionSort(arr, n);

printf("Sorted array: ");


printArray(arr, n);

return 0;
}
OUTPUT

You might also like