0% found this document useful (0 votes)
16 views21 pages

Example 1 Practical Program

Practicle fir all

Uploaded by

hofficial996
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
16 views21 pages

Example 1 Practical Program

Practicle fir all

Uploaded by

hofficial996
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 21

Note: Dear students you will write only one program in one page.

The second next page will start

2. All the students are requested to submit the file by Wednesday


onwards. Otherwise they will not allow to Sit in the internal
practical exam.

Example 1:write a program to implement malloc() and free()


// Program to calculate the sum of n numbers entered by the user

#include <stdio.h>

int main() {

int n, i, *ptr, sum = 0;

printf("Enter number of elements: ");

scanf("%d", &n);

ptr = (int*) malloc(n * sizeof(int));

// if memory cannot be allocated

if(ptr == NULL) {

printf("Error! memory not allocated.");

exit(0);

printf("Enter elements: ");

for(i = 0; i < n; ++i) {

scanf("%d", ptr + i);

sum += *(ptr + i);

}
printf("Sum = %d", sum);

// deallocating the memory

free(ptr);

return 0;

Output

Enter number of elements: 3


Enter elements: 100
20
36
Sum = 156

Example 2: write a program to implement calloc() and free()

// Program to calculate the sum of n numbers entered by the user

#include <stdio.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("Error! memory not allocated.");

exit(0);

printf("Enter elements: ");

for(i = 0; i < n; ++i) {

scanf("%d", ptr + i);

sum += *(ptr + i);

printf("Sum = %d", sum);

free(ptr);

return 0;

Output

Enter number of elements: 3


Enter elements: 100
20
36
Sum = 156
Example 3: write a program to implement Factorial of a Number in c
#include <stdio.h>

int main() {

int n, i;

unsigned long long fact = 1;

printf("Enter an integer: ");

scanf("%d", &n);

// shows error if the user enters a negative integer

if (n < 0)

printf("Error! Factorial of a negative number doesn't exist.");

else {

for (i = 1; i <= n; ++i) {

fact *= i;

printf("Factorial of %d = %llu", n, fact);

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];

int front = -1;

int rear = -1;

// Function to insert an element into the queue

void enqueue(int item) {

if (rear == MAX_SIZE - 1) {

printf("Error: Queue is full\n");

return;

if (front == -1) {

front = 0;

rear++;

queue[rear] = item;

// Function to display the elements in the queue

void display() {

if (front == -1 || front > rear) {

printf("Queue is empty\n");
return;

printf("Queue elements are: ");

for (int i = front; i <= rear; i++) {

printf("%d ", queue[i]);

printf("\n");

// Function to check whether the queue is empty

int is_empty() {

if (front == -1 || front > rear) {

return 1;

return 0;

int main() {

printf("Initialize a queue!");

// Insert some elements into the queue.

printf("\nCheck the queue is empty or not? %s\n", is_empty() ? "Yes" : "No");

printf("\nInsert some elements into the queue:\n");

enqueue(1);

enqueue(2);

enqueue(3);

display();
printf("\nInsert another element into the queue:\n");

enqueue(4);

display();

printf("\nCheck the queue is empty or not? %s\n", is_empty() ? "Yes" : "No");

return 0;

Example 5://Write a C program to implement a Stack using an array.


#include <stdio.h>

#define MAX_SIZE 8

int stack[MAX_SIZE];

int top = -1;

void push(int data) {

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("Elements in the stack are: ");

while (top != -1) {

printf("%d ", pop());

printf("\n");

return

}
Example 6: write a program to Implement selection sort in C
#include <stdio.h>

int main()

int arr[5], length = 5, i, j, temp,n,min;

printf("Enter the number of elements : ");

scanf("%d",&n);

printf("Enter %d numbers : ",n);

for (i = 0; i < n; i++)

scanf("%d",&arr[i]);

for (i = 0; i < n-1; i++)

min = i;

for (j = i+1; j < n; j++)

if (arr[j] < arr[min])

min = j;

temp=arr[min];

arr[min]=arr[i];

arr[i]=temp;

printf("Sorted array is : ");


for (i = 0; i < n; i++)

printf(" %d ",arr[i]);

return 0;

Output

Enter the number of elements : 5

Enter 5 numbers : 11 99 44 77 22

Sorted array is : 11 22 44 77 99

Example 7: //write a program to Implement insertion


sort in C
#include <stdio.h>

int main()

int arr[5], length = 5, i, j, temp,n,key;

printf("Enter the number of elements : ");

scanf("%d",&n);

printf("Enter %d numbers : ",n);

for (i = 0; i < n; i++)

{
scanf("%d",&arr[i]);

for (i = 1; i < n; i++) {

key = arr[i];

j = i - 1;

while (key < arr[j] && j >= 0) {

arr[j + 1] = arr[j];

--j;

arr[j + 1] = key;

printf("Sorted array is : ");

for (i = 0; i < n; i++)

printf(" %d ",arr[i]);

return 0;

Output

Enter the number of elements : 5

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>

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;

}
Output

Enter a positive integer: 6


Factorial of 6 = 720

Example 9: write a program to implement Fibonacci Series up to n terms


#include <stdio.h>

int main() {

int i, n;

// initialize first and second terms

int t1 = 0, t2 = 1;

// initialize the next term (3rd term)

int nextTerm = t1 + t2;

// get no. of terms from user

printf("Enter the number of terms: ");

scanf("%d", &n);

// print the first two terms t1 and t2

printf("Fibonacci Series: %d, %d, ", t1, t2);

// print 3rd to nth terms

for (i = 3; i <= n; ++i) {


printf("%d, ", nextTerm);

t1 = t2;

t2 = nextTerm;

nextTerm = t1 + t2;

return 0;

Output

Enter the number of terms: 10


Fibonacci Series: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34,

Example 10: write a Program to implement Check if the entered number is a

Prime Number or not.


#include <stdio.h>

int main() {

int n, i, flag = 0;

printf("Enter a positive integer: ");

scanf("%d", &n);

// 0 and 1 are not prime numbers

// change flag to 1 for non-prime number

if (n == 0 || n == 1)

flag = 1;
for (i = 2; i <= n / 2; ++i) {

// if n is divisible by i, then n is not prime

// change flag to 1 for non-prime number

if (n % i == 0) {

flag = 1;

break;

// flag is 0 for prime numbers

if (flag == 0)

printf("%d is a prime number.", n);

else

printf("%d is not a prime number.", n);

return 0;

Output

Enter a positive integer: 29


29 is a prime number.
Example 11: write a program to implement Relationship Between Arrays

and Pointers

#include <stdio.h>
int main() {
int x[4];
int i;

for(i = 0; i < 4; ++i) {


printf("&x[%d] = %p\n", i, &x[i]);
}

printf("Address of array x: %p", x);

return 0;
}

Output

&x[0] = 1450734448
&x[1] = 1450734452
&x[2] = 1450734456
&x[3] = 1450734460
Address of array x: 1450734448

Example 12:write a program to implement Two-dimensional array


example in C

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

example 13: write a program to implement One-Dimensional


Array in C

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>

int binarySearch(int array[], int x, int low, int high) {


// Repeat until the pointers low and high meet each other
while (low <= high) {
int mid = low + (high - low) / 2;

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;
}

Example 16:Write a program to Linked list implementation in C

#include <stdio.h>

// Creating a node
struct node {
int value;
struct node *next;
};

// print the linked list value


void printLinkedlist(struct node *p) {
while (p != NULL) {
printf("%d ", p->value);
p = p->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));

// Assign value values


one->value = 1;
two->value = 2;
three->value = 3;

// Connect nodes
one->next = two;
two->next = three;
three->next = NULL;

// printing node-value
head = one;
printLinkedlist(head);
}

You might also like