You are on page 1of 5

[28/02, 10:30 am] Ganesh Clg: #include <stdio.

h>

unsigned long long factorial(int n) {


unsigned long long result = 1;
for (int i = 1; i <= n; ++i) {
result *= i;
}
return result;
}

int main() {
int n = 5;
printf("Factorial of %d is %llu\n", n, factorial(n));
return 0;
}
[28/02, 10:30 am] Ganesh Clg: #include <stdio.h>

unsigned long long factorial(int n) {


if (n == 0 || n == 1)
return 1;
else
return n * factorial(n - 1);
}

int main() {
int n = 5;
printf("Factorial of %d is %llu\n", n, factorial(n));
return 0;
}
[28/02, 10:30 am] Ganesh Clg: #include<stdio.h>

// Function to find maximum of two integers


int max(int a, int b) {
return (a > b) ? a : b;
}

// Function to solve knapsack problem


int knapSack(int W, int wt[], int val[], int n) {
int i, w;
int K[n+1][W+1];

// Build table K[][] in bottom up manner


for (i = 0; i <= n; i++) {
for (w = 0; w <= W; w++) {
if (i == 0 || w == 0)
K[i][w] = 0;
else if (wt[i-1] <= w)
K[i][w] = max(val[i-1] + K[i-1][w-wt[i-1]], K[i-1][w]);
else
K[i][w] = K[i-1][w];
}
}

return K[n][W];
}

int main() {
int val[] = {60, 100, 120};
int wt[] = {10, 20, 30};
int W = 50;
int n = sizeof(val)/sizeof(val[0]);
printf("Maximum value that can be obtained: %d", knapSack(W, wt, val, n));
return 0;
}
[28/02, 10:30 am] Ganesh Clg: #include <stdio.h>
#include <stdbool.h>

#define N 8 // Change N to the desired board size

bool isSafe(int board[N][N], int row, int col) {


int i, j;

// Check this row on the left side


for (i = 0; i < col; i++)
if (board[row][i])
return false;

// Check upper diagonal on left side


for (i = row, j = col; i >= 0 && j >= 0; i--, j--)
if (board[i][j])
return false;

// Check lower diagonal on left side


for (i = row, j = col; j >= 0 && i < N; i++, j--)
if (board[i][j])
return false;

return true;
}

bool solveNQueens(int board[N][N], int col) {


if (col >= N)
return true;

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


if (isSafe(board, i, col)) {
board[i][col] = 1;

if (solveNQueens(board, col + 1))


return true;

board[i][col] = 0; // Backtrack
}
}

return false;
}

void printSolution(int board[N][N]) {


for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++)
printf("%d ", board[i][j]);
printf("\n");
}
}

int main() {
int board[N][N] = {0};

if (solveNQueens(board, 0) == false) {
printf("Solution does not exist");
} else {
printf("Solution:\n");
printSolution(board);
}

return 0;
}
[28/02, 10:30 am] Ganesh Clg: #include <stdio.h>
#include <stdlib.h>

struct MinHeapNode {
char data;
unsigned freq;
struct MinHeapNode *left, *right;
};

struct MinHeap {
unsigned size;
unsigned capacity;
struct MinHeapNode** array;
};

// Function to create a new min heap node


struct MinHeapNode* newNode(char data, unsigned freq)
{
struct MinHeapNode* temp
= (struct MinHeapNode*)malloc
(sizeof(struct MinHeapNode));

temp->left = temp->right = NULL;


temp->data = data;
temp->freq = freq;

return temp;
}

// Function to create a min heap


struct MinHeap* createMinHeap(unsigned capacity)
{
struct MinHeap* minHeap
= (struct MinHeap*)malloc(sizeof(struct MinHeap));

minHeap->size = 0;
minHeap->capacity = capacity;
minHeap->array
= (struct MinHeapNode**)malloc(minHeap->
capacity * sizeof(struct MinHeapNode*));
return minHeap;
}

// Other functions like minHeapify, extractMin, insert, etc. are needed.

// Huffman encoding function


void HuffmanCodes(char data[], int freq[], int size)
{
// Create a min heap & build the heap
struct MinHeap* minHeap = createMinHeap(size);

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


minHeap->array[i] = newNode(data[i], freq[i]);

// Build the Huffman tree


// ...

// Print Huffman codes


// ...
}

int main()
{
char data[] = {'a', 'b', 'c', 'd', 'e', 'f'};
int freq[] = {5, 9, 12, 13, 16, 45};
int size = sizeof(data) / sizeof(data[0]);

HuffmanCodes(data, freq, size);

return 0;
}

You might also like