POINTERS
POINTERS
(Topic: Pointers)
ENROLLMENT NUMBER-NJG246523
1.Write a C program using a pointer to declare a variable var with value 20 and print the value
CODE:-
#include <stdio.h>
int main()
ptr = &var;
return 0;
OUTPUT:-
CODE:-
#include <stdio.h>
int main()
{
char str[100];
scanf("%[^\n]%*c", str);
int len = 0;
ptr++;
len++;
return 0;
OUTPUT:-
CODE:-
#include <stdio.h>
int main()
int size;
scanf("%d",&size);
int arr[size];
scanf("%d", &arr[i]);
int sum = 0;
ptr++;
return 0;
OUTPUT:-
4. WAP to check if the number is prime or not using pointers.
CODE:-
#include <stdio.h>
#include<math.h>
int main()
int n;
int *p = &n;
scanf("%d", p);
int isprime = 1;
if (*p % i == 0)
isprime = 0;
break;
if (isprime == 1)
else
{
return 0;
OUTPUT:-
CODE:-
#include <stdio.h>
int main()
int size;
scanf("%d",&size);
int arr[size];
scanf("%d", &arr[i]);
}
int *p = arr;
int *q = &arr[size-1];
while (p < q)
*p = *q;
*q = temp;
p++;
q--;
return 0;
OUTPUT:-
6. WAP to check if two strings are equal using pointers
CODE:-
#include <stdio.h>
void main()
scanf("%[^\n]%*c", str1);
scanf("%[^\n]%*c", str2);
p = str1;
q = str2;
int is_equal = 1;
{
if (*p != *q)
is_equal = 0;
break;
p++;
q++;
if (is_equal)
else
OUTPUT:-
7. Write a C program to find the kth smallest element in an integer array using pointers and
pointer arithmetic. Allow the user to input the array and the value of k. Assume k is valid (1
<= k <= array size).
CODE:-
#include <stdio.h>
int main()
int size;
scanf("%d",&size);
int arr[size];
scanf("%d", &arr[i]);
int k;
scanf("%d", &k);
int i, j;
int key;
j = i - 1;
j--;
*(arr + j + 1) = key;
printf("\n");
return 0;
OUTPUT:-
8. Write a C program to reverse a string using pointers. Allow the user to input a string, reverse
CODE:-
#include <stdio.h>
#include <string.h>
int main() {
char arr[100];
scanf("%[^\n]%*c", arr);
char temp;
for (int i = 0; i < len / 2; i++) {
return 0;
OUTPUT:-
9. Write a C program that takes a 2D integer array as input from the user. Assign an integer
pointer to the same. Then after that, calculate and print the sum of all the elements in the
array.
CODE:-
#include <stdio.h>
int main()
int r, c;
int arr[r][c];
int *ptr;
int i, j;
int sum = 0;
}
}
return 0;
OUTPUT:-
10.Write a C program that checks if a given string is a palindrome (reads the same forwards and
backwards) using pointers and pointer arithmetic. Allow the user to input a string.
CODE:-
#include <stdio.h>
#include <string.h>
int main() {
char str[100];
scanf("%[^\n]%*c", str);
int isPalindrome = 1;
if (*start != *end) {
isPalindrome = 0;
break;
start++;
end--;
if (isPalindrome) {
} else {
return 0;
}
OUTPUT:-
11.Write a C program that counts and prints the number of vowels in a given string using
CODE:-
#include <stdio.h>
int main()
char *s;
char str[20];
int c = 0;
scanf("%[^\n]%*c", str);
s = &str[0];
c = c + 1;
}
printf("THE TOTAL NUMBER OF VOWELS = %d", c);
return 0;
OUTPUT:-
12.Write a program to print the elements of the array using dynamic memory allocation in C.
CODE:-
#include <stdio.h>
#include <stdlib.h>
int main() {
int size;
scanf("%d", &size);
scanf("%d", &arr[i]);
printf("\n");
free(arr);
return 0;
OUTPUT:-
13.Write a program to read a one-dimensional array, print sum of all elements along with
CODE:-
#include <stdio.h>
#include <stdlib.h>
int main() {
int size;
scanf("%d", &size);
if (arr == NULL) {
return 1;
int sum = 0;
scanf("%d", &arr[i]);
sum += arr[i];
printf("\n");
printf("Sum of all elements: %d\n", sum);
free(arr);
return 0;
OUTPUT:-