algo lab
algo lab
#include <stdio.h>
int fibonacci(int n)
{
if (n <= 1)
return n;
int a = 0, b = 1, c;
for (int i = 2; i <= n; i++)
{ c = a + b; a = b; b = c;
}
return b;
} int main()
{
int n;
printf("Enter the position of Fibonacci number: ");
scanf("%d", &n);
printf("Fibonacci number at position %d is %d\n", n, fibonacci(n));
return 0;
}
Output
#include <stdio.h>
int reverseNumber(int n) {
int reversed = 0;
while (n != 0) {
reversed = reversed * 10 + n % 10;
n /= 10;
}
return reversed;
}
int main() {
int n;
printf("Enter a decimal number: ");
scanf("%d", &n);
printf("Reversed number: %d\n", reverseNumber(n));
return 0;
}
Output
Enter a decimal number: 12345
Reversed number: 54321
#include <stdio.h>
binary /= 10;
base *= 2;
return decimal;
}
int main() {
int binary;
scanf("%d", &binary);
return 0;
Output
Enter a binary number: 1234
Decimal equivalent: 26
#include <stdio.h>
double factorial(int n) {
double result = 1;
result *= i;
return result;
double sinx(double x) {
double result = 0;
result += term;
return result;
int main() {
double x;
scanf("%lf", &x);
return 0;
output
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
void main()
{
int n, x1;
float accuracy, term, denominator, x, cosx, cosval;
printf("Enter the value of x (in degrees) \n");
scanf("%f", &x);
x1 = x;
/* Converting degrees to radians */
x = x * (3.142 / 180.0);
cosval = cos(x);
printf("Enter the accuracy for the result \n");
scanf("%f", &accuracy);
term = 1;
cosx = term;
n = 1;
do
{
denominator = 2 * n * (2 * n - 1);
term = -term * x * x / denominator;
cosx = cosx + term;
n = n + 1;
} while (accuracy <= fabs(cosval - cosx));
printf("Sum of the cosine series = %f\n", cosx);
printf("Using Library function cos(%d) = %f\n", x1, cos(x));
}
Output
Enter the value of x (in degrees)
60
Enter the accuracy for the result
0.86602
Sum of the cosine series = 0.451546
Using Library function cos(60) = 0.499882
#include <math.h>
#include <stdio.h>
int main()
{
// 30 degrees in radians
double a = M_PI / 6;
// 45 degrees in radians
double b = M_PI / 4;
// 60 degrees in radians
double c = M_PI / 3;
// 90 degrees in radians
double d = M_PI / 2;
// Variable to store the results
double res;
res = tan(a);
printf("The tangent of %.3lf radians (30 degrees) is "
"%.3lf\n",
a, res);
res = tan(b);
printf("The tangent of %.3lf radians (45 degrees) is "
"%.3lf\n",
b, res);
res = tan(c);
printf("The tangent of %.3lf radians (60 degrees) is "
"%.3lf\n",
c, res);
res = tan(d);
printf("The tangent of %.3lf radians (90 degrees) is "
"%.3lf\n",
d, res);
return 0;
}
output
The tangent of 0.524 radians (30 degrees) is 0.577
The tangent of 0.785 radians (45 degrees) is 1.000
The tangent of 1.047 radians (60 degrees) is 1.732
The tangent of 1.571 radians (90 degrees) is 16331239353195370.000
7. Calculate GCD of Two Numbers
#include <stdio.h>
int main() {
int a, b;
printf("Enter two numbers: ");
scanf("%d %d", &a, &b);
printf("GCD of %d and %d is %d\n", a, b, gcd(a, b));
return 0;
}
Output
Enter two numbers: 2 5
GCD of 2 and 5 is 1
#include <stdio.h>
void generatePrimes(int n) {
for (int num = 2; num <= n; num++) {
int isPrime = 1;
for (int i = 2; i * i <= num; i++) {
if (num % i == 0) {
isPrime = 0;
break;
}
}
if (isPrime) {
printf("%d ", num);
}
}
printf("\n");
}
int main() {
int n;
printf("Enter the range to generate prime numbers: ");
scanf("%d", &n);
printf("Prime numbers up to %d are: ", n);
generatePrimes(n);
return 0;
}
output
Enter the range to generate prime numbers: 100
Prime numbers up to 100 are: 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61
67 71 73 79 83 89 97
#include <stdio.h>
double squareRoot(double n) {
double low = 0, high = n, mid;
double epsilon = 0.00001;
while (high - low > epsilon) {
mid = (low + high) / 2;
if (mid * mid < n) {
low = mid;
} else {
high = mid;
}
}
return mid;
}
int main() {
double n;
printf("Enter a number: ");
scanf("%lf", &n);
printf("Square root of %lf is approximately %lf\n", n, squareRoot(n));
return 0;
}
output
Enter a number: 32
Square root of 32.000000 is approximately 5.65685
#include <stdio.h>
int main() {
char c;
printf("Enter a character: ");
scanf(" %c", &c);
printf("ASCII value of '%c' is %d\n", c, (int)c);
return 0;
}
output
Enter a character: a
ASCII value of 'a' is 97
#include <stdio.h>
int main() {
int arr[] = {5, 3, 8, 4, 2};
int n = sizeof(arr) / sizeof(arr[0]);
int pivot = 4;
partition(arr, n, pivot);
printf("Partitioned array: ");
for (int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
printf("\n");
return 0;
}
output
Partitioned array: 3 2 8 4 5
#include <stdio.h>
void removeDuplicates(int arr[], int* n) {
int temp[*n];
int j = 0;
int main() {
int arr[] = {1, 2, 3, 2, 1, 4};
int n = sizeof(arr) / sizeof(arr[0]);
removeDuplicates(arr, &n);
printf("Array after removing duplicates: ");
for (int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
printf("\n");
return 0;
}
Output
Array after removing duplicates: 1 2 3 4
#include <stdio.h>
void countDuplicates(int arr[], int n) {
int count[n];
for (int i = 0; i < n; i++) {
count[i] = 1; // Initialize counts
}
int main() {
int arr[] = {1, 2, 3, 2, 1, 4, 1};
int n = sizeof(arr) / sizeof(arr[0]);
countDuplicates(arr, n);
return 0;
}
Output
#include <stdio.h>
void primeFactors(int n) {
printf("Prime factors of %d are: ", n);
for (int i = 2; i <= n; i++) {
while (n % i == 0) {
printf("%d ", i);
n /= i;
}
}
printf("\n");
}
int main() {
int n;
printf("Enter a number: ");
scanf("%d", &n);
primeFactors(n);
return 0;
}
Output
Enter a number: 45
Prime factors of 45 are: 3 3 5
#include <stdio.h>
void reverseArray(int arr[], int n) {
int start = 0, end = n - 1;
while (start < end) {
int temp = arr[start];
arr[start] = arr[end];
arr[end] = temp;
start++;
end--;
}
}
int main() {
int arr[] = {1, 2, 3, 4, 5};
int n = sizeof(arr) / sizeof(arr[0]);
reverseArray(arr, n);
printf("Reversed array: ");
for (int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
printf("\n");
return 0;
}
output
Reversed array: 5 4 3 2 1
16. Calculate kth Smallest Number in an Array
#include <stdio.h>
#include <limits.h>
int main() {
int arr[] = {7, 10, 4, 3, 20, 15};
int n = sizeof(arr) / sizeof(arr[0]);
int k = 3; // Find 3rd smallest
printf("The %dth smallest element is %d\n", k, kthSmallest(arr, n, k));
return 0;
}
Output
#include <stdio.h>
void merge(int arr[], int left, int mid, int right) {
int i, j, k;
int n1 = mid - left + 1;
int n2 = right - mid;
i = 0;
j = 0;
k = left;
while (i < n1 && j < n2) {
if (L[i] <= R[j]) {
arr[k++] = L[i++];
} else {
arr[k++] = R[j++];
}
}
while (i < n1) arr[k++] = L[i++];
while (j < n2) arr[k++] = R[j++];
}
int main() {
int arr[] = {12, 11, 13, 5, 6, 7};
int n = sizeof(arr) / sizeof(arr[0]);
mergeSort(arr, 0, n - 1);
printf("Sorted array: ");
for (int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
printf("\n");
return 0;
}
output
Sorted array: 5 6 7 11 12 13
#include <stdio.h>
#include <limits.h>
int main() {
int arr[] = {12, 3, 5, 7, 19};
int n = sizeof(arr) / sizeof(arr[0]);
int min, max;
findMinMax(arr, n, &min, &max);
printf("Minimum: %d, Maximum: %d\n", min, max);
return 0;
}
output
Minimum: 3, Maximum: 19
19. Perform Quick Sort
#include <stdio.h>
int partition(int arr[], int low, int high) {
int pivot = arr[high];
int i = (low - 1);
for (int j = low; j < high; j++) {
if (arr[j] < pivot) {
i++;
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
int temp = arr[i + 1];
arr[i + 1] = arr[high];
arr[high] = temp;
return (i + 1);
}
int main() {
int arr[] = {10, 7, 8, 9, 1, 5};
int n = sizeof(arr) / sizeof(arr[0]);
quickSort(arr, 0, n - 1);
printf("Sorted array: ");
for (int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
printf("\n");
return 0;
}
output
Sorted array: 1 5 7 8 9 10
#include <stdio.h>
printf("Value\tCount\n");
for (int i = 0; i <= range; i++) {
if (hist[i] > 0) {
printf("%d\t%d\n", i, hist[i]);
}
}
}
int main() {
int arr[] = {1, 2, 2, 3, 1, 4, 5, 4};
int n = sizeof(arr) / sizeof(arr[0]);
int range = 5; // Max value in arr
histogram(arr, n, range);
return 0;
}
output
Value Count
1 2
2 2
3 1
4 2
5 1