lab work dsa
lab work dsa
Write a program to find out the Greatest Common divisor of two numbers
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
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.
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;
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
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;
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
Selection Sort:
#include <stdio.h>
int min_idx = i;
min_idx = j;
}
}
int main() {
int arr[] = {64, 25, 12, 22, 11};
int n = sizeof(arr) / sizeof(arr[0]);
selectionSort(arr, n);
return 0;
}
OUTPUT