1) Write a program example of malloc() function.
#include<stdio.h>
#include<stdlib.h>
int main(){
int n,i,*ptr,sum=0;
printf("Enter number of elements: ");
scanf("%d",&n);
ptr=(int*)malloc(n*sizeof(int));
if(ptr==NULL)
{
printf("Sorry! unable to allocate memory");
exit(0);
12. }
printf("Enter elements of array: ");
for(i=0;i<n;++i)
{
scanf("%d",ptr+i);
sum+=*(ptr+i);
18. }
printf("Sum=%d",sum);
free(ptr);
return 0;
}
2) Write program example of calloc() function.
#include<stdio.h>
#include<stdlib.h>
int main(){
int n,i,*ptr,sum=0;
printf("Enter number of elements: ");
scanf("%d",&n);
ptr=(int*)calloc(n,sizeof(int));
if(ptr==NULL)
{
printf("Sorry! unable to allocate memory");
exit(0);
12. }
printf("Enter elements of array: ");
for(i=0;i<n;++i)
{
scanf("%d",ptr+i);
sum+=*(ptr+i);
18. }
printf("Sum=%d",sum);
free(ptr);
return 0;
}
3) Write program to create stack with array and linked list implementation.
#include<stdio.h>
#include<process.h>
#include<stdlib.h>
#define MAX 5
void pop();
void
display();
int main()
{
int ch;
while(1)
{
printf("\n*** Stack Menu ***");
printf("\n\n1.Push\n2.Pop\n3.Display\n4.Exit");
printf("\n\nEnter your choice(1-4):");
scanf("%d",&ch);
switch(ch)
{
case 1:
push();
break;
case 2: pop();
break;
case 3: display();
break;
case 4: exit(0);
default: printf("\nWrong Choice!!");
}}}
void push()
{
int val;
if(top==MAX- 1)
{
printf("\nStack is full!!");
}
else
{
printf("\nEnter element to push:");
scanf("%d",&val);
top=top+1;
stack[top]=val;
}}
void pop()
{if(top==-1)
{
printf("\nStack is empty!!");
}
else
{
printf("\nDeleted element is %d",stack[top]);
top=top-1;
} }
void display()
{
int i;
if(top==)
{
printf("\nStack is empty!!");
}
else
{
printf("\nStack is...\n");
for(i=top;i>=0;--i)
printf("%d\n",stack[i]);
} }
4) Write a program to print Fibonacci series.
#include<stdio.h>
void printFibonacci(int n){ static int n1=0,n2=1,n3; if(n>0){
n3 = n1 + n2;
n1 = n2; n2 = n3;
printf("%d ",n3);
printFibonacci(n-1);
} }
int main(){ int n;
printf("Enter the number of elements: ");
scanf("%d",&n);
printf("Fibonacci Series: ");
printf("%d %d ",0,1);
printFibonacci(n-2);
return 0;
}
5) Write a program to calculate factorial.
#include<stdio.h>
long int multiplyNumbers(int n); int main() {
int n;
printf("Enter a positive integer: "); scanf("%d",&n);
printf("Factorial of %d = %ld", n, multiplyNumbers(n));
return 0;
}
long int multiplyNumbers(int n)
{
if (n>=1)
return n*multiplyNumbers(n-1);
else;
return 1;
}
6) Write a program to implement binary tree.
#includ<stdio.h>
#include<stdlib.h>
struct node
{
int value;
struct node *left_child, *right_child;
};
struct node *new_node(int value)
{
struct node *tmp = (struct node *)malloc(sizeof(struct node));
tmp->value = value;
tmp->left_child = tmp->right_child = NULL;
return tmp;
}
void print(struct node *root_node)
{
if (root_node != NULL)
{
print(root_node->left_child); printf("%d \n", root_node->value); print(root_node->right_child);
} }
struct node* insert_node(struct node* node, int value)
{
if (node == NULL) return new_node(value); if (value < node->value)
{
node->left_child = insert_node(node->left_child, value);
}
else if (value > node->value)
{
node->right_child = insert_node(node->right_child, value);
}
return node;
}
int main()
{
printf("TechVidvan Tutorial: Implementation of a Binary Tree in C!\n\n");
struct node *root_node = NULL; root_node = insert_node(root_node, 10); insert_node(root_node, 10);
insert_node(root_node, 30);
insert_node(root_node, 25);
insert_node(root_node, 36);
insert_node(root_node, 56);
insert_node(root_node, 78);
print(root_node);
return 0;
}
7) Write a program to implement bubble sort in C.
#include <stdio.h>
int main()
{
int array[100], n, c, d, swap;
printf("Enter number of elements\n"); scanf("%d", &n);
printf("Enter %d integers\n", n);
for (c = 0; c < n; c++) scanf("%d", &array[c]);
for (c = 0 ; c < n - 1; c++)
{
for (d = 0 ; d < n - c - 1; d++)
{
if (array[d] > array[d+1])
{
swap = array[d]; array[d] = array[d+1]; array[d+1] = swap;
} } }
printf("Sorted list in ascending order:\n");
for (c = 0; c < n; c++) printf("%d\n", array[c]);
return 0;
}
8) Write a program to implement selection sort in C.
#include <stdio.h>
int main()
{
int array[100], n, c, d, position, t;
printf("Enter number of elements\n"); scanf("%d", &n);
printf("Enter %d integers\n", n); for (c = 0; c < n; c++)
scanf("%d", &array[c]);
for (c = 0; c < (n - 1); c++)
{
position = c;
for (d = c + 1; d < n; d++)
{
if (array[position] > array[d]) position = d;
}
if (position != c)
{
t = array[c];
array[c] = array[position]; array[position] = t;
} }
printf("Sorted list in ascending order:\n"); for (c = 0; c < n; c++)
printf("%d\n", array[c]);
return 0;
}