Example 1 Practical Program
Example 1 Practical Program
#include <stdio.h>
int main() {
scanf("%d", &n);
if(ptr == NULL) {
exit(0);
}
printf("Sum = %d", sum);
free(ptr);
return 0;
Output
#include <stdio.h>
int main() {
scanf("%d", &n);
if(ptr == NULL) {
printf("Error! memory not allocated.");
exit(0);
free(ptr);
return 0;
Output
int main() {
int n, i;
scanf("%d", &n);
if (n < 0)
else {
fact *= i;
return 0;
Output
Enter an integer: 10
Factorial of 10 = 3628800
Example 4://Write a C program to implement a queue using an array
#include <stdio.h>
#define MAX_SIZE 10
int queue[MAX_SIZE];
if (rear == MAX_SIZE - 1) {
return;
if (front == -1) {
front = 0;
rear++;
queue[rear] = item;
void display() {
printf("Queue is empty\n");
return;
printf("\n");
int is_empty() {
return 1;
return 0;
int main() {
printf("Initialize a queue!");
enqueue(1);
enqueue(2);
enqueue(3);
display();
printf("\nInsert another element into the queue:\n");
enqueue(4);
display();
return 0;
#define MAX_SIZE 8
int stack[MAX_SIZE];
if (top == MAX_SIZE - 1) {
printf("Overflow stack!\n");
return;
top++;
stack[top] = data;
int pop() {
if (top == -1) {
printf("Stack is empty!\n");
return -1;
}
int data = stack[top];
top--;
return data;
int main()
push(1);
push(2);
push(3);
push(4);
push(5);
push(3);
printf("\n");
return
}
Example 6: write a program to Implement selection sort in C
#include <stdio.h>
int main()
scanf("%d",&n);
scanf("%d",&arr[i]);
min = i;
min = j;
temp=arr[min];
arr[min]=arr[i];
arr[i]=temp;
printf(" %d ",arr[i]);
return 0;
Output
Enter 5 numbers : 11 99 44 77 22
Sorted array is : 11 22 44 77 99
int main()
scanf("%d",&n);
{
scanf("%d",&arr[i]);
key = arr[i];
j = i - 1;
arr[j + 1] = arr[j];
--j;
arr[j + 1] = key;
printf(" %d ",arr[i]);
return 0;
Output
Enter 5 numbers : 11 99 44 77 22
Sorted array is : 11 22 44 77 99
Example 8: write a program to implement Factorial of a Number Using
Recursion
#include<stdio.h>
int main() {
int n;
scanf("%d",&n);
return 0;
if (n>=1)
return n*multiplyNumbers(n-1);
else
return 1;
}
Output
int main() {
int i, n;
int t1 = 0, t2 = 1;
scanf("%d", &n);
t1 = t2;
t2 = nextTerm;
nextTerm = t1 + t2;
return 0;
Output
int main() {
int n, i, flag = 0;
scanf("%d", &n);
if (n == 0 || n == 1)
flag = 1;
for (i = 2; i <= n / 2; ++i) {
if (n % i == 0) {
flag = 1;
break;
if (flag == 0)
else
return 0;
Output
and Pointers
#include <stdio.h>
int main() {
int x[4];
int i;
return 0;
}
Output
&x[0] = 1450734448
&x[1] = 1450734452
&x[2] = 1450734456
&x[3] = 1450734460
Address of array x: 1450734448
1. #include<stdio.h>
2. int main(){
3. int i=0,j=0;
4. int arr[4][3]={{1,2,3},{2,3,4},{3,4,5},{4,5,6}};
5. //traversing 2D array
6. for(i=0;i<4;i++){
7. for(j=0;j<3;j++){
8. printf("arr[%d] [%d] = %d \n",i,j,arr[i][j]);
9. }//end of j
10. }//end of i
11. return 0;
12. }
Output
arr[0][0] = 1
arr[0][1] = 2
arr[0][2] = 3
arr[1][0] = 2
arr[1][1] = 3
arr[1][2] = 4
arr[2][0] = 3
arr[2][1] = 4
arr[2][2] = 5
arr[3][0] = 4
arr[3][1] = 5
arr[3][2] = 6
1. #include <stdio.h>
2. int main() {
3. int numbers[5] = {10, 20, 30, 40, 50};
4. for(int i=0; i<5; i++) {
5. printf("numbers[%d] = %d\n", i, numbers[i]);
6. }
7. return 0;
8. }
Output:
numbers[0] = 10
numbers[1] = 20
numbers[2] = 30
numbers[3] = 40
numbers[4] = 50
example 14: Write a program to implement heap sort in C language.
1. #include <stdio.h>
2. /* function to heapify a subtree. Here 'i' is the
3. index of root node in array a[], and 'n' is the size of heap. */
4. void heapify(int a[], int n, int i)
5. {
6. int largest = i; // Initialize largest as root
7. int left = 2 * i + 1; // left child
8. int right = 2 * i + 2; // right child
9. // If left child is larger than root
10. if (left < n && a[left] > a[largest])
11. largest = left;
12. // If right child is larger than root
13. if (right < n && a[right] > a[largest])
14. largest = right;
15. // If root is not largest
16. if (largest != i) {
17. // swap a[i] with a[largest]
18. int temp = a[i];
19. a[i] = a[largest];
20. a[largest] = temp;
21.
22. heapify(a, n, largest);
23. }
24. }
25. /*Function to implement the heap sort*/
26. void heapSort(int a[], int n)
27. {
28. for (int i = n / 2 - 1; i >= 0; i--)
29. heapify(a, n, i);
30. // One by one extract an element from heap
31. for (int i = n - 1; i >= 0; i--) {
32. /* Move current root element to end*/
33. // swap a[0] with a[i]
34. int temp = a[0];
35. a[0] = a[i];
36. a[i] = temp;
37.
38. heapify(a, i, 0);
39. }
40. }
41. /* function to print the array elements */
42. void printArr(int arr[], int n)
43. {
44. for (int i = 0; i < n; ++i)
45. {
46. printf("%d", arr[i]);
47. printf(" ");
48. }
49.
50. }
51. int main()
52. {
53. int a[] = {48, 10, 23, 43, 28, 26, 1};
54. int n = sizeof(a) / sizeof(a[0]);
55. printf("Before sorting array elements are - \n");
56. printArr(a, n);
57. heapSort(a, n);
58. printf("\nAfter sorting array elements are - \n");
59. printArr(a, n);
60. return 0;
61. }
Output
//example 15:write a program to implement Binary Search in C
#include <stdio.h>
if (array[mid] == x)
return mid;
if (array[mid] < x)
low = mid + 1;
else
high = mid - 1;
}
return -1;
}
int main(void) {
int array[] = {3, 4, 5, 6, 7, 8, 9};
int n = sizeof(array) / sizeof(array[0]);
int x = 4;
int result = binarySearch(array, x, 0, n - 1);
if (result == -1)
printf("Not found");
else
printf("Element is found at index %d", result);
return 0;
}
#include <stdio.h>
// Creating a node
struct node {
int value;
struct node *next;
};
int main() {
// Initialize nodes
struct node *head;
struct node *one = NULL;
struct node *two = NULL;
struct node *three = NULL;
// Allocate memory
one = malloc(sizeof(struct node));
two = malloc(sizeof(struct node));
three = malloc(sizeof(struct node));
// Connect nodes
one->next = two;
two->next = three;
three->next = NULL;
// printing node-value
head = one;
printLinkedlist(head);
}