1 Write a program to swap two numbers using pointers.
#include <stdio.h>
void swap(int *a, int *b)
int temp;
temp = *a;
*a = *b;
*b = temp;
int main()
int x, y;
printf("Enter two numbers:\n");
scanf("%d %d", &x, &y);
printf("\nBefore swapping:\n");
printf("x = %d, y = %d\n", x, y);
swap(&x, &y);
printf("\nAfter swapping:\n");
printf("x = %d, y = %d\n", x, y);
return 0;
}
2 Write a program to allocate memory dynamically for an array using malloc, accept values from the
user, and print the array using pointers.
#include <stdio.h>
#include <stdlib.h>
int main()
int *arr, n, i;
printf("Enter number of elements: ");
scanf("%d", &n);
arr = (int *)malloc(n * sizeof(int));
if (arr == NULL)
printf("Memory allocation failed");
return 1;
printf("Enter %d elements:\n", n);
for (i = 0; i < n; i++)
{
scanf("%d", arr + i);
printf("\nArray elements are:\n");
for (i = 0; i < n; i++)
printf("%d ", *(arr + i));
free(arr);
return 0;
3 Implement a program that dynamically allocates memory for an array of integers based on user
input. The program should allow the user to input the size of the array and then enter the elements.
After storing the elements, the program should calculate and display the sum of the array elements.
Finally, the program should free the allocated memory.
#include <stdio.h>
#include <stdlib.h>
int main()
int *arr;
int n, i, sum = 0;
printf("Enter the size of the array: ");
scanf("%d", &n);
arr = (int *)malloc(n * sizeof(int));
if (arr == NULL)
printf("Memory allocation failed");
return 1;
printf("Enter %d elements:\n", n);
for (i = 0; i < n; i++)
scanf("%d", &arr[i]);
sum += arr[i];
printf("\nSum of array elements = %d\n", sum);
free(arr);
return 0;
}
4 Write a program that takes an array of integers and reverses the elements of the array using
pointers
#include <stdio.h>
int main()
int arr[100];
int n, i;
int *start, *end, temp;
printf("Enter number of elements: ");
scanf("%d", &n);
printf("Enter %d elements:\n", n);
for(i = 0; i < n; i++)
scanf("%d", &arr[i]);
start = arr;
end = arr + n - 1;
while(start < end)
temp = *start;
*start = *end;
*end = temp;
start++;
end--;
printf("\nReversed array:\n");
for(i = 0; i < n; i++)
printf("%d ", arr[i]);
return 0;
5 Write a C program that uses pointers to manipulate a string. The program should include functions
to: Calculate the length of the string. Reverse the string
#include <stdio.h>
int stringLength(char *str)
int length = 0;
while (*str != '\0')
length++;
str++;
}
return length;
void reverseString(char *str)
char *start = str;
char *end = str;
char temp;
while (*end != '\0')
end++;
end--;
while (start < end)
temp = *start;
*start = *end;
*end = temp;
start++;
end--;
int main()
char str[100];
printf("Enter a string: ");
scanf(" %[^\n]", str);
printf("\nLength of string: %d\n", stringLength(str));
reverseString(str);
printf("Reversed string: %s\n", str);
return 0;
6 Define a structure to represent a student (e.g., name, roll number, and marks). Use a pointer to a
structure to access and print the student details..
#include <stdio.h>
struct Student
char name[50];
int roll_no;
float marks;
};
int main()
struct Student s = {"Amit Kumar", 12, 87.5};
struct Student *ptr;
ptr = &s;
printf("Student Details\n");
printf("Name: %s\n", ptr->name);
printf("Roll Number: %d\n", ptr->roll_no);
printf("Marks: %.2f\n", ptr->marks);
return 0;
7 Implement a program that demonstrates the use of function pointers in C.
#include <stdio.h>
int add(int a, int b)
return a + b;
int subtract(int a, int b)
return a - b;
int multiply(int a, int b)
return a * b;
}
int main()
int x = 10, y = 5;
int result;
int (*operation)(int, int);
operation = add;
result = operation(x, y);
printf("Addition: %d\n", result);
operation = subtract;
result = operation(x, y);
printf("Subtraction: %d\n", result);
operation = multiply;
result = operation(x, y);
printf("Multiplication: %d\n", result);
return 0;