Program to reverse an array | Program to reverse the elements of an array

Program to reverse an array | Program to reverse the elements of an array

Various techniques and algorithms can be used to reverse an array, including printing it from the last element, utilizing an auxiliary array, recursion, swapping, etc. Some of the program to reverse the elements of an array is discussed here.

Program to reverse an array


Given an array of integers, all the array elements are reversed.

For example, consider the array

Input:

arr = {1, 2, 3, 4, 5}

Output:

Reversed array: {5, 4, 3, 2, 1}

Algorithm to reverse an array

  • Input the number of elements of an array.
  • Input the array elements.
  • Traverse the array from the last.
  • Print all the elements.


program to reverse an arrayClick here to learn more about FACE Prep PRO


Programs to reverse an array

@@coding::1@@


Revering the array using pointers


// The same program, but using POINTERSn#include<stdio.h>nvoid reverse(int n,int *a)n{nint i;nprintf(“Reversed array is:n”);nfor(i=n-1;i>=0;i–)n{nprintf(“%dn”,a[i]);n}n}nint main()n{nint n,*a,i;nscanf(“%d”,&n);nfor(i=0;i<n;i++)n{nscanf(“%d”,a+i);n}nreverse(n,a);nreturn 0;n}n


Recommended Programs


Program to reverse an array

c