Program 1: Factorial and Fibonacci
// Program to find Factorial and Fibonacci of a number
#include <stdio.h>
int factorial(int n) {
if (n == 0) return 1;
return n * factorial(n - 1);
}
int fibonacci(int n) {
if (n <= 1) return n;
return fibonacci(n - 1) + fibonacci(n - 2);
}
int main() {
int n;
printf("Enter a number: ");
scanf("%d", &n);
printf("Factorial = %d\n", factorial(n));
printf("Fibonacci = %d\n", fibonacci(n));
return 0;
}
// Sample Output:
// Enter a number: 5
// Factorial = 120
// Fibonacci = 5
Program 2: Maximum and Minimum
// Program to find Maximum and Minimum of given integer values
#include <stdio.h>
int main() {
int n, i;
printf("Enter number of elements: ");
scanf("%d", &n);
int a[n];
for(i = 0; i < n; i++)
scanf("%d", &a[i]);
int max = a[0], min = a[0];
for(i = 1; i < n; i++) {
if(a[i] > max) max = a[i];
if(a[i] < min) min = a[i];
}
printf("Maximum = %d\nMinimum = %d\n", max, min);
return 0;
}
// Sample Output:
// Enter number of elements: 5
// 2 7 4 1 9
// Maximum = 9
// Minimum = 1
Program 3: Tower of Hanoi
// Program to perform Tower of Hanoi
#include <stdio.h>
void TOH(int n, char from, char to, char aux) {
if(n == 1) {
printf("Move disk 1 from %c to %c\n", from, to);
return;
}
TOH(n - 1, from, aux, to);
printf("Move disk %d from %c to %c\n", n, from, to);
TOH(n - 1, aux, to, from);
}
int main() {
int n;
printf("Enter number of disks: ");
scanf("%d", &n);
TOH(n, 'A', 'C', 'B');
return 0;
}
// Sample Output:
// Enter number of disks: 3
// Move disk 1 from A to C
// Move disk 2 from A to B
// Move disk 3 from A to C
Program 4: Selection Sort
// Program to perform Selection Sort
#include <stdio.h>
int main() {
int n, i, j, temp, min;
printf("Enter number of elements: ");
scanf("%d", &n);
int a[n];
for(i = 0; i < n; i++)
scanf("%d", &a[i]);
for(i = 0; i < n-1; i++) {
min = i;
for(j = i+1; j < n; j++)
if(a[j] < a[min]) min = j;
temp = a[i];
a[i] = a[min];
a[min] = temp;
}
printf("Sorted array: ");
for(i = 0; i < n; i++)
printf("%d ", a[i]);
return 0;
}
// Sample Output:
// Enter number of elements: 5
// 9 3 7 1 4
// Sorted array: 1 3 4 7 9
Program 5: Uniqueness of Element
// Program to check Uniqueness of elements in an array
#include <stdio.h>
int main() {
int n, i, j, unique = 1;
printf("Enter number of elements: ");
scanf("%d", &n);
int a[n];
for(i = 0; i < n; i++)
scanf("%d", &a[i]);
for(i = 0; i < n; i++) {
for(j = 0; j < n; j++) {
if(i != j && a[i] == a[j]) {
unique = 0;
break;
}
}
}
if(unique) printf("All elements are unique.");
else printf("Elements are not unique.");
return 0;
}
// Sample Output:
// Enter number of elements: 4
// 1 2 3 4
// All elements are unique.
Program 6: Maximum Element in Array
// Program to find Maximum element in an array
#include <stdio.h>
int main() {
int n, i, max;
printf("Enter number of elements: ");
scanf("%d", &n);
int a[n];
for(i = 0; i < n; i++)
scanf("%d", &a[i]);
max = a[0];
for(i = 1; i < n; i++)
if(a[i] > max) max = a[i];
printf("Maximum element = %d", max);
return 0;
}
// Sample Output:
// Enter number of elements: 5
// 10 45 12 67 34
// Maximum element = 67
Program 7: Bubble Sort
// Program to perform Bubble Sort
#include <stdio.h>
int main() {
int n, i, j, temp;
printf("Enter number of elements: ");
scanf("%d", &n);
int a[n];
for(i = 0; i < n; i++)
scanf("%d", &a[i]);
for(i = 0; i < n-1; i++)
for(j = 0; j < n-i-1; j++)
if(a[j] > a[j+1]) {
temp = a[j];
a[j] = a[j+1];
a[j+1] = temp;
}
printf("Sorted array: ");
for(i = 0; i < n; i++)
printf("%d ", a[i]);
return 0;
}
// Sample Output:
// Enter number of elements: 5
// 8 2 4 6 1
// Sorted array: 1 2 4 6 8
Program 8: Recursive Linear Search
// Program to perform Linear Search recursively
#include <stdio.h>
int linearSearch(int a[], int n, int key, int i) {
if(i == n) return -1;
if(a[i] == key) return i;
return linearSearch(a, n, key, i+1);
}
int main() {
int n, key, i, res;
printf("Enter number of elements: ");
scanf("%d", &n);
int a[n];
for(i = 0; i < n; i++)
scanf("%d", &a[i]);
printf("Enter element to search: ");
scanf("%d", &key);
res = linearSearch(a, n, key, 0);
if(res == -1) printf("Not found.");
else printf("Found at position %d", res+1);
return 0;
}
// Sample Output:
// Enter number of elements: 5
// 3 8 2 5 9
// Enter element to search: 5
// Found at position 4
Program 9: 0/1 Knapsack Problem
// Program to solve 0/1 Knapsack Problem using brute-force recursion
#include <stdio.h>
int max(int a, int b) { return (a > b) ? a : b; }
int knapSack(int W, int wt[], int val[], int n) {
if(n == 0 || W == 0) return 0;
if(wt[n-1] > W)
return knapSack(W, wt, val, n-1);
else
return max(val[n-1] + knapSack(W - wt[n-1], wt, val, n-1),
knapSack(W, wt, val, n-1));
}
int main() {
int n, W, i;
printf("Enter number of items: ");
scanf("%d", &n);
int val[n], wt[n];
printf("Enter values and weights: ");
for(i = 0; i < n; i++)
scanf("%d%d", &val[i], &wt[i]);
printf("Enter capacity: ");
scanf("%d", &W);
printf("Maximum value = %d", knapSack(W, wt, val, n));
return 0;
}
// Sample Output:
// Enter number of items: 3
// Enter values and weights: 60 10 100 20 120 30
// Enter capacity: 50
// Maximum value = 220