21. write a C program to display the array elements in reverse order using pointers


Program to display the array elements in reverse order using pointers

Source Code:

 #include <stdio.h>
 #include <stdlib.h>
int main()
{
  int tmp,size,i,j,*arr;
  printf("Enter size of array:");
  scanf("%d",&size);
  arr=calloc(sizeof(int),size);
  printf("Enter elements in array: ");
  for(i=0;i<size;i++)
    scanf("%d",arr+i);
      for(i=0,j=size-1;i<j;i++,j--){
    //swap the elements 
    tmp=*(arr+i);
    *(arr+i)=*(arr+j);
    *(arr+j)=tmp;
  }
    printf("After reversing the array: ");
  for(i=0;i<size;i++)
    printf("%d ",*(arr+i));
    return 0;
}

Output: 

Enter size of array:5
Enter elements in array: 1
2
3
5
6
After reversing the array: 6 5 3 2 1 

No comments:

Post a Comment