HackerRank C- Array Reversal




Given an array, of size n, reverse it.

Example: If array is arr=[1,2,3,4,5] , after reversing it, the array should be, arr=[5,4,3,2,1]



  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. int main()
  4. {
  5. int num, *arr,*rev_arr, i,j;
  6. scanf("%d", &num);
  7. arr = (int*) malloc(num * sizeof(int));
  8. rev_arr = (int*) malloc(num * sizeof(int));
  9. for(i = 0; i < num; i++) {
  10. scanf("%d", arr + i);
  11. }
  12. for(i = num-1,j=0; i>=0&&j<num; i--,j++){
  13. *(rev_arr+j)=*(arr+i);
  14. }
  15. for(int i=0;i<num;i++)
  16. {
  17. *(arr+i)=*(rev_arr+i);
  18. printf("%d ",*(arr+i));
  19. }
  20. return 0;
  21. }




codesadda.com

Codesadda.com is your home of programming solutions, tutorials, video tutorials and much more. Sign Up for our weekly newsletter to get update about new content.

Like us on Facebook | Connect with us on LinkedIn | Subscribe our Channel on Youtube