Explore
ProGrad Programs
Placement Prep
TCS Codevita
Live Placement Training
Webinars
About Us

Edit
Reply




Edit

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

Published on 11 Mar 2020

Program to reverse the elements of an array is discussed here. Given an array of integers, all the elements of the array 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 array


C
C++
Java
Python 3

Output
4 (size of the array)
2 (array elements)
6
7
1
Reversed array is:
1
7
6
2


Program to reverse an array using pointers


// The same program, but using POINTERS
#include<stdio.h>
void reverse(int n,int *a)
{
int i;
printf(“Reversed array is:\n”);
for(i=n-1;i>=0;i–)
{
printf(“%d\n”,a[i]);
}
}
int main()
{
int n,*a,i;
scanf(“%d”,&n);
for(i=0;i<n;i++)
{
scanf(“%d”,a+i);
}
reverse(n,a);
return 0;
}


Recommended Programs






If you have any feedback about this article and want to improve this, please write to enquiry@faceprep.in