Write a program to count the palindromic array elements from the group of 30 array elements where array is allocated memory dynamically.
#include <stdio.h>
#include <stdlib.h>
int main() {
int n, reversedN = 0, remainder, originalN;
int* ptr;
int k, i;
printf("Enter an integer: ");
scanf("%d", &n);
originalN = n;
k = 30;
printf("Enter number of elements: %d\n", n);
ptr = (int*)malloc(n * sizeof(int));
if (ptr == NULL) {
printf("Memory not allocated.\n");
exit(0);
}
else {
printf("Memory successfully allocated using malloc.\n");
for (i = 0; i < n; ++i) {
ptr[i] = i + 1;
}
printf("The elements of the array are: ");
for (i = 0; i < n; ++i) {
printf("%d, ", ptr[i]);
}
}
while (n != 0) {
remainder = n % 10;
reversedN = reversedN * 10 + remainder;
n /= 10;
}
if (originalN == reversedN)
printf("%d is a palindrome.", originalN);
else
printf("%d is not a palindrome.", originalN);
return 0;
}
Comments
Leave a comment