Answer to Question #186614 in C++ for Pronoy

Question #186614

Write a program to count the palindromic array elements from the group of 30 array elements where array is allocated memory dynamically.


1
Expert's answer
2021-04-28T16:48:38-0400
#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;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS