Reverse the array elements - C Program

Write a C program to take the input into an array and reverse the element of the array (Dynamic array).

Solution:

In this example, we will take the dynamic array from the user and then reverse the order of the array element. The malloc function is used to store the dynamic array.

Program

#include<conio.h>
#include<stdio.h>
#include<stdlib.h>
void reverse(int);
int main()
{
    int n;
    printf("Enter total number of elements to reverse: ");
    scanf("%d",&n);
    reverse(n);
    return(0);
}
void reverse(int n)
{
    int *p,tmp,i;
    p=((int *)malloc(n*sizeof(int)));
    printf("Enter elements: ");
    for(i=0;i<n;i++)
    {
        scanf("%d",(p+i));
    }
    for(i=0;i<n/2;i++)
    {
        tmp=*(p+i);
        *(p+i)=*(p+n-1-i);
        *(p+n-1-i)=tmp;
    }
    printf("\n The reversed array is: ");
    for(i=0;i<n;i++)
    {
        printf("%d ",*(p+i));
    }
    return(0);
}


Output:

reverse array element